添加 boost program_options 作为 git 子模块并使用其源构建可执行文件

Add boost program_options as a git submodule and build an executable with its sources

我有一个使用 boost::program_options 的小项目。我想将这个(只有这个)库作为 git submodule 添加到我的项目中,并使用它的源构建我的可执行文件。

我已经完成了 git submodule 部分 (git submodule add https://github.com/boostorg/program_options.git),并且我做了一个小实验,其中我只使用提到的子模块中的源构建了一个示例应用程序(仔细检查没有额外的依赖项)。

现在我想将这些资源添加到我的项目中。我在 CMakeLists.txt 文件中所做的是:

set(BOOST_PROGRAM_OPTIONS_SOURCES
  boost/program_options/src/split.cpp
  boost/program_options/src/positional_options.cpp
  boost/program_options/src/parsers.cpp
  boost/program_options/src/options_description.cpp
  boost/program_options/src/convert.cpp
  boost/program_options/src/config_file.cpp
  boost/program_options/src/cmdline.cpp
  boost/program_options/src/winmain.cpp
  boost/program_options/src/variables_map.cpp
  boost/program_options/src/value_semantic.cpp
  boost/program_options/src/utf8_codecvt_facet.cpp
)

add_executable(
  ...
  src/main.cpp
  ${BOOST_PROGRAM_OPTIONS_SOURCES}
)

但我收到以下错误:

CMake Error at CMakeLists.txt:69 (add_executable):
  The target name "boost/program_options/src/split.cpp" is reserved or not
  valid for certain CMake features, such as generator expressions, and may
  result in undefined behavior.


CMake Error at CMakeLists.txt:86 (target_link_libraries):
  Cannot specify link libraries for target "..." which is not built
  by this project.

我想做什么才是正确的>

我找到问题了。

我已将 boost::program_options (git clone https://github.com/boostorg/program_options.git) 克隆到与 CMakeLists.txt

相同的文件夹中

之后,按照正常的FindBoost流程

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED OFF)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package(Boost 1.45.0 COMPONENTS program_options)

if(Boost_FOUND)
  message(WARNING "BOOST FOUND")
  include_directories(${Boost_INCLUDE_DIRS})
  set(BOOST_LIBS ${Boost_LIBRARIES})
else()
  set(BOOST_LIBS )
  message(WARNING "BOOST NOT FOUND")
endif()
...
...
target_link_libraries(... ${BOOST_LIBS})
...
...

CMake 能够找到提升,即使 program_options 不包含任何 CMakeLists.txt(我至少找不到)