CMake - 在同一项目中的应用程序/库之间的依赖关系(头) [英] CMake - dependencies (headers) between apps/libraries in same project

查看:228
本文介绍了CMake - 在同一项目中的应用程序/库之间的依赖关系(头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下项目结构:




  • CMakeLists.txt

    • lib1 /CMakeLists.txt以及lib

    • lib2 / CMakeLists.txt的所有cpp和头文件,以及lib

    • 应用程序的所有cpp和头文件/CMakeLists.txt和应用程序的所有cpp和头文件




CMakeLists.txt如下:

  PROJECT($ {PROJECT_NAME})
add_subdirectory(lib1)
add_subdirectory (lib2)
add_subdirectory(app)

lib1 / CMakeLists.txt看起来像已删除):

  SET(SOURCE 
file.cpp

SET(HEADERS
some_lib_header.h

add_library(lib1 $ {SOURCE} $ {HEADERS})


b $ b

,除了ADD_EXECUTABLE外,应用程式的外观也相同:

  SET(SOURCE 
main.cpp

SET(HEADERS
some_header.h

add_library(lib1 $ {SOURCE} $ {HEADERS})
ADD_EXECUTABLE $ {SOURCE} $ {HEADERS})

我发现安装程序运行良好, ,我可以生成一个Visual Studio解决方案文件,其中包含所有这三个项目。但我的问题是,我的应用程序包括头文件lib1(和lib2,这取决于lib1)。当我做

  $ mkdir build 
$ cd build
$ cmake -C .. \myproject

它会根据我的需要生成源代码VS.sln文件,因为它找不到lib1的头文件(显然)。



现在我读了很多东西,比如 TARGET_LINK_LIBRARIES lib1)(它让应用程序链接到lib1,但不解决头包括问题),和 add_subdirectory(../lib1)在应用程序的CMakeLists.txt(其中所有抛出的错误,我不能修复)的各种变体,以及find_package(我猜是错误的方法)。



那么我该如何解决这个问题(我猜简单...)?

解决方案

/ p>

Root CMakeLists.txt:

  cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project($ {PROJECT_NAME})
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(app)



lib1 / CMakeLists.txt:

  project(Lib1)
add_library(lib1 lib1.cpp lib1.h)



lib2 / CMakeLists.txt:

  project(Lib2)
add_library(lib2 lib2.cpp lib2.h)

#添加/ lib1到#include搜索路径
include_directories($ {Lib1_SOURCE_DIR})
#指定lib2对lib1的依赖
target_link_libraries(lib2 lib1 )



app / CMakeLists.txt:

  project(App)
add_executable(app main.cpp some_header.h)

#添加/ lib1和/ lib2到#include搜索路径
include_directories($ {Lib1_SOURCE_DIR} $ {Lib2_SOURCE_DIR})
#指定应用程序对lib2的依赖。
#lib2对lib1的依赖性被自动添加。
target_link_libraries(app lib2)

不同的方式来实现相同的最终结果。对于相对较小的项目,我可能只使用一个CMakeLists.txt:

  cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Test)

add_library(lib1 lib1 / lib1.cpp lib1 / lib1.h)
add_library(lib2 lib2 / lib2.cpp lib2 / lib2.h)
add_executable(app app / main.cpp app / some_header.h)

include_directories($ {CMAKE_SOURCE_DIR} / lib1 $ {CMAKE_SOURCE_DIR} / lib2)

target_link_libraries(lib2 lib1)
target_link_libraries(app lib2)



相关命令及其原理,运行:

  cmake --help命令add_subdirectory 
cmake --help-command include_directories
cmake --help-command target_link_libraries


I have the following project structure:

  • CMakeLists.txt
    • lib1/CMakeLists.txt and all cpp and header files of the lib
    • lib2/CMakeLists.txt and all cpp and header files of the lib
    • app/CMakeLists.txt and all cpp and header files of the app

The main CMakeLists.txt looks like:

PROJECT( ${PROJECT_NAME} )
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(app)

The lib1/CMakeLists.txt looks eg like (stripped):

SET(SOURCE
file.cpp
)
SET(HEADERS
    some_lib_header.h
)
add_library( lib1 ${SOURCE} ${HEADERS} )

and the one for the app looks the same except of ADD_EXECUTABLE:

SET(SOURCE
main.cpp
)
SET(HEADERS
    some_header.h
)
add_library( lib1 ${SOURCE} ${HEADERS} )
ADD_EXECUTABLE( app ${SOURCE} ${HEADERS} )

I found the setup working well this way because out of this, I can generate one Visual Studio solution file which contains all those three projects. But my problem is that my app includes header files of lib1 (and also of lib2, which depends on lib1). When I do

$mkdir build
$cd build
$cmake -C ..\myproject

it generates out-of-source VS .sln file as I want it, but the app doesn't compile because it can't find the header files of lib1 (obviously).

Now I read and tried many things, like TARGET_LINK_LIBRARIES( app lib1 ) (which got the app to link with the lib1, but not solve the header include issue), and things like add_subdirectory( ../lib1 ) in various variants in the CMakeLists.txt of app (which all throwed errors that I couldn't fix), and also find_package (which I guess is the wrong approach).

So how can I solve this (I guess simple...) problem?

解决方案

Here's one possible solution:

Root CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(${PROJECT_NAME})
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(app)


lib1/CMakeLists.txt:

project(Lib1)
add_library(lib1 lib1.cpp lib1.h)


lib2/CMakeLists.txt:

project(Lib2)
add_library(lib2 lib2.cpp lib2.h)

# Add /lib1 to #include search path
include_directories(${Lib1_SOURCE_DIR})
# Specify lib2's dependency on lib1
target_link_libraries(lib2 lib1)


app/CMakeLists.txt:

project(App)
add_executable(app main.cpp some_header.h)

# Add /lib1 and /lib2 to #include search path
include_directories(${Lib1_SOURCE_DIR} ${Lib2_SOURCE_DIR})
# Specify app's dependency on lib2.
# lib2's dependency on lib1 is automatically added.
target_link_libraries(app lib2)


There are plenty of different ways to achieve the same end result here. For a relatively small project, I'd probably just use a single CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Test)

add_library(lib1 lib1/lib1.cpp lib1/lib1.h)
add_library(lib2 lib2/lib2.cpp lib2/lib2.h)
add_executable(app app/main.cpp app/some_header.h)

include_directories(${CMAKE_SOURCE_DIR}/lib1 ${CMAKE_SOURCE_DIR}/lib2)

target_link_libraries(lib2 lib1)
target_link_libraries(app lib2)


For further info on the relevant commands and their rationale, run:

cmake --help-command add_subdirectory
cmake --help-command include_directories
cmake --help-command target_link_libraries

这篇关于CMake - 在同一项目中的应用程序/库之间的依赖关系(头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