Create a CMakeFiles.txt file for newbies - c++

I have a project with the following structure
/cmake_modules/
FindSFML.cmake
/includes/
car.hpp
motor.hpp
tires.hpp
/sources/
car.cpp
motor.cpp
tires.cpp
/main.cpp
/main.hpp
I have the following CMakeFiles.txt file:
cmake_minimum_required(VERSION 2.8)
project (MYGAME)
set (MYGAME_VERSION_MAJOR 1)
set (MYGAME_VERSION_MINOR 0)
set (EXECUTABLE_NAME "mygame")
include_directories ("${MYGAME_BINARY_DIR}")
include_directories ("${MYGAME_BINARY_DIR}/includes")
link_directories ("${MYGAME_BINARY_DIR}/sources")
add_executable(${EXECUTABLE_NAME} main.cpp)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.0 REQUIRED system window graphics network audio)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
When I try to execute make I get this:
[100%] Building CXX object CMakeFiles/mygame.dir/main.cpp.o
Linking CXX executable mygame
CMakeFiles/mygame.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `mynamespace::Car::Instance()'
main.cpp:(.text+0x21): undefined reference to `mynamespace::Car::start()'
collect2: error: ld returned 1 exit status
make[2]: *** [mygame] Error 1
make[1]: *** [CMakeFiles/mygame.dir/all] Error 2
make: *** [all] Error 2
How do I fix it?

You need to include the rest of your sources (car.cpp, motor.cpp and tires.cpp) in the build in some way.
You can either add them along with main.cpp in the executable directly:
set(MySources sources/car.cpp sources/motor.cpp sources/tires.cpp main.cpp)
add_executable(${EXECUTABLE_NAME} ${MySources})
or you can make these into a library and link that:
set(MyLibSources sources/car.cpp sources/motor.cpp sources/tires.cpp)
add_library(MyLib ${MyLibSources})
add_executable(${EXECUTABLE_NAME} main.cpp)
...
target_link_libraries(${EXECUTABLE_NAME} MyLib ${SFML_LIBRARIES})
A couple of other points to note:
You should avoid the use of link_directories if possible (its own documentation discourages its use), and it's often helpful to include the headers in the list of files added via add_executable or add_library since these then show up in IDEs like MS Visual Studio.

Related

Loading a PNG using SDL2 + SDL2Image using C++ and cmake on Ubuntu

