Problem description
I am trying to have the following CMake project working: the idea is to use cmake-conan to have cmake handle the conan install step (sparing the user the need to set up the profile etc).
However it fails to link to Boost.
System configuration
- Ubuntu 20.04.4 LTS
- cmake version 3.23.2 (via snap)
- Conan version 1.49.0 (via virtual env)
- gcc (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
- Python 3.8.10
Minimal example
# CMakelist.txtcmake_minimum_required(VERSION 3.23.2)# project name and languageproject(MYAPP LANGUAGES CXX)# we default to Release build type if DCMAKE_BUILD_TYPE not providedif(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)endif()message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")message(STATUS "C++ flags, Debug configuration: ${CMAKE_CXX_FLAGS_DEBUG}")message(STATUS "C++ flags, Release configuration: ${CMAKE_CXX_FLAGS_RELEASE}")message(STATUS "C++ flags, Release configuration with Debug info: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")message(STATUS "C++ flags, minimal Release configuration: ${CMAKE_CXX_FLAGS_MINSIZEREL}")# Use modern C++ with support for concepts and mp-unitsset(CMAKE_CXX_STANDARD 20)# Prevent use of non-portable compiler extensionsset(CMAKE_CXX_EXTENSIONS OFF)# This makes C++20 a requirement and prevents a "decay" to C++98 when the compiler does not support C++20.set(CMAKE_CXX_STANDARD_REQUIRED ON)# Tell find_package() to first search using Config mode before falling back to Module mode (for conan)set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)set(Boost_DEBUG ON)list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake""${CMAKE_BINARY_DIR}/conan.cmake" TLS_VERIFY ON)endif()include(${CMAKE_BINARY_DIR}/conan.cmake)conan_cmake_configure( REQUIRES boost/1.79.0 GENERATORS cmake_find_package )# By default, Conan only searches for packages from the two central repositories# hosted and moderated by **Conan.io** staff: `conan-center` and `conan-transit`.# We will need packages that are not hosted by these official repositories.# The [Bincrafters](https://bincrafters.github.io/2017/06/06/using-bincrafters-conan-repository/)# community posts new packages/versions every week in a separate Conan repository.conan_add_remote(NAME bincrafters URL https://bincrafters.jfrog.io/artifactory/api/conan/public-conan)# Detect settings like OS and architecture# I think it also detects CMake settings like gcc, gcc-version; cppstd, build_type etcconan_cmake_autodetect(settings)# Since GCC >= 5, the compiler is likely to be using the new CXX11 ABI by default (libstdc++11)# See https://docs.conan.io/en/latest/howtos/manage_gcc_abi.htmlconan_cmake_install(PATH_OR_REFERENCE . BUILD missing REMOTE conancenter bincrafters SETTINGS ${settings} compiler.libcxx=libstdc++11 )find_package(Boost 1.79 REQUIRED COMPONENTS program_options REQUIRED)add_executable(my_app main.cpp)target_link_libraries(my_app Boost::boost)# We need C++ 20 activated with the concepts librarytarget_compile_features(my_app PUBLIC cxx_std_20)
# main.cpp#include <boost/program_options.hpp>namespace bpo = boost::program_options;int main(int argc, char* argv[]){ bpo::variables_map vm; bool verbose = false; return 0;}
Configuration & build
$ mkdir build && cd build$ cmake -D CMAKE_BUILD_TYPE=Release \ -D CMAKE_C_COMPILER=/usr/bin/gcc-10 \ -D CMAKE_CXX_COMPILER=/usr/bin/g++-10 \ ..$ cmake --build .
Error message
[ 50%] Building CXX object CMakeFiles/my_app.dir/main.cpp.o[100%] Linking CXX executable my_app/usr/bin/ld: CMakeFiles/my_app.dir/main.cpp.o: in function `main':main.cpp:(.text.startup+0x24): undefined reference to `boost::program_options::variables_map::variables_map()'/usr/bin/ld: main.cpp:(.text.startup+0x33): undefined reference to `vtable for boost::program_options::variables_map'collect2: error: ld returned 1 exit statusmake[2]: *** [CMakeFiles/my_app.dir/build.make:97: my_app] Error 1make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/my_app.dir/all] Error 2make: *** [Makefile:91: all] Error 2
I have looked around for now two days and I could not find any lead (although I learned a lot).