我为打扰你们而道歉,但我对cmake有一点编译问题。
我有一个CMakeLists.txt文件,用于构建测试可执行文件和共享库。它们都依赖于另一个库(SFML)。
我在MinGW的窗口上使用cmake。
我知道我正在构建的lib的名称与sfml有点混淆,但它应该是一个SFML包装器,所以,我找不到更好的名字!
这里是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}
)
我在终端得到的东西:
"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
如果有人知道发生了什么,我会非常感激!
答案 0 :(得分:14)
猜测,您的SFMLwindow
库需要链接到部分或全部sfml-main,sfml-system,sfml-window。
您可以尝试将CMakeLists.txt的结尾更改为:
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}
)
顺便说一句,file(GLOB_RECURSE...
通常不赞成收集来源列表。来自file
的文档:
我们不建议使用GLOB从源树中收集源文件列表。如果在添加或删除源时没有更改CMakeLists.txt文件,则生成的构建系统无法知道何时要求CMake重新生成。
此外,在这种情况下,find_library
应优先于link_directories
。来自link_directories
的文档:
请注意,此命令很少需要。 find_package()和find_library()返回的库位置是绝对路径。将这些绝对库文件路径直接传递给target_link_libraries()命令。 CMake将确保链接器找到它们。