2016-03-09 36 views
1

我有一个用CodeBlocks编写的C++项目,但我不知道如何将它与allegro5链接。我想让人们能够在不安装任何库或包的情况下编译我的项目。所以我把从他们网页上下载的allegro目录放到项目文件夹中。接下来,我包括图书馆如何将allegro5库链接到C++项目

#include "allegro/include/allegro5/allegro5.h" 

但是,当我尝试编译我有一个错误

/home/stanek/ClionProjects/proj/guiMain.cpp:17: undefined reference to `al_install_system' 
/home/stanek/ClionProjects/proj/guiMain.cpp:22: undefined reference to `al_create_display' 
/home/stanek/ClionProjects/proj/guiMain.cpp:28: undefined reference to `al_map_rgb' 
/home/stanek/ClionProjects/proj/guiMain.cpp:28: undefined reference to `al_clear_to_color' 
/home/stanek/ClionProjects/proj/guiMain.cpp:30: undefined reference to `al_flip_display' 
/home/stanek/ClionProjects/proj/guiMain.cpp:32: undefined reference to `al_rest' 
/home/stanek/ClionProjects/proj/guiMain.cpp:34: undefined reference to `al_destroy_display' 

这是我guiMain.cpp:

#include <iostream> 
#include "allegro/include/allegro5/allegro5.h" 


using namespace std; 

int main(int argc, char **argv){ 

    ALLEGRO_DISPLAY *display = NULL; 

    if(!al_init()) { 
     fprintf(stderr, "failed to initialize allegro!\n"); 
     return -1; 
    } 

    display = al_create_display(640, 480); 
    if(!display) { 
     fprintf(stderr, "failed to create display!\n"); 
     return -1; 
    } 

    al_clear_to_color(al_map_rgb(0,0,0)); 

    al_flip_display(); 

    al_rest(10.0); 

    al_destroy_display(display); 

    return 0; 
} 

这是我CMakeList.txt

cmake_minimum_required(VERSION 3.3) 
project(proj) 

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++11") 

set(SOURCE_FILES guiMain.cpp) 
add_executable(projids ${SOURCE_FILES}) 

UPDATE: 我在CMakeList

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++11 'pkg-config --cflags --libs allegro-5.0 '") 

改变了一套,但我得到了一个错误:

Error:Configuration proj [Debug] 
Compiler exited with error code 1: /usr/bin/c++ -xc++ -g -Wall -std=c++11 'pkg-config --cflags --libs allegro-5.0 ' -g -v -dD -E 
c++: error: pkg-config --cflags --libs allegro-5.0 : No such file or directory 
Using built-in specs. 
COLLECT_GCC=/usr/bin/c++ 
Target: x86_64-unknown-linux-gnu 
Configured with: /build/gcc/src/gcc-5-20160209/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release 
Thread model: posix 
gcc version 5.3.0 (GCC) 

Error:Configuration proj [Release] 
Compiler exited with error code 1: /usr/bin/c++ -xc++ -g -Wall -std=c++11 'pkg-config --cflags --libs allegro-5.0 ' -O3 -DNDEBUG -v -dD -E 
c++: error: pkg-config --cflags --libs allegro-5.0 : No such file or directory 
Using built-in specs. 
COLLECT_GCC=/usr/bin/c++ 
Target: x86_64-unknown-linux-gnu 
Configured with: /build/gcc/src/gcc-5-20160209/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release 
Thread model: posix 
gcc version 5.3.0 (GCC) 
+1

_“我想让人们能够编译我的项目,而无需安装任何库或包。”_这是不可能的。 –

+0

哦对,所以我必须在我的电脑上安装allegro,并且我想将我的项目发送给我的讲师,那么他还必须安装快捷键? – Mateusz

+0

是的,您还必须链接库以摆脱链接器错误。 –

回答

1

其实,这可以允许其他人玩他们的游戏而不安装他们系统上的allegro库。

一种方法是你的可执行文件静态链接到 快板库的静态版本。关于如何在Code :: Blocks中执行此操作,allegro wiki实际上有一个 tutorial

总之,静态链接直接包括快板的必要部分在 你的可执行文件,因此用户不必担心链接时,他们跑 它。

另一种选择是使用动态链接,但将所需的动态 库与您的游戏一起打包。例如, 你的游戏文件夹可能看起来像:

bin/ 
    my_game.exe 
    liballegro.dll 
    liballegro_color.dll 
    ... 

然后,用户可以链接到你不用担心什么是其系统上安装提供的库。

请注意,动态库通常以.so(Linux)或.dll(Windows)结尾,而静态库以.a结尾。

编辑:

刚才看了你说编译而不是运行。如果您希望他们自己编译它,您需要包含这些库或指导他们如何获取库本身。

+0

好的,谢谢。但是,即使我已经安装了allegro,并且包含了“#include ”,我仍然遇到了同样的错误。包含标题的 – Mateusz

+1

是不够的,您需要链接到库。查看上面的链接或https://wiki.allegro.cc/index.php?title=Compiling_Allegro_Programs – rcorre