链接CLion项目中的提升 [英] Linking boost in CLion project

查看:74
本文介绍了链接CLion项目中的提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我在CLion上的c ++项目链接到Boost.为了使一切都更容易移植,我希望将所有库直接放在项目文件中,因此我将boost文件夹直接放在我的项目文件中,然后继续编辑CMakeList.txt以链接库.我想将我在CLion上的c ++项目与Boost链接起来.为了使一切更容易移植,我希望将所有库直接放在项目文件中,因此我将boost文件夹直接放在我的项目文件中,然后继续编辑CMakeList.txt以链接库.

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})

但是CMake一直告诉我它-找不到Boost(丢失:系统文件系统)(找到版本"1.71.0")".我试图将1.71.0添加到find_package中,但它不会改变CMake告诉我无法找到Boost的事实.关键是,如果我的find_package行变为:

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说找到了Boost:C:/Users/kimer/CLionProjects/untitled/boost_1_71_0(找到了合适的版本"1.71.0",最低要求是"1.71.0")".我不确定该怎么想.

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.

我不一定要编译库,因为大多数组件仅是头文件,不需要编译.此外,我尝试编译Boost,但是它需要msvc,但是我使用的编译器是mingw w64 g ++,并且也没有找到任何解决方案(我尝试使用mingw参数运行boost.build bootstrap.bat,但它返回一个cpp错误,将无法编译.)

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?

我只有chrono这样的仅标头库有完全相同的问题

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

好吧,看来我必须用我的编译器Mingw x86_64-win32编译Boost,但是每当我尝试编译它时,我都会收到此错误:

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.

我发现了为什么不编译的原因:我使用的是Mingw的w32版本,而不是POSIX.整个东西用POSIX版本编译的很好;现在,我在"boost"文件夹中具有编译好的boost库.我更改了CMakeLists.txt文件:

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)

boost库在调试告诉我的地方搜索正确的目录.它还正在搜索具有以下名称的文件:

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

当我编译的boost库生成了这些A文件时:

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 ?

推荐答案

我找到了整个故事,并且我正在写下解决方案,以防像我这样有点迷路的人需要它.

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

因此,如果要使用mingw作为编译器使用cmake将boost链接到Windows上的项目,首先需要下载并安装具有 posix 线程和cours x86_64体系结构的mingw.然后,下载Boost的源代码,然后可以按照本教程进行构建Boost.对于大多数Boost库,Build Boost是强制性的.顺便说一句,要将内容添加到Windows PATH,您需要转到控制面板">系统和安全性">系统">高级参数">环境变量",然后在其中编辑PATH变量(您可能希望将mingw/bin文件夹添加到PATH).最后一步将花费一些时间.

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.

一旦构建了库,您想要做的就是在CMakeLists.txt文件上链接它们.这是您要添加到文件中的代码:

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})

因此,FindBoost文档中找不到Boost_ARCHITECTURE变量,由于某些原因,未指定该变量.问题是,由boost构建生成的lib文件将被命名为: libboost_atomic-mgw81-mt-x64-1_71.a ,但默认情况下,FindBoost将查找诸如 boost_atomic-mgw81-mt-1_71 之类的名称,此后再也找不到所有名称中都包含架构.因此,要使FindBoost能够搜索名称中带有-x64(或-x32)的.a文件,您需要添加 set(Boost_ARCHITECTURE -x64)行,否则cmake会告诉您它没有找到助推器.

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.

这篇关于链接CLion项目中的提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