Search code examples
c++clionvcpkg

How to properly configure vcpkg for CLion?


Hello everyone There was a problem with the work of vcpkg and CLion, specifically, I installed the boost library via CLion vcpkg (The one that is downloaded automatically in the IDE), wrote a program, CMake finds the library, the IDE sees it, but nothing compiles.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.25)
project(untitled1)

set(CMAKE_CXX_STANDARD 17)

add_executable(untitled1 main.cpp)
find_package(Boost 1.82.0 COMPONENTS program_options REQUIRED)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(untitled1 PRIVATE Boost::boost)
endif()

command for CMake:

"B:\CLion 2023.1\bin\cmake\win\x64\bin\cmake.exe" -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=B:\vcpkg_clion\scripts\buildsystems\vcpkg.cmake -S B:\untitled1 -B B:\untitled1\cmake-build-debug-visual-studio

main.cpp:

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

namespace opt = boost::program_options;

int main(int argc, char *argv[]) {
    opt::options_description desc("All options");
    desc.add_options()
            ("apples", opt::value<int>(), "how many apples you have")
            ("oranges", opt::value<int>(), "how many oranges you have")
            ("help", "produce help message")
            ;
    opt::variables_map vm;
    opt::store(opt::parse_command_line(argc, argv, desc), vm);
    opt::notify(vm);
    if (vm.count("help"))
    {
        std::cout << desc << "\n";
        return 1;
    }
    std::cout << "Fruits count: "
              << vm["apples"].as<int>() + vm["oranges"].as<int>()
              << std::endl;
    return 0;
}

command for build:

"B:\CLion 2023.1\bin\cmake\win\x64\bin\cmake.exe" --build B:\untitled1\cmake-build-debug-visual-studio --target untitled1 --config Debug -j 10

compilator output:

main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl boost::program_options::validation_error::validation_error(enum boost::program_options::validation_error::kind_t,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (__imp_??0validation_error@program_options@boost@@QEAA@W4kind_t@012@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@1H@Z) в функции "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __cdecl boost::program_options::validators::get_single_string<char>(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > const &,bool)" (??$get_single_string@D@validators@program_options@boost@@YAAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@4@_N@Z). [B:\untitled1\cmake-build-debug-visual-studio\untitled1.vcxproj]
...
...
...

There is a big conclusion there, but in general, the LNK2019 error occurs

triplets that I used in vcpkg - x64-windows

so, I tried using vcpkg in Visual Studio 2022, installed vcpkg from GitHub, used "vcpkg install integrate", installed boost, and everything compiled. The only thing I didn't set up CMakeLists.txt (I don't even know if VS 2022 uses CMake and if it does, where it store CMakeLists?)

Most likely I missed something in the CLion setup and don't quite understand what, so I'll ask the question: How to properly configure CLion to use vcpkg without any problems?


Solution

  • From the Imported Targets section of the documentation of FindBoost it looks like your

    target_link_libraries(untitled1 PRIVATE Boost::boost)
    

    links only to the boost-header only target and not the program_options component.

    Here are the the descriptions of the targets taken from the documentation:

    Boost::boost
    Target for header-only dependencies. (Boost include directory).
    
    Boost::<component>
    Target for specific component dependency (shared or static library); <component> name is lower-case.
    

    To fix this you need to add Boost::program_options to your target_link_libraries:

    target_link_libraries(untitled1 PRIVATE Boost::boost Boost::program_options)