Boost Linker Issues

I am using the Boost library and I am having some linker issues. Currently my code is outputting this:

Undefined symbols for architecture x86_64: “boost::program_options::to_internal(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&)”, referenced from: std::__1::vector<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > > > boost::program_options::to_internal<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > > > const&) in train_model_main.cc.o “boost::program_options::variables_map::variables_map()”, referenced from: _main in train_model_main.cc.o (THE LIST CONTINUES)

At the bottom my code says this:

ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am trying to use the program_options library from Boost, but the linking step seems to be failing. Here is how I link in my CMake file:

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(main ./apps/something.cc)
    target_link_libraries( main program_options)
endif()

I believe that I am correctly linking the library, so what could be causing this issue?

“I believe that I am correctly linking the library” unfortunately not. You need to reference the full target name including the namespace prefix with Boost::program_options. And you can omit adding the Boost_INCLUDE_DIRS explicitely as the target definition does include this information. So your lines would look like:

if(Boost_FOUND)
    add_executable(main ./apps/something.cc)
    target_link_libraries(main PUBLIC Boost::program_options)
endif()

This seemed to fix the linker issue, however, I am getting this issue now:

fatal error: ‘boost/program_options.hpp’ file not found
#include <boost/program_options.hpp>

What platform are you on, what version of Boost are you trying to link to and how does your find_package command for Boost does look like (including all settings for Boost)?

A typical command sequence for Boost in my CMakeLists.txt looks like this:

set(Boost_USE_STATIC_LIBS   ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost REQUIRED program_options)

I am on MacOS. I am using Boost Version 1.73.0. My find_package:
find_package(Boost 1.73.0 COMPONENTS program_options REQUIRED)
After making the changes you suggested, I received the “fatal error: ‘boost/program_options.hpp’ file not found” which occurred once 87% of the build was complete. After adding " INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})" outside of the endif() the build completes but then I get the same issue as before (Undefined symbols for architecture x86_64:, etc and at the bottom it says: NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation))

I believe the step my code is failing on (when building) is:

“Linking CXX executable train-model”

Does this target differs from the one you built with add_executable(main ./apps/something.cc)
or is it the same?

When you say “target”, what are you referring to?

The main in add_executable command. add_executable adds a target to the CMakeLists.txt.

The target I built with add_executable is the file I am running from (the main method). I get these errors when running something.cc. Also, “Linking CXX executable train-model” train-model is something.cc.

Did you built Boost by yourself or installed it via Homebrew or Macports?

I installed it via Homebrew. However, I initially installed it from the website (the .tar file). I installed it via homebrew without uninstalling the .tar files. After installing it via homebrew, I deleted the .tar files.

Did you clear the CMakeCache.txt or the entire build directory after applying those changes? Otherwise some settings might still be cached by CMake.

I did not, how would I go about do that? And should I do that right now or is it too late?

It’s never too late :). Delete the CMakeCache.txt from the build directory and rerun the CMake configure command.

I deleted it. What exactly do I need to rerun to make it reappear? Sorry, I am new to this stuff.

Just call your configure command. A typical sequence from a build dir under the source dir is:

mkdir build
cd build
cmake ..
make
make install

I mean the command that configures your project (cmake ..)

Reconfigured it, still getting the same issue.