Boost Linker Issues

105 Views Asked by At

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:

find_package(Boost 1.73.0 COMPONENTS program_options REQUIRED)
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?

2

There are 2 best solutions below

1
On

I'm gonna copy and paste the answer from the cmake discourse: https://discourse.cmake.org/t/boost-linker-issues/2030

"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:" - Volker Enderlein

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

Mismatch of architecture is the issue here. Match your compile of the application you are building with the boost program options

How do you build the x64 Boost libraries on Windows?

The address needs to be set to 64 in your case or use 64 bit binaries