boostorg / program_options

Boost.org program_options module
http://boost.org/libs/program_options
110 stars 107 forks source link

Link error with CMake (undefined symbols), not observed when compiling manually #125

Closed david-alvarez-rosa closed 1 year ago

david-alvarez-rosa commented 1 year ago

I'm trying to link against boost/program_options library with a dummy program main.cpp

#include <boost/program_options.hpp>

int main() {
  boost::program_options::options_description desc("My description");
}

by using the following CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(my-project)

find_package(Boost 1.82 REQUIRED)
add_executable(exe "main.cpp")
target_include_directories(exe PRIVATE ${Boost_INCLUDE_DIR})
target_link_libraries(exe ${Boost_LIBRARIES})

And I'm getting the following error

$ cmake -B build
...  [Succeeds]
$ cmake --build build -v
...
[100%] Linking CXX executable exe
/opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -E cmake_link_script CMakeFiles/exe.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++  -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -mmacosx-version-min=13.6 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/exe.dir/main.cpp.o -o exe 
ld: Undefined symbols:
  boost::program_options::options_description::m_default_line_length, referenced from:
      _main in main.cpp.o
  boost::program_options::options_description::options_description(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, unsigned int, unsigned int), referenced from:
      _main in main.cpp.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

However, surprisingly, when compiling manually it works

$ clang++ -I/opt/homebrew/include -L/opt/homebrew/lib -lboost_program_options main.cpp

I've been using Boost libraries in this same project, so I'm not sure what could be going wrong.

Any pointers appreciated!

david-alvarez-rosa commented 1 year ago

I've Boost libraries installed (v. 1.82) directly by Homebrew

$ brew install boost
david-alvarez-rosa commented 1 year ago

Solved by

find_package(Boost 1.65 COMPONENTS program_options REQUIRED)