Search code examples
boostcmakegit-submodulesboost-program-options

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


I have a small project that uses boost::program_options. I want to add this (only this) library to my project as a git submodule and build my executable using its sources.

I have already done the git submodule part (git submodule add https://github.com/boostorg/program_options.git), and I have done a small experiment in which I build an example application using only the sources in the mentioned submodule (to double check that there are not extra dependencies).

Now I want to add these sources to my project. What I do in my CMakeLists.txt file is:

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
)

and

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

but I obtain the following error:

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.

What is the right way to do what I want>


Solution

  • I have found the problem.

    I have cloned boost::program_options (git clone https://github.com/boostorg/program_options.git) in the same folder as the CMakeLists.txt

    After that, following the normal FindBoost flow

    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 is able to find boost even though program_options does not contain any CMakeLists.txt (I can't find it at least)