引用 Allegro5 插件的问题

Problems referencing Allegro5 addons

我正在尝试在 CLion 中使用 Allegro5,但我无法引用插件,尽管它可以找到头文件和 lib 文件。

我在“/usr/lib”中有库,在“/usr/include”中有头文件,此外,我在 proyect 目录中有库和头文件。

我可以使用和编译这个:

[..]
al_set_window_title(display, "title");
al_clear_to_color(al_map_rgb(4,0,90));
ALLEGRO_COLOR electric_blue = al_map_rgb(44, 117, 255);

但我无法编译它(尽管有检测头和函数):

#include <allegro5/allegro_primitives.h>
[..]
al_draw_line(100,500,300,500,electric_blue,6.0);

这是错误:

Scanning dependencies of target project
[  9%] Building CXX object CMakeFiles/project.dir/main.cpp.o
[ 18%] Linking CXX executable project
CMakeFiles/project.dir/main.cpp.o: En la función `main':
/home/lifka/Desktop/tetris/project/main.cpp:80: reference to `al_draw_line' undefined
collect2: error: ld devolvió el estado de salida 1
make[2]: *** [CMakeFiles/project.dir/build.make:329: project] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/project.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

这是 CMakeList.txt:

cmake_minimum_required(VERSION 3.7)
project(project)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp [..])

SET(ALLEGRO_ROOT allegro/)

INCLUDE_DIRECTORIES( ${ALLEGRO_ROOT}/include)
LINK_DIRECTORIES( /${ALLEGRO_ROOT}/lib )

add_executable(project ${SOURCE_FILES})

TARGET_INCLUDE_DIRECTORIES(project PUBLIC ${ALLEGRO_ROOT})
TARGET_LINK_LIBRARIES(project allegro)

这是项目结构:

我做错了什么?

正如您在错误中看到的那样,main.cpp:80 中对 'al_draw_line' 的引用未定义。这是因为 Allegro 有太多的插件,如果你使用它们来让它工作,你还必须 link。

要解决它,替换

TARGET_LINK_LIBRARIES(project allegro)

来自

TARGET_LINK_LIBRARIES(project
                        allegro_acodec
                        allegro_audio
                        allegro_color
                        allegro_dialog
                        allegro_image
                        allegro_main
                        allegro_memfile
                        allegro_physfs
                        allegro_primitives
                        allegro_ttf
                        allegro_font
                        allegro)

或者只列出您需要的任何插件。在这种情况下,例如,要使用 'al_draw_line',您只需要 'allegro_primitives' 即可。