简体   繁体   English

CMake FetchContent 不复制库

[英]CMake FetchContent does not copy libraries

I am using CMake FetchContent to download and build a third party library(realsense2 in this case).我正在使用 CMake FetchContent 下载和构建第三方库(在本例中为 realsense2)。 After trying out the googletest example from the official documentation ( https://cmake.org/cmake/help/v3.11/module/FetchContent.html ) I was impressed how easy it works.在尝试了官方文档 ( https://cmake.org/cmake/help/v3.11/module/FetchContent.html ) 中的 googletest 示例后,它的工作方式给我留下了深刻的印象。 Including headers was done magically.包括标题是神奇地完成的。 Now with realsense2 SDK I have a problem.现在使用 realsense2 SDK 我遇到了问题。

I need to do add an additional include_directories command like this:我需要添加一个额外的 include_directories 命令,如下所示:

FetchContent_Declare(
realsense2
GIT_REPOSITORY https://github.com/IntelRealSense/librealsense.git
GIT_TAG        v2.23.0
)
FetchContent_MakeAvailable(realsense2)
FetchContent_GetProperties(realsense2)
if(NOT realsense2_POPULATED)
    FetchContent_Populate(realsense2)
    add_subdirectory(${realsense2_SOURCE_DIR} ${realsense2_BINARY_DIR})
endif()
//I should not be required to do this according to documentation
include_directories(${realsense2_SOURCE_DIR}/include)

If I do not do this, some headers are not found.如果我不这样做,则找不到某些标题。 Any suggestions regarding this problem?关于这个问题有什么建议吗?

EDIT: To clarify, this is how I added the libraries:编辑:为了澄清,这就是我添加库的方式:

target_link_libraries(TestExe gtest gtest_main)

and the other exactly the same but this time it is not an exe its a dll另一个完全一样,但这次不是exe而是dll

add_library(TestLib SHARED ${TestLib_HEADERS} ${TestLib_SOURCES} )
target_link_libraries(TestLib realsense2)

At this point I am more concerned about why I do not have to add any includes for googletest framework在这一点上我更关心为什么我不必为 googletest 框架添加任何包含

The main purpose of FetchContent is a garantee that at the time of call FetchContent的主要目的是保证在调用时

add_subdirectory(${Foo_SOURCE_DIR} ${Foo_BINARY_DIR})

the "fetched" project will be (as sources) in the ${Foo_SOURCE_DIR} directory. “获取的”项目将(作为源)在${Foo_SOURCE_DIR}目录中。

How to use the project inluded via add_subdirectory is completely up to that project :如何使用通过add_subdirectory包含的项目完全取决于该项目

  1. Some projects (including gtest) create the library target Foo in a "modern" CMake way, by associating properties with it using target_include_directories and other commands.一些项目(包括 gtest)通过使用target_include_directories和其他命令将属性与其关联,以“现代”CMake 方式创建库目标Foo So, to use a library like this, it is sufficient to call target_link_libraries .因此,要使用这样的库,调用target_link_libraries就足够了。

  2. Some other projects require both include_directories and target_link_libraries to work with them.其他一些项目需要include_directoriestarget_link_libraries才能使用它们。

  3. Finally, there are many projects, which simply don't work when included via add_subdirectory .最后,有许多项目,当通过add_subdirectory包含时根本不起作用。 So FetchContent makes little sense for them.所以FetchContent对他们来说意义不大。

Only a small subset of projects describe how to work with them via add_subdirectory approach.只有一小部分项目描述了如何通过add_subdirectory方法使用它们。 And gtest is among them. gtest就是其中之一。

But most projects simply don't describe this;但大多数项目根本没有描述这一点; if you want to use add_subdirectory with such a project, then you need to investigate the internals of that project to understand its usage (or use trial and error).如果您想在这样的项目中使用add_subdirectory ,那么您需要调查该项目的内部结构以了解其用法(或使用反复试验)。

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

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