2

I am trying to do the tutorial on the boost website (https://www.boost.org/doc/libs/1_81_0/doc/html/program_options/tutorial.html), but I cannot get cmake to link to boost. Right now I am only trying to link to program_options. I have pulled boost from github and built it. intensity.cpp

#include <iostream>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int ac, char *av[]){
    po::options_description desc("allowed options");
    desc.add_options()
        ("help", "--intesity: set intesity levels")
        ("intensity", po::value<int>(), "set intensity level")
    ;
    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, desc), vm);
    po::notify(vm);

    if(vm.count("help")) {
        std::cout << desc << "\n";
        return 1;
    }

    if(vm.count("intensity")) {
        std::cout << "intensity level set to "
            << vm["intensity"].as<int>() << "\n";
    } else {
        std::cout << "intesity was not set\n";
    }

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project(Intensity VERSION 1.0)

set(CMAKE_CXX_COMPILER clang)

set(BOOST_ROOT "/Users/nabiel.kandiel/code/thirdparty/boost_1_81_0")
set(Boost_USE_STATIC_LIBS OFF)
set(BOOST_USE_MULTITHREADED ON)
set(BOOST_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.81 REQUIRED COMPONENTS program_options)

add_executable(intensity intensity.cpp)
target_link_libraries(intensity Boost::program_options)

cmake output

NKandiel-REMM22:build nabiel.kandiel$ cmake ../
-- The C compiler identification is AppleClang 14.0.0.14000029
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /opt/homebrew/lib/cmake/Boost-1.81.0/BoostConfig.cmake (found suitable version "1.81.0", minimum required is "1.81") found components: program_options 
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nabiel.kandiel/code/imageProc/build

cmake build

NKandiel-REMM22:build nabiel.kandiel$ cmake --build .
[ 50%] Building CXX object CMakeFiles/intensity.dir/intensity.cpp.o
[100%] Linking CXX executable intensity
Undefined symbols for architecture arm64:
  "std::logic_error::what() const", referenced from:
      vtable for boost::program_options::error in intensity.cpp.o
  "std::runtime_error::what() const", referenced from:
      vtable for boost::wrapexcept<boost::bad_function_call> in intensity.cpp.o
      vtable for boost::bad_function_call in intensity.cpp.o
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced
.....
      std::__1::allocator<boost::program_options::basic_option<char> >::deallocate(boost::program_options::basic_option<char>*, unsigned long) in intensity.cpp.o
      ...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [intensity] Error 1
make[1]: *** [CMakeFiles/intensity.dir/all] Error 2
make: *** [all] Error 2

I have tried using a combination of CMakeLists.txt I have found online and none have worked.

  • Hi, how do you have installed your BOOST 1.81.0 library? – loic.lopez Feb 04 '23 at 18:15
  • 2
    As you can see from the configuration log, CMake **configures** the project for use compiler `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++`, not a `clang` as you assigned to `CMAKE_CXX_COMPILER` variable. But most likely **building** the project uses `clang`. See that [my answer](https://stackoverflow.com/a/63944545/3440745) about why your setting is wrong. – Tsyvarev Feb 04 '23 at 18:21
  • try add `include_directories(${Boost_INCLUDE_DIRS})` in your `CMakeLists.txt` – Tom Feb 06 '23 at 08:27

0 Answers0