Lifka Lifka - 1 month ago 9
C++ Question

Problems referencing Allegro5 addons

I'm trying use Allegro5 in CLion but I can't references addons althought it can find header and lib files.

I have the library in "/usr/lib" and headers files in "/usr/include", besides, I have the library and headers in the proyect directory.

I can use and compile this:

[..]
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);


But I can't compile this (in spite of detect header and the funcion):

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


This is the error:

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


It's the 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)


This is the project structure:
Project structure

What am I doing wrong?

Answer Source

As you can see in your error, the reference to 'al_draw_line' in your main.cpp:80 is undefined. This is because Allegro has so many addons that you also have to link if you are using them to make it work.

To solve it, replace

TARGET_LINK_LIBRARIES(project allegro)

by

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)

Or just make the list with any addon you need. In this case, for example, for using 'al_draw_line' you only need 'allegro_primitives' to make it works.