C++ 如何将第三方库链接到cmake中的静态库目标?
我想做一个这样的项目:C++ 如何将第三方库链接到cmake中的静态库目标?,c++,visual-studio,cmake,linker,C++,Visual Studio,Cmake,Linker,我想做一个这样的项目: Foo/ |- libFoo/ |- CMakeLists.txt |- foo.h |- foo.cpp |- libFooTests/ |- CMakeLists.txt |- test.cpp |- CMakeLists.txt # Foo/CMakeLists.txt CMAKE_MINIMUM_REQUIRED(VER
Foo/
|- libFoo/
|- CMakeLists.txt
|- foo.h
|- foo.cpp
|- libFooTests/
|- CMakeLists.txt
|- test.cpp
|- CMakeLists.txt
# Foo/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(Foo)
# I'm on windows, so make sure that all projects are using same runtime library
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd"
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT"
ADD_SUBDIRECTORY(libFoo)
ADD_SUBDIRECTORY(libFooTests)
# Foo/libFoo/CMakeLists.txt
ADD_LIBRARY(libFoo foo.h foo.cpp)
LINK_DIRECTORIES(${THIRD_PARTY_LIB_DIR})
TARGET_LINK_LIBRARIES(libFoo lib3rdparty)
# Foo/libFooTests/CMakeLists.txt
ADD_EXECUTABLE(libFooTests test.cpp)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/libFoo")
TARGET_LINK_LIBRARIES(libFooTests libFoo)
如果在此设置上运行cmake,则可以成功生成项目。但是它无法构建,因为生成的项目被配置为将lib3rdparty
链接到测试目标libFooTests
,而不是libFoo
,当然libFooTests
不知道(也不应该知道)在哪里可以找到lib3rdparty
我在windows(使用generator“Visual Studio 12 2013 Win64”)和Ubuntu(使用generator“Unix Makefiles”)上都试过了
我所期望的是:
${THIRD_PARTY_LIB_DIR}
应配置为指向libFoo
目标的库搜索路径,lib3rdparty
应配置为libFoo
libFooTests
只知道在哪里包括libFoo
(又名${PROJECT\u SOURCE\u DIR}/libFoo
)的标题和链接libFoo
。它不应该知道任何关于lib3rdparty
发生了什么事
${THIRD\u PARTY\u LIB\u DIR}
似乎被忽略了。
lib3rdparty
是用于libTests
我注意到:
我注意到visual studio中的libFoo.vcxproj
和libFooTests.vcxproj
的Properties页
对话框略有不同
前者有一个configurationproperties | library
条目,而后者则有一个configurationproperties | Linker
条目
我如何才能仅将lib3rdparty
链接到libFoo
静态库不会被链接。静态库只是一组打包成一个文件的对象文件
链接发生在您将它们与完整程序链接时。因此,如果静态库依赖于另一个库,则必须将该库链接到使用静态库的所有程序中。CMake通过自动执行此操作似乎在做正确的事情。我明白了。有什么方法可以实现类似的目标吗?使
libFoo
共享等。鉴于“静态lib不会被链接”,这里最合适的方法可能是让libfootest
知道从libFoo
中“继承”这些lib的位置。将LINK_目录
放入libFooTests/CMakeLists.txt将起作用,但这样做真的很麻烦。如何使LINK_目录
也“继承”到libFooTests
?