是否将Boost添加到CMake项目? [英] Adding Boost to CMake project?

查看:132
本文介绍了是否将Boost添加到CMake项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我是一个使用C ++的新手,我已经一次又一次地头痛不已,因此,如果这非常简单,请原谅我,而我 那只是愚蠢的 ...

我有一个最终可以在Linux上编译并运行的项目.不幸的是,在我的C ++开发环境出现了很多问题(仍未解决)之后,我放弃了尝试可以在Linux上开发并移至Windows Visual Studio2017.我的希望是让我的代码在Windows上运行,然后,由于C ++被认为是一种可移植的语言,因此它只需稍作改动即可在Linux上运行.

Visual Studio似乎已经工作了一天左右.我可以编写代码,点击编译",然后像魔术一样运行.我汇集了几个类来构造一个有向无环图,将标准库用于哈希表,然后尝试创建一个套接字...

Windows和Linux对套接字使用不同的库(<sys/socket.h><winsock.h>),因此我需要某种方式来抽象化差异,并且我更喜欢建立良好的标准.我在Google上四处搜寻,发现 Boost库似乎很符合我的需求./p>

我的项目设置

因为此项目将在多种平台和IDE上开发(某些人使用Windows + Visual Studio,某些人使用Mac + Eclipse,而其他人使用Linux + VIM),所以我选择将其设为CMake项目.经过数小时的阅读,学习和研究,CMake似乎应该给我我想要的东西(便捷且可复制的跨平台构建,几乎没有依赖问题)

我的源代码(直接来自 Windows入门指南指南)如下:

CMakeProject2.cpp

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " ");
}

根据 Windows入门指南指南,我从这里下载了Boost:

https://dl.bintray.com/boostorg/版本/1.67.0/source/boost_1_67_0.zip

(有趣的是,《入门指南》的标题为在Windows上提升Boost入门-1.69.0",但它已链接到Boost版本1.67.0)

下载并解压缩ZIP文件后,我发现文件一团糟-但不知道将文件放在何处:

尝试使其正常运行

我试图无法 a 单个

对于初学者来说,我将Boost ZIP文件的 整个 内容复制到了我的项目中的文件夹名称"Boost"下:

然后我尝试了一系列不同的CMakeList.txt命令:

每个如何在CMakeLists.txt中添加Boost库?:

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost REQUIRED COMPONENTS lambda) 

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS}) 
    add_executable(CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h") 
    target_link_libraries(CMakeProject2 ${Boost_LIBRARIES})
endif()

每个 https://www.selectiveintellect.net/blog/2016/7/29/using-cmake-to-add-third-party-libraries-to-your-project-1 :

include("Boost")
add_subdirectory("Boost")
add_subdirectory("boost")
add_subdirectory("Boost/boost")
add_subdirectory("Boost/boost/lambda")

target_link_libraries(boost)
target_link_libraries(Boost)

https://cmake.org/pipermail/cmake/2009-November/033249.html :

SET (Boost_FIND_REQUIRED TRUE)
SET (Boost_FIND_QUIETLY TRUE)
SET (Boost_DEBUG FALSE)
set (Boost_USE_MULTITHREADED TRUE)
set (Boost_USE_STATIC_LIBS TRUE)
SET (Boost_ADDITIONAL_VERSIONS "1.67" "1.67.0")

FIND_PACKAGE(Boost COMPONENTS  lambda)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})

我尝试了其他几种方法(不熟悉C ++ 作为工具的CMake),并且从CMakeLists.txt或CMakeProject2.cpp收到有关cannot open source file "boost/lambda/lambda.hpp".实际上,对于那些"CMakeLists.txt"错误,在向我的文件添加 足够 行之后,我开始定期使Visual Studio崩溃.请注意,我有第8代i7、32 GB的RAM和M.2 NVMe硬盘-因此,令我印象深刻的是,文本文件中的几行惹恼了Microsoft,足以将我的计算机在10分钟内锁定时间.

全部失败,我尝试将所需的文件直接复制到项目中:

现在,再次,我是C/C ++开发的新手,所有可能出错的地方 都出错了.到目前为止,我已经花了将近两个星期的时间,几乎没有设法在两台计算机,三个IDE和四个编译器上编译一个"Hello,World"应用程序.我还没有取得任何成功,包括从任何地方,任何流行程度或简单程度的第三方库,并实际上编译了引用该库的功能程序.因此,当我说以下内容时,请相信我:我不知道仅标头库"与...之间的区别.我只是知道,根据 Windows入门指南指南,Boost的 most 是仅标头",因此我不需要任何构建步骤-使用起来应该很简单.此外,根据其说明,此示例(使用boost::lambda)是仅标头的库,因此应该非常易于使用.

我现在稍微更新了源代码,以查找当前目录,而不是查找系统包含目录(据我所知,该目录在Windows中不存在):

#include "boost/lambda/lambda.hpp"
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " ");
}

现在,我可以 手动验证 (该文件存在(可以在文件资源管理器中找到文件CMakeProject2\CMakeProject2\boost\lambda\lambda.hpp)),但我是 仍然 出现错误:

cannot open source file "boost/lambda/lambda.hpp"

有些进一步的Google搜索使我再次更新了CMakeLists.txt文件,并将其设置为当前格式:

# CMakeList.txt : CMake project for CMakeProject2, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)

# Add source to this project's executable.
file(GLOB CMakeProject2_SRC
    "*.h"
    "*.cpp"
    "**/*.h"
    "**/*.cpp"
    "**/*.hpp"
    "boost/lambda/lambda.hpp"
)
add_executable (CMakeProject2 ${CMakeProject2_SRC})
#add_executable (CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h")

# TODO: Add tests and install targets if needed.

尽管如此,我仍然 仍然 遇到错误:

cannot open source file "boost/lambda/lambda.hpp"

这时我正在扯头发.我究竟做错了什么?我不知道什么像"Hello,World"的Boost一样简单的东西对我不起作用?

解决方案

下面的食谱应该有用

官方Boost二进制文件位置下载Boost二进制文件并安装以说C:\ Boost

大多数时候,您不需要自己构建Boost.

您的CMakeLists.txt应该如下所示

cmake_minimum_required (VERSION 3.8)

project(boostAndCMake)

set(BOOST_ROOT "C:\Boost") # either set it here or from the command line  
set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost REQUIRED COMPONENTS system) # header only libraries must not be added here

add_executable(CMakeProject2 CMakeProject2.cpp CMakeProject2.h) 
target_include_directories(CMakeProject2 PUBLIC ${Boost_INCLUDE_DIRS}) 
target_link_libraries(CMakeProject2 ${Boost_LIBRARIES})

由于我们在find_package调用中使用了REQUIRED,因此CMake将无法执行,并且如果找不到该脚本,则跳过该脚本的其余部分.因此,无需检查Boost_FOUND.省略REQUIRED时,需要检查它.

现在从脚本所在目录的命令行调用中进行

:

cmake -H. -Bbuildit -G "Visual Studio 15 2017" -DBOOST_ROOT=C:\Boost 

这将在当前目录中创建一个名为buildit的构建目录,进一步在构建目录内为Visual Studio 2017创建一个解决方案,并提供变量BOOST_ROOT的设置,该变量在find_package调用中用于标识Boost.您计算机上的目录.要查看find_package(Boost ...)调用上可用的选项,请参见 FindBoost 文档.

仅标头库

如果仅库是标头,则需要从find_package(Boost ...)调用中忽略它们.要查看哪些库不是 not 标头,请仅查看此一起使用. Boost命名方案的最后一次更改是从1.65.1更改为1.66.

Background

I'm a complete newb with C++ and I've been running into one headache after another, so forgive me if this is incredibly simple and I'm just that dumb...

I have a project that should ultimately compile and run in Linux. Unfortunately after lots of issues with my C++ development environemnt (still unresolved), I gave up on trying to develop in Linux and moved to Windows Visual Studio 2017. My hope was to get my code working in Windows and then, since C++ is supposedly a portable language, it should just work in Linux with minimal changes.

For a day or so Visual Studio seemed to be working. I could write code, hit "compile", and like magic it would run. I threw together a few classes to construct a directed acyclic graph, used the standard library for a hash table, and then I tried to create a socket...

Windows and Linux use different libraries for sockets (<sys/socket.h> vs <winsock.h>) so I needed some way to abstract the differences, and I preferred a well-established standard. Googling around I found the Boost library that seemed to fit my needs... That's when everything went to hell.

My project setup

Because this project will be developed across a variety of platforms and IDEs (some people use Windows + Visual Studio, some people use Mac + Eclipse, and others use Linux + VIM) I opted to make it a CMake project. After several hours of reading and learning and research it seems like CMake should give me what I want (convenient and reproducible cross-platform builds with little or no dependency issues)

My source code (directly from the Boost Getting Started on Windows guide) is as follows:

CMakeProject2.cpp

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " ");
}

Per the Boost Getting Started on Windows guide, I downloaded Boost from here:

https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.zip

(interestingly, the Getting Started guide is titled "Boost Getting Started on Windows - 1.69.0", yet it linked to Boost versions 1.67.0)

After downloading and extracting the ZIP file, I had a whole mess of files - but no idea where to put them:

Attempts to Get It Working So Far

I tried to add the Boost library to my project, but none of the expected menu options were available:

Although I couldn't find a single page that warns you of this gotcha, apparently CMake projects don't have the elusive "Properties" window - and instead third party libraries must somehow be included via the CMakeLists.txt file

For starters, I copied the entire 540 MB contents of the Boost ZIP file to within my project under the folder name "Boost":

I then tried a series of different CMakeList.txt commands:

