简体   繁体   English

如何将allegro5库链接到C ++项目

[英]How to link allegro5 library to C++ project

I have got a C++ project written in CodeBlocks but I do not know how to link it with allegro5. 我有一个用CodeBlocks编写的C ++项目,但我不知道如何将其与allegro5链接。 I want to make people able to compile my project without installing any libraries or packages. 我想使人们能够在不安装任何库或软件包的情况下编译我的项目。 So I put allegro directory downloaded from their webpage into project folder. 因此,我将从他们的网页下载的allegro目录放入项目文件夹。 Next I included library 接下来我包括图书馆

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

But when I try to compile I have got an error 但是当我尝试编译时出现错误

/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'

This is my guiMain.cpp: 这是我的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;
}

And this is my CMakeList.txt 这是我的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: I have changed set in my CMakeList 更新:我已经更改了我的CMakeList中的设置

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

but I have got an error: 但我有一个错误:

    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) 

and

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)

Actually, it is possible to allow others to play your game without intalling the allegro libraries on their system. 其实,这可以让别人玩你的游戏,而不intalling他们的系统上的快板库。

One approach is to statically link your executable to static versions of the allegro libraries. 一种方法是将可执行文件静态链接到Allegro库的静态版本。 The allegro wiki actually has a tutorial on how to do this with Code::Blocks. allegro Wiki实际上有一个有关如何使用Code :: Blocks进行操作的教程

In short, static linkage includes the necessary parts of Allegro directly in your executable so the user doesn't have to worry about linking when they run it. 简而言之,静态链接将Allegro的必要部分直接包含在可执行文件中,因此用户在运行链接时不必担心链接。

Another option is to use dynamic linkage, but package the needed dynamic libraries along with your game. 另一种选择是使用动态链接,但将所需的动态库与您的游戏一起打包。 For example, your game folder might look like: 例如,您的游戏文件夹可能如下所示:

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

Then the user can link to the libraries you have provided without worrying about what is installed on their system. 然后,用户可以链接到您提供的库,而不必担心系统上安装了什么。

Note that dynamic libraries typically end with .so (Linux) or .dll (Windows) while static libraries end with .a . 请注意,动态库通常以.so (Linux)或.dll (Windows)结尾,而静态库通常以.a结尾。

Edit: 编辑:

Just read you said compile and not run . 刚读完,你说编译就不运行 If you want them to compile it themselves, you will need to include the libraries or instruct them on how to get the libraries themselves. 如果希望他们自己进行编译,则需要包括这些库或指导他们如何获取这些库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM