这是我的基本文件结构:
├── BUILD
│ └── lib
│ └── *.a files
│ └── include
│ └── library
├── test.cpp
└── CmakeLists.txt
我正在尝试将include和lib文件夹文件链接到test.cpp
。这是我当前的CmakeLists.txt文件:
cmake_minimum_required(VERSION 3.10)
project(test)
set(CMAKE_CXX_STANDARD 14)
add_executable(test test.cpp)
target_include_directories(test PUBLIC ${CMAKE_SOURCE_DIR}/BUILD/include)
target_link_libraries(test not-sure-what-to-put-here)
我知道我的target_include_directories
行正在工作,但无论我为target_link_libraries
输入什么,它似乎总是失败并给出错误:
undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
我希望得到帮助,解决我的CMakeLists.txt
文件有什么问题,谢谢!
编辑1,这是用于静态链接库
您用sfml
标记了您的问题,因此您得到了关于此库的评论和答案。然而,您的问题似乎只是关于您想要链接到可执行文件的一些静态库。这应该有效:
cmake_minimum_required(VERSION 3.10)
project(test)
set(CMAKE_CXX_STANDARD 14)
add_executable(test test.cpp)
target_include_directories(test PUBLIC ${CMAKE_SOURCE_DIR}/BUILD/include)
target_link_libraries(test ${CMAKE_SOURCE_DIR}/BUILD/lib/libA.a ${CMAKE_SOURCE_DIR}/BUILD/lib/libB.a)
所以,是的,您需要在target_link_libraries
命令中列出所有库。
如果您正在使用动态/共享SFML,请使用此。
cmake_minimum_required(VERSION 3.10)
project(test)
set(CMAKE_CXX_STANDARD 14)
target_include_directories(test PUBLIC ${CMAKE_SOURCE_DIR}/BUILD/include)
add_library(sfml-graphics shared IMPORTED)
set_property(TARGET sfml-graphics PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILDv/lib/libsfml-graphics.so)
add_library(sfml-window shared IMPORTED)
set_property(TARGET sfml-window PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-window.so)
add_library(sfml-audio shared IMPORTED)
set_property(TARGET sfml-audio PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-audio.so)
add_library(sfml-network shared IMPORTED)
set_property(TARGET sfml-network PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-network.so)
add_library(sfml-system shared IMPORTED)
set_property(TARGET sfml-system PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-system.so)
add_executable(test test.cpp)
target_link_libraries(test sfml-graphics sfml-window sfml-audio sfml-network sfml-system)
如果您使用的是sfml的静态版本,请使用此。
cmake_minimum_required(VERSION 3.10)
project(test)
set(CMAKE_CXX_STANDARD 14)
target_include_directories(test PUBLIC ${CMAKE_SOURCE_DIR}/BUILD/include)
add_library(sfml-graphics-s shared IMPORTED)
set_property(TARGET sfml-graphics-s PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILDv/lib/libsfml-graphics-s.a)
add_library(sfml-window-s shared IMPORTED)
set_property(TARGET sfml-window-s PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-window-s.a)
add_library(sfml-audio-s shared IMPORTED)
set_property(TARGET sfml-audio-s PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-audio-s.a)
add_library(sfml-network-s shared IMPORTED)
set_property(TARGET sfml-network-s PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-network-s.a)
add_library(sfml-system-s shared IMPORTED)
set_property(TARGET sfml-system-s PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/BUILD/lib/libsfml-system-s.a)
add_executable(test test.cpp)
target_link_libraries(test sfml-graphics-s sfml-window-s sfml-audio-s sfml-network-s sfml-system-s freetype X11 Xrandr pthread GL GLU udev FLAC ogg vorbis vorbisenc vorbisfile openal)
这应该可以明确地工作