简体   繁体   English

如何链接包含 lib 的库并在 Windows 上的 cmake 中包含文件夹

[英]How to link libraries containing lib and include folders in cmake on windows

This is my basic file structure:这是我的基本文件结构:

├── BUILD
│   └── lib
│       └── *.a files
│   └── include
│       └── library
├── test.cpp
└── CmakeLists.txt

I'm trying to link boh the include and lib folder files to test.cpp .我正在尝试将包含和 lib 文件夹文件的test.cpp链接到test.cpp This is my current CmakeLists.txt file:这是我当前的 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)

I know my target_include_directories line is working however no matter what i put for target_link_libraries it seems to always fail and give the error:我知道我的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'

I was hoping to get help on what I'm doing wrong with my CMakeLists.txt file, thank you!我希望得到关于我在CMakeLists.txt文件中做错了什么的帮助,谢谢!

Edit 1, this is for statically linked libraries编辑 1,这是静态链接库

You tagged your question with the sfml tag, so you got comments and an answer about this library.你用sfml标签标记了你的问题,所以你得到了关于这个库的评论和答案。 However it looks like your question is only about some static libraries you want to link to your executable.但是,您的问题似乎只是关于您想要链接到可执行文件的一些静态库。 This should work:这应该有效:

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)

So, yes - you need to list all the libraries in the target_link_libraries command.所以,是的 - 您需要在target_link_libraries命令中列出所有库。

if you are using dynamic/shared SFML use this.如果您使用的是动态/共享 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)

If you are using a static version of sfml use this.如果您使用的是 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)

this should definetly work这应该肯定有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM