CMake:将add_subproject与包含Include的库一起使用会导致错误的相对路径 [英] CMake: Using add_subproject with a library using Include ends up in wrong relative path

查看:536
本文介绍了CMake:将add_subproject与包含Include的库一起使用会导致错误的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此CMakeLists.txt使用MOC编译了一些C ++文件(Qt5/...),在任何方面都不是特殊"的.

This CMakeLists.txt compiles a few C++ files (Qt5/...) with MOC and is not 'special' in any regards.

有问题的代码是这样的:

The code in question is this:

add_subdirectory(third-party/libwebrtc)
include_directories(third-party/libwebrtc)
target_link_libraries(${PROJECT_NAME} libwebrtc)

libwebrtc CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
project(libwebrtc)

# Allow the use of IN_LIST operand
cmake_policy(SET CMP0057 NEW)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
    ${CMAKE_SOURCE_DIR}/CMakeModules)

find_package(Git REQUIRED)

include(FindLibraries)
include(Version)
include(Options)
include(TargetOsAndCpu)
...

完整的libwebrtc源-> https://github.com/cloudwebrtc/libwebrtc-build/blob/dev/CMakeLists.txt

Full libwebrtc source -> https://github.com/cloudwebrtc/libwebrtc-build/blob/dev/CMakeLists.txt

如果运行mkdir build; cd build; cmake ..,我可以看到CMake在顶级项目中搜索include(),而不是在我期望的子目录中搜索.

If I run mkdir build; cd build; cmake .. I can see that CMake searches the include() in the top-level project and not in its subdirectory as I had expected it.

joachim@thinkpad-x1:[~/projects/client/build]: cmake ..
-- CMAKE_SYSTEM_INFO_FILE: 
-- CMAKE_SYSTEM_NAME:      
-- CMAKE_SYSTEM_PROCESSOR: 
-- CMAKE_SYSTEM:           
-- CMAKE_C_COMPILER:       /usr/bin/clang
-- CMAKE_CXX_COMPILER:     /usr/bin/clang++
CMake Error at sources/third-party/libwebrtc/CMakeLists.txt:13 (include):
  include could not find load file:

    FindLibraries


CMake Error at sources/third-party/libwebrtc/CMakeLists.txt:14 (include):
  include could not find load file:

    Version

问题(原始)

除了通过从CMakeModules目录复制包含内容将库集成到我的基础项目中之外,我还能做些其他事情吗?

Question (original)

Is there something I can do about that other than integrating the library into my base-project by copying the includes from the CMakeModules directory?

感谢您在下面回答了这个问题,仍然留在这里.

Thanks this has been answered below, still leaving this here.

add_subdirectory调用正确地构建了libwebrtc,但是我的include_directories调用,即顶层CMakeLists.txt(不是libwebrtc库)中的那个,想要包含仅在完整构建之后才可用的文件:

The add_subdirectory call builds the libwebrtc correctly, but my include_directories call, the one from the top level CMakeLists.txt (not the libwebrtc library), wants to include files which are only available after the complete build:

include_directories(
  ${CMAKE_CURRENT_BINARY_DIR}/sources/third-party/libwebrtc/include/webrtc
  ${CMAKE_CURRENT_BINARY_DIR}/sources/third-party/libwebrtc/include/webrtc/third_party/libyuv/include/
  ${CMAKE_CURRENT_BINARY_DIR}/sources/third-party/libwebrtc/webrtc/src/third_party/abseil-cpp
)

如何使cmake依赖于库的完整构建,然后再构建主程序?通常一个人使用 add_custom_command(..),但是由于我们使用的是 add_subdirectory(..),因此我们也不能使用 add_custom_command(..)

How to make cmake depend on the complete build of the library and then build the main program onwards? Usually one uses add_custom_command(..) but since we are using add_subdirectory(..) we can't also use add_custom_command(..).

我对此问题的解决方法是将库重命名为libwebrtc/CMakeLists.txt中的project(libwebrtcx)以及ExternalProject_Add(libwebrtcx ...)并添加 add_dependencies($ {PROJECT_NAME} libwebrtcx)但是可以做到这一点吗?

My hack to this problem was to rename the library to project(libwebrtcx) inside libwebrtc/CMakeLists.txt and also the ExternalProject_Add(libwebrtcx ...) and add a add_dependencies(${PROJECT_NAME} libwebrtcx) but can it be done without that hack?

推荐答案

错误的CMAKE_SOURCE_DIR

我认为错误似乎在此处

Wrong CMAKE_SOURCE_DIR

In my opinion the error seems to be within here

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
    ${CMAKE_SOURCE_DIR}/CMakeModules)

CMAKE_SOURCE_DIR将是您的主要源目录的路径.将此更改为

CMAKE_SOURCE_DIR will be the path to your main source-Dir. Change this into

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
    ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules)


用作外部依存关系

另一个想法是自己构建项目,并将其作为外部依赖项在您的项目中使用.


Use as external dependendecy

Another idea is to build the project by its own and use it within your project as an external dependecy.

find_package(LibWebRTC REQUIRED)
include(${LIBWEBRTC_USE_FILE})

target_link_libraries(my-app ${LIBWEBRTC_LIBRARIES})

这篇关于CMake:将add_subproject与包含Include的库一起使用会导致错误的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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