cpp boost库的安装和使用 基于ubuntu和windows 包含jupyter notebook/lab exus-cling kernel和vs code 添加第三方库的方法

参考资料

所有参考资料来源于boost官网 https://www.boost.org/

boost库简介

懒得写了

boost库的安装

linux下基于ubuntu

官方guide
https://www.boost.org/doc/libs/1_78_0/more/getting_started/unix-variants.html
写的比较模糊

下载和解压

https://boostorg.jfrog.io/artifactory/main/release/1.78.0/source/
在这个页面随意选择一个版本下载

比如我下载的是:boost_1_78_0.tar.gz
打开Terminal进入下载的文件夹使用指令:

tar -xzvf boost_1_78_0.tar.gz #boost_1_78_0.tar.gz是文件名

其实就是解压,这里不懂的需要学习tar的方法.
tar指令详解

前置条件

在下一步正式开始安装boost库之前,你需要至少有一个编译环境

没有编译环境的话直接运行会显示
Could not find a suitable toolset.

You can specify the toolset as the argument, i.e.:
    ./build.sh [options] gcc

Toolsets supported by this script are:
    acc, clang, como, gcc, intel-darwin, intel-linux, kcc, kylix, mipspro,
    pathscale, pgi, qcc, sun, sunpro, tru64cxx, vacpp

差不多这样.
如果提示这样可以运行指令:

sudo apt install build-essential

build-essential包含了gcc g++等基本编译器,是ubuntu自己打包的,当然你也可以自己安装clang或者gcc之类的.

正式安装阶段1

打开terminal进入解压到的文件夹,执行:

sudo ./bootstrap.sh

运行成功会显示如下.

Building B2 engine..

###
###
### Using 'gcc' toolset.
###
###

g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


###
###

> g++ -x c++ -std=c++11 -O2 -s -DNDEBUG builtins.cpp class.cpp command.cpp compile.cpp constants.cpp cwd.cpp debug.cpp debugger.cpp execcmd.cpp execnt.cpp execunix.cpp filesys.cpp filent.cpp fileunix.cpp frames.cpp function.cpp glob.cpp hash.cpp hcache.cpp hdrmacro.cpp headers.cpp jam_strings.cpp jam.cpp jamgram.cpp lists.cpp make.cpp make1.cpp md5.cpp mem.cpp modules.cpp native.cpp object.cpp option.cpp output.cpp parse.cpp pathnt.cpp pathsys.cpp pathunix.cpp regexp.cpp rules.cpp scan.cpp search.cpp startup.cpp subst.cpp sysinfo.cpp timestamp.cpp variable.cpp w32_getreg.cpp modules/order.cpp modules/path.cpp modules/property-set.cpp modules/regex.cpp modules/sequence.cpp modules/set.cpp -o b2
> cp b2 bjam
tools/build/src/engine/b2
Unicode/ICU support for Boost.Regex?... not found.
Generating B2 configuration in project-config.jam for gcc...

Bootstrapping is done. To build, run:

    ./b2

To generate header files, run:

    ./b2 headers

The configuration generated uses gcc to build by default. If that is
unintended either use the --with-toolset option or adjust configuration, by
editing 'project-config.jam'.

Further information:

   - Command line help:
     ./b2 --help

   - Getting started guide:
     http://www.boost.org/more/getting_started/unix-variants.html

   - B2 documentation:
     http://www.boost.org/build/

下一步安装之前,

进入root权限

大部分时候先需要进入root权限,否则下一步安装会失败.

su root#进入root权限

然后输入密码,假如一直显示是su: Authentication failure可能是因为没设置root的密码,需要

sudo passwd root#设置root密码

进入root成功后进行下一步

正式安装阶段2

./b2 install

默认会安装到 /usr/local 也可以使用./b2 install --prefix=来设定安装位置,还有别的很多操作见./b2 --help

安装完成

windows

下载和解压

https://boostorg.jfrog.io/artifactory/main/release/1.78.0/source/
下载boost_1_78_0.zip 解压到你想要的位置

安装

进入那个文件夹,运行bootstrap.bat生成b2.exe
运行b2.exe
安装完成

boost库的使用

VS Code

c_cpp_properties.json

c_cpp_properties.json文件中

            "includePath": [
                "${workspaceFolder}/**",
                "F:\\Library\\boost_1_78_0\\**"
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "F:\\Library\\boost_1_78_0\\**"
                ]
            },

其中两个F:\\Library\\boost_1_78_0\\替换成你安装boost的文件夹,这里是win下,linux下改一下斜杠(其实直接用/在win下也可以)

tasks.json

tasks.json文件中

            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g",
                "-I F:\\Library\\boost_1_78_0\\",
                "-Wall",
                "-static-libgcc",
                "-fexec-charset=UTF-8",
                "-std=c++17",
                "-lpthread",
                "-lboost_thread",
                "-lboost_system"
            ],

需要添加 "-I F:\\Library\\boost_1_78_0\\",
-I后的地址是安装boost的位置.

setttings.json

如果使用了code runner等,需要在settings.json code runner的args出添加 "-I F:\\Library\\boost_1_78_0\\",实际上和在task.json中添加的是一个意思

jupyter notebook/lab exus-cling kernel

如果你使用的是jupyter notebook/lab并且安装了exus-cling kernel在最开始的cell里,或者自己写个.h后面用的时候#include

#pragma cling add_library_path("library文件的文件夹地址") //Add library path 
#pragma cling add_include_path("include的头文件等的文件夹地址")//include path
#pragma cling load("external shared library的地址")//Add an external shared library 

后面就可以正常的#include了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值