前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

Boost

作者头像
用户1145562
发布2020-10-23 12:10:21
1.8K0
发布2020-10-23 12:10:21
举报
文章被收录于专栏:LC刷题LC刷题

前言

我今天真的是被这个boost库搞到头炸,怎么在linux下安装boost库,及后续使用。一开始用sudo apt-get install libboost-dev倒是能解决代码中头文件引用不存在问题,但是编译不成功,总是会出现什么未定义引用错误,之后remove掉,重新下载源码编译还是会存在一些问题。

安装全过程

  1. 这里下载boost的源码包,我下载的是unix平台的boost源码包,Version 为1.73.0。 这里有官网指导教程。
  2. 下载好,复制到桌面吧,之后解压,得到压缩包。 tar --bzip2 -xf boost_1_73_0.tar.bz2
  3. 切换到源码目录,cd boost_1_73_0,可以看到有一个bootstrap.sh文件
  4. 然后运行bootstrap.sh脚本并设置相关参数: ./bootstrap.sh --with-libraries=all --with-toolset=gcc # --with-libraries指定编译哪些boost库,all把全部选上,以免出了啥子差错 # --with-toolset指定编译时使用哪种编译器,Linux使用gcc,当然默认就有
  5. 设置完成以后,开始编译,编译命令./b2,编译过程有点慢,编译结束后大致涨这个模样。
  1. 接着就是安装boost,安装命令./b2 install --prefix=/usr --prefix=/usr用来指定boost的安装目录,不加此参数的话默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下。这里把安装目录指定为–prefix=/usr则boost会直接安装到系统头文件目录和库文件目录下,可以省略配置环境变量。 我没有指定安装目录,直接``./b2 install`,结果还要配置环境变量烦。 还要注意的一点,这个操作是往usr目录写入一些文件,没有提示需要root权限居然也执行成功了,但是在该出现的目录却没有应该要出现的文件,心态崩了,所以需要在root权限下执行,我又重新做了一遍。
  2. 最后运行一下ldconfig, 该命令通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令。

示例使用

示例代码

代码语言:javascript
复制
#include <boost/filesystem.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

std::vector<std::string> dirs;//目录向量字符串形式
int INTERVAL = 10;//间隔

int main () 
{
    for ( boost::filesystem::recursive_directory_iterator end, dir("./0");dir != end; ++dir )
    {
        // std::cout << *dir << "\n";  // full path
        std::string s = dir->path().c_str();
        dirs.push_back(s); // just last bit
        
    }
    std::srand(42);
    int ndirs = dirs.size();
    std::cout << "Directories found: " << ndirs << std::endl;

    std::vector<std::string> files;

    for (int i=0; i<10000000; i++)
    {
        int r = std::rand() % ndirs;
        std::string* d = &dirs[r];
        std::string n = *d + "/f" + std::to_string(i);
        files.push_back(n);
     }

    int nfiles = files.size();
    std::cout << "Files generated: " << nfiles << std::endl;

    int start = (int)time(NULL);
    int count = 0;
    int lastcount = 0;

    for (std::string fn : files)
    {
        count++;
        int f = open(fn.c_str(), O_WRONLY | O_CREAT, 0777);
        close(f);
        int current = (int)time(NULL);
        if (current - start >= INTERVAL)
        {
            start = current;
            printf("%d:%d\n", count, (count-lastcount)/INTERVAL);
            fflush(stdout);
            lastcount = count;
        }
    }
    return 0;
}

调试执行,需要在末尾后面指定 -lboost_filesystem,因为我的示例代码导入了#include <boost/filesystem.hpp>,相应的其他的也需要指定

代码语言:javascript
复制
mm@ubuntu:~/桌面/filt$ g++ create.cpp -o creat -lboost_filesystem
mm@ubuntu:~/桌面/filt$ ./creat
terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
  what():  boost::filesystem::directory_iterator::construct: No such file or directory: "./0"
已放弃 (核心已转储)

总结

我感觉我的折腾劲在一点点磨掉,我不断地搜索着我出现的问题,其实也就那些前人遇到问题,我不断地尝试,不断地复现,而新问题,我解决不了,还是搜索,求人。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-062,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 安装全过程
  • 示例使用
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档