链接到尚未使用CMake构建的库

链接到尚未使用CMake构建的库

本文介绍了链接到尚未使用CMake构建的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个项目,其最终输出是一个静态库,我的基于CMake的构建系统包括两个子目录 - Src和测试 - 其中测试的生成一个可执行文件并链接到库它是从src构建的。



我的问题是测试构建需要库已经存在,如果它是没有任何错误继续。有没有办法让CMake理解库将在构建测试时存在,或者我必须在单独的步骤中这样做?



我的CMakeLists .txt文件如下:



根文件:

  cmake_minimum_required (VERSION 2.8)
project(mylib)
add_subdirectory(Src)
add_subdirectory(测试)

Src档案:

 档案(GLOB MYLIB_SOURCES * .cpp)
add_library(mylib $ {MYLIB_SOURCES})

测试档案:

 文件(GLOB MYLIB_TESTS * .cpp)
add_executable(tests $ {MYLIB_TESTS})

find_package(GTest REQUIRED)
find_library(LIB_MYLIB NAMES mylib PATHS$ {CMAKE_SOURCE_DIR} / Build / Src)

include_directories(../Src)
include_directories($ {GTEST_INCLUDE_DIRECTORIES})

target_link_libraries测试$ {LIB_MYLIB} $ {GTEST_LIBRARIES} pthread)


解决方案

CMake应该能够自动找出Src和测试之间的依赖关系,当然你只能在你的根CMakeLists.txt上调用CMake。你不需要一个find_library。



所以,我会保持你的Src CMakeLists.txt如下:为增加'封装'你可以。设置'MyLib_INCLUDE_DIRS'并强制它进入缓存:

 项目(MyLib)
文件(GLOB MYLIB_SOURCES *。 cpp)
add_library(mylib $ {MYLIB_SOURCES})
#我不知道
set(mylib_INCLUDE_DIRS $ {MyLib_SOURCE_DIR} CACHE STRINGInclude-directories for MyLibFORCE)



并重写您的测试CMakeLists.txt:

  project(MyTests)
文件(GLOB MYLIB_TESTS * .cpp)
add_executable(tests $ {MYLIB_TESTS})

find_package(GTest REQUIRED)
include_directories($ {mylib_INCLUDE_DIRS})
include_directories($ {GTEST_INCLUDE_DIRECTORIES})

target_link_libraries(测试mylib $ {GTEST_LIBRARIES} pthread)
pre>

如果您只想构建测试,我建议您在根CMakeLists.txt上调用CMake,然后进入测试目录并调用'make' 'msbuild'。


I'm building a project whose final output is a static library, and my CMake-based build system consists of two subdirectories - the Src and the Tests - where the build for the tests produces an executable and links the to the library which is built from src.

My problem is that the test build requires the library to already exist if it is to proceed without any errors. Is there a way to get CMake to understand that the library will exists when it comes to build the tests, or do I have to do these in separate steps?

My CMakeLists.txt files as as follows:

Root file:

cmake_minimum_required( VERSION 2.8 )
project( mylib )
add_subdirectory( Src )
add_subdirectory( Tests )

Src file:

file( GLOB MYLIB_SOURCES *.cpp )
add_library( mylib ${MYLIB_SOURCES} )

Test file:

file( GLOB MYLIB_TESTS *.cpp )
add_executable( tests ${MYLIB_TESTS} )

find_package( GTest REQUIRED )
find_library( LIB_MYLIB NAMES mylib PATHS "${CMAKE_SOURCE_DIR}/Build/Src" )

include_directories( ../Src )
include_directories( ${GTEST_INCLUDE_DIRECTORIES} )

target_link_libraries( tests ${LIB_MYLIB} ${GTEST_LIBRARIES} pthread )
解决方案

CMake should be able to figure out the dependency between Src and Tests automatically, provided of course you call CMake only on your root CMakeLists.txt. You don't really need a find_library.

So, I would keep your Src CMakeLists.txt as follows: For increased 'encapsulation' you could e.g. set 'MyLib_INCLUDE_DIRS' there and force it into the cache:

project( MyLib )
file( GLOB MYLIB_SOURCES *.cpp )
add_library( mylib ${MYLIB_SOURCES} )
# I do not know
set( mylib_INCLUDE_DIRS ${MyLib_SOURCE_DIR} CACHE STRING "Include-directories for MyLib" FORCE )

and rewrite your Tests CMakeLists.txt:

project( MyTests )
file( GLOB MYLIB_TESTS *.cpp )
add_executable( tests ${MYLIB_TESTS} )

find_package( GTest REQUIRED )
include_directories( ${mylib_INCLUDE_DIRS} )
include_directories( ${GTEST_INCLUDE_DIRECTORIES} )

target_link_libraries( tests mylib ${GTEST_LIBRARIES} pthread )

If you wish to build "tests" only, I suggest you call CMake on the root CMakeLists.txt and then step into the tests directory and call 'make' or 'msbuild'.

这篇关于链接到尚未使用CMake构建的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 14:07