我如何从CMake项目的构建树中依赖它的构建目标?为什么我试图从它的构建树中找到_package不起作用?

b1payxdu  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(90)

我编译了一些开源应用程序,没有使用make install安装,所以可执行文件仍然在构建路径中:

/home/vagrant/oatpp/build/src
vagrant@vagrant:~/oatpp/build/src$ find . -name "*.a"
./liboatpp-test.a
./liboatpp.a
vagrant@vagrant:~/oatpp/build/src$

字符串
现在我喜欢使用简单的启动器应用程序来编译与此库链接的应用程序,其CMakefile看起来像这样:

cmake_minimum_required(VERSION 3.1)
 "/home/vagrant/oatpp/")
set(project_name my-project) ## rename your project here

project(${project_name})

set(CMAKE_CXX_STANDARD 11)

add_library(${project_name}-lib
        src/AppComponent.hpp
        src/controller/MyController.cpp
        src/controller/MyController.hpp
        src/dto/DTOs.hpp
)

## link libs

find_package(oatpp 1.3.0 REQUIRED PATHS /home/vagrant/oatpp/build/src /home/vagrant/oatpp/build/ /home/vagrant/oatpp/)

target_link_libraries(${project_name}-lib
        PUBLIC oatpp::oatpp
        PUBLIC oatpp::oatpp-test
)

target_include_directories(${project_name}-lib PUBLIC src)

## add executables

add_executable(${project_name}-exe
        src/App.cpp
        test/app/MyApiTestClient.hpp)
target_link_libraries(${project_name}-exe ${project_name}-lib)
add_dependencies(${project_name}-exe ${project_name}-lib)

add_executable(${project_name}-test
        test/tests.cpp
        test/app/TestComponent.hpp
        test/app/MyApiTestClient.hpp
        test/MyControllerTest.cpp
        test/MyControllerTest.hpp
)

target_link_libraries(${project_name}-test ${project_name}-lib)
add_dependencies(${project_name}-test ${project_name}-lib)
set_target_properties(${project_name}-lib ${project_name}-exe ${project_name}-test PROPERTIES
        CXX_STANDARD 11
        CXX_EXTENSIONS OFF
        CXX_STANDARD_REQUIRED ON
)

enable_testing()
add_test(project-tests ${project_name}-test)


当执行时,我得到:

vagrant@vagrant:~/oatpp-starter/build$ cmake ..
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /home/vagrant/oatpp/build/src/oatppConfig.cmake:18 (include):
  include could not find requested file:

    /home/vagrant/oatpp/build/src/oatppTargets.cmake
Call Stack (most recent call first):
  CMakeLists.txt:18 (find_package)

CMake Error at /home/vagrant/oatpp/build/src/oatppConfig.cmake:11 (message):
  File or directory /home/vagrant/include/oatpp-1.3.0/oatpp/ referenced by
  variable oatpp_INCLUDE_DIRS does not exist !
Call Stack (most recent call first):
  /home/vagrant/oatpp/build/src/oatppConfig.cmake:21 (set_and_check)
  CMakeLists.txt:18 (find_package)


如何设置cmake的搜索路径?

uz75evzq

uz75evzq1#

如果您希望从其构建目录中依赖的项目使用export命令导出了其目标,那么您可以include目标文件以依赖该构建目录中的目标输出。如果您硬编码一个相对路径或绝对路径,那就有点脆弱了,因为它假定了所依赖的构建目录的特定相对/绝对位置。如果担心这种脆弱性,可以使用-D将构建目录作为缓存变量传递,然后在include调用中使用该变量。
在同样的场景中,我认为你也可以编写一个find模块,它只是 Package 了一个类似的include调用。
或者,您可以在依赖项上运行安装,然后使用find_package--假设依赖项为安装提供了find-config脚本。
至于你得到的错误消息,这是因为find_package设法找到了在安装过程中复制到安装树的软件包的配置文件的配置/生成版本,并且应该在安装运行后使用。当然,没有目标文件,因为目标文件是在运行安装时创建的。如果你真的不知道自己在做什么,这是一个不幸和令人困惑的场景。

相关问题