Library building with cmake

I apologize for bothering you all, but I have a slight problem compiling with cmake.

I have a CMakeLists.txt file that I use to create a test executable and a shared library. They both have a dependency on another library (SFML).

I am using cmake in a window with MinGW.

I know that the name lib I create is confused with sfml, but it must be an SFML wrapper, so I could not find a better name!

Here is CMakeLists.txt

cmake_minimum_required(VERSION 2.6) project(projectName) set(EXECUTABLE_NAME testSFML) set(LIBRARY_NAME SFMLwindow) set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin/) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include / ${CMAKE_CURRENT_SOURCE_DIR}/../../include ) link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../lib/) file( GLOB_RECURSE SRC_FILES src/* ) file( GLOB_RECURSE INCLUDE_FILES include/* ) add_executable( ${EXECUTABLE_NAME} main.cpp ${SRC_FILES} ${INCLUDE_FILES} ) target_link_libraries( ${EXECUTABLE_NAME} sfml-main sfml-system sfml-window ) add_library( ${LIBRARY_NAME} SHARED ${SRC_FILES} ) 

And what I get in the terminal:

 "C:\MinGW\bin\mingw32-make.exe" -- Configuring done -- Generating done -- Build files have been written to: C:/Users/iksemel/docs/WorkBench/programming/projets/TestSFML/cmake Linking CXX shared library libSFMLwindow.dll Creating library file: libSFMLwindow.dll.a CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x59):undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj' CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0xda): undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKSsjRKNS_15ContextSettingsE' CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x163): undefined reference to `_imp___ZN2sf6Window5closeEv' CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1bd): undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE' CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1d8): undefined reference to `_imp___ZN2sf6Window7displayEv' collect2: ld a retourné 1 code d'état d'exécution mingw32-make.exe[2]: *** [libSFMLwindow.dll] Error 1 mingw32-make.exe[1]: *** [CMakeFiles/SFMLwindow.dir/all] Error 2 mingw32-make.exe: *** [all] Error 2 

If someone tells me what is going on, I will be very grateful!

+7
source share
1 answer

Assuming your SFMLwindow library should be linked to some or all of sfml-main, sfml-system, sfml-window.

You can try changing the end of your CMakeLists.txt to:

 add_library( ${LIBRARY_NAME} SHARED ${SRC_FILES} ${INCLUDE_FILES} ) add_executable( ${EXECUTABLE_NAME} main.cpp ) target_link_libraries( ${LIBRARY_NAME} sfml-main sfml-system sfml-window ) target_link_libraries( ${EXECUTABLE_NAME} ${LIBRARY_NAME} ) 


In general, file(GLOB_RECURSE... is generally underestimated as a way to compile a list of sources. From the documents for file :

We do not recommend using GLOB to collect a list of source files from the source tree. If the CMakeLists.txt file does not change when adding or removing the source, the generated build system cannot know when to request regeneration of CMake.


In addition, find_library should give preference to link_directories in this case. From the docs for link_directories :

Please note that this command is rarely needed. The library objects returned by find_package () and find_library () are absolute paths. Pass these paths of the absolute library file directly to the target_link_libraries () command. CMake ensures that the linker finds them.

+14
source

All Articles