gpt4 book ai didi

c++ - 如何获取 ExternalProject 定义的目标的输出路径?

转载 作者:行者123 更新时间:2023-11-28 04:52:03 25 4
gpt4 key购买 nike

我正在构建 Google 的 FlatBuffers 作为我自己项目的依赖项,我需要在构建时编译一个模式。我不想使用 BuildFlatBuffers.cmakeFindFlatBuffers.cmake因为我使用的是特定版本,我不能依赖它在本地安装。

这是我的 CMakeLists.txt 的简化版本:

ExternalProject_Add (
flatbuf
URL "https://github.com/google/flatbuffers/archive/v1.8.0.tar.gz"
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
)
add_custom_target (
flatbuf_schema
PREFIX ${FLATBUF_PREFIX}
DEPENDS flatbuf
COMMAND ${FLATBUF_PREFIX}/src/flatbuf-build/flatc --cpp ${FLATBUF_SCHEMA}
)

它在 Make 和 Ninja 上运行良好,但在构建 flatc 的 Xcode 中失败了在Debug目录。

我考虑了这些可能的解决方案:

  • 使用add_subdirectory而不是 ExternalProject_Add这样我就可以使用 ${FLATBUFFERS_FLATC_EXECUTABLE}$<TARGET_FILE:flatc> ;
  • 手动分配 RUNTIME_OUTPUT_DIRECTORY对于 flatbuf ;
  • 搜索 flatc在多个路径中(不可移植;我也不知道如何在构建时实现它)。

我尝试了 (2) 和 (3) 但没有成功。至于 (1),我不确定这是个好主意。如何以可移植的方式构建模式?

最佳答案

您可以使用 ExternalProject_Get_Property ,像这样的……

注意:我想您甚至不需要安装 flatbuf,只需构建并使用它即可。

ExternalProject_Add (
flatbuf_project
URL "https://github.com/google/flatbuffers/archive/v1.8.0.tar.gz"
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
INSTALL_COMMAND ""
)

ExternalProject_Get_Property(flatbuf_project source_dir)
ExternalProject_Get_Property(flatbuf_project binary_dir)

# Export flatbuf executable to consume schema file during build
add_executable(flatbuf::flatbuf IMPORTED)
set_target_properties(flatbuf::flatbuf PROPERTIES IMPORTED_LOCATION
"${binary_dir}/flatc")
add_dependencies(flatbuf::flatbuf flatbuf_project)

add_custom_target(flatbuf_schema
PREFIX ${FLATBUF_PREFIX}
COMMAND flatbuf::flatbuff --cpp ${FLATBUF_SCHEMA}
)

注2:

If COMMAND specifies an executable target name (created by the add_executable() command) it will automatically be replaced by the location of the executable created at build time. If set, the CROSSCOMPILING_EMULATOR executable target property will also be prepended to the command to allow the executable to run on the host. Additionally a target-level dependency will be added so that the executable target will be built before this custom target.

注3:不幸的是,目标 ALIAS 不适用于 IMPORTED 目标......

关于c++ - 如何获取 ExternalProject 定义的目标的输出路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com