0

I'm trying to link my project to various boost libraries (thread, system, program_options, filesystem, and date_time). I'm using CLion's bundled MinGW, and have used Vcpkg to install my required libraries, and added the requirements to vcpkg.json. CLion seems to have handled all of this well, and from what I can tell, I've done everything correctly - the libraries are all built and installed locally, and exist, the headers are found, etc. From all the documentation and other StackOverflow posts I can find, I'm doing everything correctly, but, the project refuses to link successfully.

Here is my CMakeLists.txt. Things which are commented out are various things I've tried to make this work.

cmake_minimum_required(VERSION 3.25)
project(myproject)

set(CMAKE_CXX_STANDARD 17)

# (I've tried various combinations of these to no avail)
# set(Boost_USE_STATIC_LIBS ON)
# set(Boost_USE_MULTITHREADED ON)
# set(Boost_USE_STATIC_RUNTIME ON)
# set(Boost_ARCHITECTURE -x64)
# set(Boost_USE_STATIC_LIBS OFF)
find_package(Boost REQUIRED COMPONENTS program_options system thread filesystem)

message("Boost libraries: ${Boost_LIBRARIES}")

include_directories(
        ${Boost_INCLUDE_DIR}
        lib/libmyproject/include
        include)

add_library(libmyproject STATIC
        lib/libmyproject/source/libmyproject.cpp)

add_executable(myproject
        source/myproject.cpp)

#link_libraries(${Boost_LIBRARIES})
#target_link_libraries(myproject -static)
#target_link_libraries(myproject ${Boost_LIBRARIES})
#target_link_libraries(myproject Boost::thread Boost::program_options Boost::system)
target_link_libraries(myproject ws2_32 ${Boost_LIBRARIES})

This is the output that CLion gives from CMake:

"C:\Program Files\JetBrains\CLion 2023.1.4\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2023.1.4/bin/ninja/win/x64/ninja.exe" -DCMAKE_TOOLCHAIN_FILE=C:\Users\myprofile\.clion-vcpkg\vcpkg\scripts\buildsystems\vcpkg.cmake -DBoost_NO_WARN_NEW_VERSIONS=1 -G Ninja -S D:\devel\repositories\myproject -B D:\devel\repositories\myproject\cmake-build-debug
-- Running vcpkg install
waiting to take filesystem lock on C:\Users\myprofile\.clion-vcpkg\vcpkg\.vcpkg-root...
Detecting compiler hash for triplet x64-windows...
All requested packages are currently installed.
Total install time: 200 ns
The package boost is compatible with built-in CMake targets:

    find_package(Boost REQUIRED [COMPONENTS <libs>...])
    target_link_libraries(main PRIVATE Boost::boost Boost::<lib1> Boost::<lib2> ...)

-- Running vcpkg install - done
Boost libraries: D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_program_options-vc140-mt.lib;D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_system-vc140-mt.lib;D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_thread-vc140-mt.lib;D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_filesystem-vc140-mt.lib;D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_chrono-vc140-mt.lib;D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_atomic-vc140-mt.lib
-- Configuring done
-- Generating done
-- Build files have been written to: D:/devel/repositories/myproject/cmake-build-debug

[Finished]

The library files outputted by my message() all exist, and are built, and seem to be valid in every way. CMake seems to find them just fine, and the boost libraries are passed to the linker. The linker command is as follows:

cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.4\bin\mingw\bin\G__~1.EXE -g CMakeFiles/myproject.dir/source/myproject.cpp.obj -o myproject.exe -Wl,--out-implib,libmyproject.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lws2_32  vcpkg_installed/x64-windows/lib/boost_thread-vc140-mt.lib  vcpkg_installed/x64-windows/lib/boost_program_options-vc140-mt.lib  vcpkg_installed/x64-windows/lib/boost_system-vc140-mt.lib  vcpkg_installed/x64-windows/lib/boost_chrono-vc140-mt.lib  vcpkg_installed/x64-windows/lib/boost_atomic-vc140-mt.lib  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cmd.exe /C "cd /D D:\devel\repositories\myproject\cmake-build-debug && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file C:/Users/myprofile/.clion-vcpkg/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary D:/devel/repositories/myproject/cmake-build-debug/myproject.exe -installedDir D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/debug/bin -OutVariable out""

This fails however, with various references to undefined boost symbols across all the boost libraries I need to link. I'll spare the question the long output, but, for example:

C:\Program Files\JetBrains\CLion 2023.1.4\bin\mingw\bin/ld.exe: D:/devel/repositories/myproject/source/myproject.cpp:253: undefined reference to `__imp__ZN5boost15program_options19options_descriptionC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj'
C:\Program Files\JetBrains\CLion 2023.1.4\bin\mingw\bin/ld.exe: D:/devel/repositories/myproject/source/myproject.cpp:256: undefined reference to `__imp__ZN5boost15program_options19options_description11add_optionsEv'
C:\Program Files\JetBrains\CLion 2023.1.4\bin\mingw\bin/ld.exe: D:/devel/repositories/myproject/source/myproject.cpp:257: undefined reference to `__imp__ZN5boost15program_options29options_description_easy_initclEPKcS3_'
C:\Program Files\JetBrains\CLion 2023.1.4\bin\mingw\bin/ld.exe: D:/devel/repositories/myproject/source/myproject.cpp:258: undefined reference to `__imp__ZN5boost15program_options29options_description_easy_initclEPKcS3_'
Brett Gmoser
  • 1,702
  • 1
  • 12
  • 19
  • `D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_program_options-vc140-mt.lib;D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_system-vc140-mt.lib;D:/devel/repositories/myproject/cmake-build-debug/vcpkg_installed/x64-windows/lib/boost_thread-vc140-mt.lib` looks like you are attempting to link `msvc` libraries with `MinGW`. If you are going to use MinGW as your compiler you need to build your libraries with it and not msvc if the library is `c++` based. – drescherjm Jul 01 '23 at 22:11
  • My advice to you if you want to use MinGW is to install [msys2](https://www.msys2.org/) and use the builtin `pacman` package manager to install your libraries. – drescherjm Jul 01 '23 at 22:14
  • That it is trying to link libraries with `vc140` in the name is certainly a clue. I'm new to CLion and CMake. I'd prefer to have CLion handle this all for me using Vcpkg and the bundled compiler, without resorting to fussing with external tools. It seems that Vcpkg has a triplet for eg. `x64-mingw-static`, but they won't install - logs complain of "No CMAKE_C_COMPILER could be found.". – Brett Gmoser Jul 01 '23 at 22:42

0 Answers0