Linking boost in CLion project

梦想的初衷 提交于 2021-02-11 04:50:34

问题


I want to link my c++ project on CLion with Boost. In order to make everything way easier to port I'd prefer having all the libraries directly in the project files so I put the boost folder directly in my project files and then proceeded to edit the CMakeList.txt to link the library. I want to link my c++ project on CLion with Boost. In order to make everything way easier to port I'd prefer having all the libraries directly in the project files so I put the boost folder directly in my project files and then proceeded to edit the CMakeList.txt to link the library.

set(BOOST_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT ${PROJECT_SOURCE_DIR}/boost_1_71_0/)
set(BOOST_INCLUDEDIR ${PROJECT_SOURCE_DIR}/boost_1_71_0/boost)
find_package(Boost COMPONENTS system filesystem)
/.../
target_link_libraries(untitled ${Boost_LIBRARIES})

But CMake keeps tellming me that it "-- Could NOT find Boost (missing: system filesystem) (found version "1.71.0")". I tried to add 1.71.0 to the find_package but it won't change the fact that CMake tells me that it can't find Boost. The thing is that if my find_package line becomes this :

find_package(Boost COMPONENTS)

CMake says "Found Boost: C:/Users/kimer/CLionProjects/untitled/boost_1_71_0 (found suitable version "1.71.0", minimum required is "1.71.0")". I'm not sure what to think about this.

I don't necessarily want to compile the library since most of the components are header only and don't require to be compiled. Furthermore I tried to compile Boost but it requires msvc, but the compiler i'm using is mingw w64 g++ and have not found any solutions about that either (I tried running the boost.build bootstrap.bat with the mingw parameter but it returns a cpp error and won't compile).

What am I doing wrong to link the library?

Edit : I have the exact same problem with header only libs such as chrono

Edit 2 : Alright it seems that I have to compile Boost with my compiler which is Mingw x86_64-win32, but whenever i try to compile it I get this error :

.\bootstrap.bat gcc
Bootstrapping the build engine
sysinfo.cpp: In function 'unsigned int {anonymous}::std_thread_hardware_concurrency()':
sysinfo.cpp:93:21: error: 'std::thread' has not been declared
         return std::thread::hardware_concurrency();
                     ^~~~~~
Fichier introuvable

Failed to bootstrap the build engine
Please consult bootstrap.log for further diagnostics.

Edit 3 : I found out why it did not compile : I was using the w32 version of Mingw and not the POSIX one. The whole thing compiled fine with the POSIX version ; I now have the compiled boost library in a "boost" folder. I changed my CMakeLists.txt file :

set(Boost_DEBUG ON)
set(BOOST_INCLUDEDIR ${PROJECT_SOURCE_DIR}/boost/include/boost-1_71/)
set(BOOST_LIBRARYDIR ${PROJECT_SOURCE_DIR}/boost/lib)
set(BOOST_NO_SYSTEM_PATHS ON)
find_package(Boost COMPONENTS system)

The boost library searches in the right directories as the debug tells me. It is also searching for files with names like these :

-- [ D:/Dev/CMake/share/cmake-3.15/Modules/FindBoost.cmake:2040 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-mgw81-mt-1_71;boost_system-mgw81-mt;boost_system-mgw81-mt;boost_system-mt-1_71;boost_system-mt;boost_system-mt;boost_system-mt;boost_system
-- [ D:/Dev/CMake/share/cmake-3.15/Modules/FindBoost.cmake:2095 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-mgw81-mt-d-1_71;boost_system-mgw81-mt-d;boost_system-mgw81-mt-d;boost_system-mt-d-1_71;boost_system-mt-d;boost_system-mt-d;boost_system-mt;boost_system

while my compiled boost library produced these A files :

libboost_system-mgw81-mt-d-x64-1_71.a
libboost_system-mgw81-mt-x64-1_71.a

That might be the issue but how can i solve it ?


回答1:


I found the whole story, and I'm writing down the solution in case someone a little lost like me needs it.

So if you want to link boost to a project on windows using cmake with mingw as compiler, first you need to download and install mingw, with posix threads, and of cours x86_64 architecture. Then you download the boost sources and you can follow this tutorial in order to build Boost. Building Boost is mandatory for most of the boost libraries. By the way, to add things to windows PATH you need to go to Control Panel > System and Security > System > Advanced parameters > Environment variables and there you edit the PATH variable (you might want to add the mingw/bin folder to PATH). The last step will take a somewhat long time.

Once you have build your libraries, what you want to do is to link them on your CMakeLists.txt file. This is the code you want to add to the file :

set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT <path/to/boost>)
set(BOOST_INCLUDEDIR <path/to/boost/includes>)
set(BOOST_LIBRARYDIR <path/to/boost/lib>)
set(BOOST_NO_SYSTEM_PATHS ON)
find_package(Boost COMPONENTS  <the components you want to load>)
include_directories(${Boost_INCLUDE_DIRS})

add_executable(your_project main.cpp)

target_link_libraries(untitled ${Boost_LIBRARIES})

So there is something up there that you won't find on FindBoost doc which is the Boost_ARCHITECTURE variable, which for some reason is not specified. The thing is that the lib files produced by the boost build will be named like this : libboost_atomic-mgw81-mt-x64-1_71.a, but by default FindBoost will look for names such as boost_atomic-mgw81-mt-1_71, which it will never find since all the names have the architecture in it. So to enable FindBoost to search for .a files with -x64 (or -x32) in their name you need to add the set(Boost_ARCHITECTURE -x64) line, otherwise cmake will tell you that it did not find Boost.



来源:https://stackoverflow.com/questions/58105485/linking-boost-in-clion-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!