So I did a game on Visual Studio long ago. 2 years ago I uploaded the sources to github NEGU93/ForbiddenDesert just to have it there. Now I changed from windows to linux and I wanted to go back and compile this game for linux.
I remember I used allegro for the GUI so I installed it following this steps Installing Allegro 5. When I coded the game I remember downloading an allegro prepared for Visual Studio directly so I didn't have any problems.
I created a CMakeLists.txt (never did that before, so I am quite new in that area):
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 2.8)
# projects name
project(ForbiddenDesert)
set(CMAKE_CXX_STANDARD 11) # enable C++11 standard
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
# Finds Allegro using pkgconfig, so it must be configured correctly
find_package(Allegro5 REQUIRED)
# Set include and lib dirs.
include_directories(${ALLEGRO_INCLUDE_DIR})
set(FD_LIBS ${LIBS} ${OBJC_LIBRARIES} ${ALLEGRO_LIBRARIES})
# The following folder will be included
# include_directories(${PROJECT_SOURCE_DIR}/src)
# include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories("${PROJECT_SOURCE_DIR}")
file(GLOB all_SRCS
"*.h"
"*.cpp"
)
add_executable(forbidden ${all_SRCS})
target_link_libraries(forbidden ${FD_LIBS})
I am using a file named FindAllegro5.cmake
inside a folder named cmake/
. The file is a copy of eruta/FindAllegro5.cmake.
When running cmake .
I get the following:
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'allegro-5'
-- Found allegro-5, version 5.2.4
-- Found Allegro5: /usr/lib/liballegro.so;/usr/lib/liballegro_image.so;/usr/lib/liballegro_font.so;/usr/lib/liballegro_primitives.so;/usr/lib/liballegro_ttf.so;/usr/lib/liballegro_audio.so;/usr/lib/liballegro_dialog.so;/usr/lib/liballegro_memfile.so;/usr/lib/liballegro_acodec.so;/usr/lib/liballegro_color.so;/usr/lib/liballegro_main.so;/usr/lib/liballegro_physfs.so
So far so good. Yet, when I run make
In file included from /home/ubuntu/Documents/GitHub/ForbiddenDesert/Button.h:5:0,
from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.h:4,
from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.cpp:1:
/home/ubuntu/Documents/GitHub/ForbiddenDesert/allegro.h:5:10: fatal error: allegro5\allegro.h: No such file or directory
#include <allegro5\allegro.h>
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/forbidden.dir/build.make:62: recipe for target 'CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o' failed
make[2]: *** [CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/forbidden.dir/all' failed
make[1]: *** [CMakeFiles/forbidden.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I've seen that the FindAllegro5.cmake
has:
# Include dir
find_path(Allegro5_INCLUDE_DIR
NAMES allegro5/allegro5.h
PATHS ${Allegro5_PKGCONF_INCLUDE_DIRS}
)
So I changed the include to #include <allegro5/allegro5.h>
but still no solution.
I haven't been able to find the solution (I've found plenty of information about the "fatal error: allegro5\allegro.h: No such file or directory" but haven't found one that applies to my case).
So I installed allegro with the following: Quickstart Allegro and then I created the hello.c
file and compiled it as it says there (gcc hello.c -o hello $(pkg-config allegro-5 allegro_font-5 --libs --cflags)
) and it worked. So the problem will be how to add those flags to the cmakelists.txt
So I tried:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $(pkg-config allegro-5 allegro_font-5 allegro_primitives-5 allegro_ttf-5 allegro_image-5 allegro_audio-5 allegro_acodec-5 --libs --cflags)")
But still didn't work.
Running find /usr/ -name "allegro*"
I found the header allegro5.h
on /usr/include/allegro5/allegro5.h
and some other files on /usr/lib/pkgconfig/
.
So I did:
INCLUDE_DIRECTORIES( /usr/include/allegro5 )
LINK_DIRECTORIES( /usr/lib/pkgconfig )
TARGET_LINK_LIBRARIES(game liballegro.a )
Still not working.
I tried adding the libraries directly from the build I compiled from the source code on github (following the info I found here Allegro and CMake) and still didn't work:
#Include Allegro
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/include)
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib/Headers)
link_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib)
#connect all the libraries you need
set(game_LIBS liballegro.so liballegro_dialog.so liballegro_image.so)
target_link_libraries(game ${game_LIBS})
Under Ubuntu I would just install allegro with apt-get
:
apt-get install liballegro5-dev
Then the includes would be at the right place (/usr/include/allegro5/...
)
You say you installed allegro, but I'm not too sure whether it would be the same as from a package. If it was from a build, it is often that the default installation directory is /usr/local
instead of /usr
. So the includes would be in /usr/local/include/allegro5/...
That being said, your include()
command in cmake is correct:
include_directories(${ALLEGRO_INCLUDE_DIR})
However, your libraries handling is completely wrong. You never have to change LD_FLAGS
. (At least, in the last 6 or 7 years I've used cmake, I never had a need.) Instead, you want to use the target_link_libraries()
. Something like this:
target_link_libraries(${PROJECT_NAME} ${ALLEGRO_LIBRARIES})
It will work within (as in after) a project(...)
.
If you want to verify that the variables are correctly named, you may use the message()
command. Some libraries use names such as NAME_LIBRARY
or NAME_INCLUDE_PATH
. They should follow the proper naming convention, but nothing forces them to, so you often find some funkiness there.
The following will print a message out:
message("allegro libraries = " ${ALLEGRO_LIBRARIES})
If the variable name is not correct (or the library was not found), you will see nothing. If you got it right, it will show you the variable libraries. I would imagine that is right, but the LD_FLAGS
was probably what you got wrong.