Per How do you add Boost libraries in CMakeLists.txt?:

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost REQUIRED COMPONENTS lambda) 

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS}) 
    add_executable(CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h") 
    target_link_libraries(CMakeProject2 ${Boost_LIBRARIES})
endif()

Per https://www.selectiveintellect.net/blog/2016/7/29/using-cmake-to-add-third-party-libraries-to-your-project-1:

include("Boost")
add_subdirectory("Boost")
add_subdirectory("boost")
add_subdirectory("Boost/boost")
add_subdirectory("Boost/boost/lambda")

target_link_libraries(boost)
target_link_libraries(Boost)

Per https://cmake.org/pipermail/cmake/2009-November/033249.html:

SET (Boost_FIND_REQUIRED TRUE)
SET (Boost_FIND_QUIETLY TRUE)
SET (Boost_DEBUG FALSE)
set (Boost_USE_MULTITHREADED TRUE)
set (Boost_USE_STATIC_LIBS TRUE)
SET (Boost_ADDITIONAL_VERSIONS "1.67" "1.67.0")

FIND_PACKAGE(Boost COMPONENTS  lambda)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})

I tried several other incantations (not being familiar with C++ or CMake as a tool) and either received errors from CMakeLists.txt, or from CMakeProject2.cpp about cannot open source file "boost/lambda/lambda.hpp". In fact, with regards to those "CMakeLists.txt" errors, after adding enough lines to my file I started to crash Visual Studio regularly. Note that I have an 8th generation i7, 32 gigabytes of RAM, and an M.2 NVMe hard drive -- so I was rather impressed that a few lines in a text file pissed off Microsoft enough to lock up my computer for 10 minutes at a time.

Failing all of that, I tried copying the files I needed directly into my project:

Now, again, I'm new to C/C++ development and everything that can go wrong has gone wrong. So far I've spent almost two weeks and barely managed to compile a single "Hello, World" app across two computers, three IDEs, and four compilers. I've yet to have any success including a third party library, from anywhere, of any popularity level or simplicity level, and actually compile a functioning program that references the library. So believe me when I say: I don't know the difference between a "header-only library" and... whatever the alternative is. I just know that, according to the Boost Getting Started on Windows guide, most of Boost is "headers only" and therefore I shouldn't have any build step -- it should be simple to use it. Furthermore, this example (using boost::lambda) is - per their instructions - a header-only library, and should therefore be extremely easy to use.

I now updated the source code slightly to look in the current directory, instead of looking in the system include directory (which, as far as I'm aware at this point, doesn't exist in Windows):

#include "boost/lambda/lambda.hpp"
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " ");
}

Now I can manually verify that this file exists (the file CMakeProject2\CMakeProject2\boost\lambda\lambda.hpp can be found in File Explorer) - yet I'm still getting errors:

cannot open source file "boost/lambda/lambda.hpp"

Some further Googling led me to update my CMakeLists.txt file once more, putting it in its current form:

# CMakeList.txt : CMake project for CMakeProject2, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)

# Add source to this project's executable.
file(GLOB CMakeProject2_SRC
    "*.h"
    "*.cpp"
    "**/*.h"
    "**/*.cpp"
    "**/*.hpp"
    "boost/lambda/lambda.hpp"
)
add_executable (CMakeProject2 ${CMakeProject2_SRC})
#add_executable (CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h")

# TODO: Add tests and install targets if needed.

Despite this I'm still getting the error:

cannot open source file "boost/lambda/lambda.hpp"

At this point I'm ripping my hair out. What am I doing wrong? What do I not know? How is something as simple as the Boost-equivalent of "Hello, World" not working for me?

解决方案

Following recipe should work

Download Boost binaries from official boost binaries location and install to say C:\Boost

Most times you do not need to build Boost on your own.

Your CMakeLists.txt should look like follows

cmake_minimum_required (VERSION 3.8)

project(boostAndCMake)

set(BOOST_ROOT "C:\Boost") # either set it here or from the command line  
set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost REQUIRED COMPONENTS system) # header only libraries must not be added here

add_executable(CMakeProject2 CMakeProject2.cpp CMakeProject2.h) 
target_include_directories(CMakeProject2 PUBLIC ${Boost_INCLUDE_DIRS}) 
target_link_libraries(CMakeProject2 ${Boost_LIBRARIES})

Because we used REQUIRED on the find_package call, CMake will fail execution and skip the rest of the script if it cannot be found. So no need to check Boost_FOUND. You need to check it, when you omit REQUIRED.

Now from the command line call from the directory where your script resides:

cmake -H. -Bbuildit -G "Visual Studio 15 2017" -DBOOST_ROOT=C:\Boost 

This creates a build directory named buildit in the current directory, further creates a solution for Visual Studio 2017 inside the build directory and provides the setting for the variable BOOST_ROOT that is used in the find_package call to identify the Boost directory on your computer. To see what options are available on the find_package(Boost ...) call see FindBoost documentation in CMake.

Header Only Libraries

If your libraries are header only you need to omit them from the find_package(Boost ...) call. To see which libraries are not header only see this post.

Using newer Boost versions

If your CMake installation cannot find the requested version, e.g. 1.69.0, but supports the naming scheme of the more recent Boost version you can use it with set(Boost_ADDITIONAL_VERSIONS "1.69.0" "1.69"). Last change of the Boost naming scheme was from 1.65.1 to 1.66.

这篇关于是否将Boost添加到CMake项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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