I'm working on a simple tutorial from here: https://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php
My initial program did not do any image loading. It just showed a screen and went away. Everything worked fine. Here is my initial CMakeLists.txt file:
cmake_minimum_required(VERSION 3.7)
project(01_hello_world)
set(CXX_STANDARD 17)
find_package(SDL2 REQUIRED)
add_executable(hello 01_hello_SDL.cpp)
target_include_directories(hello PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(hello ${SDL2_LIBRARIES})
Everything worked and compiled just fine. However, I then wanted to load a PNG image, which I thought would be a very easy. Googling led me to the SDL2 Image library, and the IMG_Load method. So I went ahead and installed libsdl2-image-dev, and my CMakeLists.txt file grew by two more lines::
set(SDL2IMAGE_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
However, just by using the IMG_Load method, the make command threw a whole bunch of library requirements: libtiff-dev, libpng-dev, libjpeg-dev, libwebp-dev. All this just to load a png file! So I went ahead and installed all those, and now my CMakeLists.txt file looks like this abomination (I used find_package where I could, and manually set variables where I couldn't):
cmake_minimum_required(VERSION 3.7)
project(01_hello_world)
set(CXX_STANDARD 17)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(SDL2 REQUIRED)
find_package(PNG REQUIRED)
find_package(JPEG REQUIRED)
find_package(TIFF REQUIRED)
set(SDL2IMAGE_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
set(WEBP_LIBRARIES "/usr/lib/x86_64-linux-gnu/libwebp.a")
add_executable(hello 01_hello_SDL.cpp)
target_include_directories(hello PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS})
target_link_libraries(hello ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES} ${PNG_LIBRARY} ${JPEG_LIBRARY} ${WEBP_LIBRARIES} ${TIFF_LIBRARIES} Threads::Threads)
At this point, when I make, I get this error:
[ 50%] Linking CXX executable hello
/usr/bin/ld: cannot find -l{TIFF_LIBRARIES}
collect2: error: ld returned 1 exit status
CMakeFiles/hello.dir/build.make:98: recipe for target 'hello' failed
make[2]: *** [hello] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/hello.dir/all' failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I've been googling for hours. Can someone provide a light in this darkness?
Also is this the correct approach to working with sdl + cmake?
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
It seems like you aren't dynamically linking SDL2_image to your program.
I don't know how to change it in CMake,but you could compile your program utilizing this pattern:
g++ your_program.cpp -o your_program -lSDL2 -lSDL2_image
You may also utilize a Makefile like this one:
OBJS = your_program.cpp
OBJ_NAME = your_program_output
all:
g++ $(OBJS) -o $(OBJ_NAME) -lSDL2 -lSDL2_image # don't forget about replacing the four spaces by a tab character
One last thing, lazy foo has a tutorial teaching how to install and set up the SDL2_image library on linux utilizing an IDE (Code::Blocks) or not (command lines and Makefiles).
http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/linux/index.php

CMake linking with dll on windows - undefined reference

I am trying to link with a DLL executable using CMake. This works fine on Linux (of course with .so), but on Windows, it seems to be tricky. I thought it might be some other dependencies, so I tried creating a simple test program, but I couldn't get that to work either.
My folder structure is simple:
main.cpp
CmakeLists.txt
lib/libtest.dll
lib/CMakeLists.txt
I based this structure on the answer to another question that was asked.
Unfortunately, when I build, it it fails.
The content of the lib/CMakeLists.txt are:
message("-- Linking Test")
add_library(TEST libtest.dll)
set_target_properties(TEST PROPERTIES LINKER_LANGUAGE C)
And the content of the main CMakeLists.txt are:
cmake_minimum_required(VERSION 3.15)
project(Test_program)
set(CMAKE_CXX_STANDARD 14)
set(GCC_COVERAGE_COMPILE_FLAGS "-DDebug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
# Contain the header files for testlib
include_directories("C:/test/utils/")
add_subdirectory(lib)
add_executable(Test_program main.cpp)
target_link_libraries(Test_program TEST)
This results in the following output:
-- Linking Test
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/akda/Test_program/cmake-build-debug
[ 33%] Linking C static library libTEST.a
[ 33%] Built target TEST
[ 66%] Building CXX object CMakeFiles/Test_program.dir/main.cpp.obj
[100%] Linking CXX executable Test_program.exe
CMakeFiles\Test_program.dir/objects.a(main.cpp.obj): In function `powerOn()':
C:/Users/akda/Test_program/main.cpp:8: undefined reference to `My_Library_Function'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\Test_program.dir\build.make:87: Test_program.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:77: CMakeFiles/Test_program.dir/all] Error 2
mingw32-make.exe: *** [Makefile:83: all] Error 2
Using tools as DLLExportViewer, I can confirm that the DLL, in fact, does contain the function called.
What am I doing wrong? I'll never claim to be an expert in CMake, but I really can't see what's wrong.
Update with link to .a file
As pointed out, I could try linking with the .a file.
I've added this to the library folder, and changed the main CMakeLists.txt to the following:
cmake_minimum_required(VERSION 3.15)
project(Test_program)
set(CMAKE_CXX_STANDARD 14)
set(GCC_COVERAGE_COMPILE_FLAGS "-DDebug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
include_directories("C:/test/utils/")
find_library(TEST ${CMAKE_SOURCE_DIR}/lib/libtest.a)
add_executable(Test_program main.cpp)
target_link_libraries(Test_program ${TEST})
And this ends up in the same way, with the same errors.

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:
What am I doing wrong?
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.

cmake link xlib directories c++

I'm trying to compile a c++ program that uses xlib with cmake. However, I'm having a problem including and linking xlib libraries in cmake file.
This is the error that I'm getting.
main.cpp:378: undefined reference to `XClearWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/project1.dir/build.make:94: recipe for target 'project1' failed
make[2]: *** [project1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/project1.dir/all' failed
make[1]: *** [CMakeFiles/project1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And when I use just the command line to compile, it works just fine.
I use this command (g++ main.cpp -L/usr/X11R6/lib -lX11)
and this is my cmake file.
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
link_directories(/usr/X11R6/lib)
include_directories(/usr/share/X11)
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
In your case, you forgot to specify the libraries that cmake should use to link your application (target_link_libraries or link_libraries).
But, why not just let cmake find the required path, libraries and includes by itself? I suggest you to use find_package(X11). In your case, you can try:
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_STANDARD 11) # for c++11
find_package(X11 REQUIRED)
link_libraries(${X11_LIBRARIES})
include_directories(${X11_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
I won't be able to explain why (if you have some idea, don't hesitate to comment my answer), but, in my case, I had to link X11 library using this command:
target_link_libraries( DisplayImage "-lX11" )
It works, at least, for the 3.5.1 version of cmake.

OpenGL hello.c fails to build using CMake

I am trying to build the hello.c example from http://www.glprogramming.com/red/chapter01.html (look for "Example 1-2").
My CMakeLists.txt is as follows:
cmake_minimum_required (VERSION 2.8)
project (GLUTEX)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
include_directories(${OpenGL_INCLUDE_DIRS})
add_executable (glutex glutex.c)
target_link_libraries (glutex ${OpenGL_LIBRARIES})
target_link_libraries (glutex ${GLUT_LIBRARIES})
The CMake call succeeds in generating the required Makefile. But when I call make, I encounter the following:
Scanning dependencies of target glutex
[100%] Building C object CMakeFiles/glutex.dir/glutex.c.o
Linking C executable glutex
/usr/bin/ld: CMakeFiles/glutex.dir/glutex.c.o: undefined reference to symbol 'glOrtho'
/usr/bin/ld: note: 'glOrtho' is defined in DSO /usr/lib64/libGL.so.1 so try adding it to the linker command line
/usr/lib64/libGL.so.1: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
make[2]: *** [glutex] Error 1
make[1]: *** [CMakeFiles/glutex.dir/all] Error 2
make: *** [all] Error 2
How do I fix this?
Try changing
target_link_libraries (glutex ${OpenGL_LIBRARIES})
to
target_link_libraries (glutex ${OPENGL_LIBRARIES})