V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
gramyang
V2EX  ›  Linux

ubuntu 安装 boost 遇到的坑

  •  
  •   gramyang · 2019-12-14 11:41:35 +08:00 · 6713 次点击
    这是一个创建于 1566 天前的主题,其中的信息可能已经有所发展或是发生改变。
    手动 wget 下载 boost,解压后编译成功后,用./b2 install --prefix=/usr 进行安装,安装后 ldconfig 更新动态库链接。
    到这里都没问题,但是在写了一个测试程序:
    #include<iostream>
    #include<boost/asio.hpp>

    int main(){
    boost::shared_ptr<int> myPtr(new int(1));
    std::cout<<*myPtr;
    return 0;
    }

    后,g++编译时提示找不到包。。。换了别的程序也是这样。。。。这我就很迷茫了,boost 库文件在 /usr/lib 下面,boost 头文件在 /usr/include/boost 里面都能找得到。。

    为什么不用 apt-get ?因为 apt-get 死活找不到 boost 的头文件和库文件。。

    求大佬指点!
    第 1 条附言  ·  2019-12-14 19:09:37 +08:00
    好吧,我猜测是 apt 的命令错了,我的是 libboost_dev
    14 条回复    2019-12-14 23:18:50 +08:00
    northisland
        1
    northisland  
       2019-12-14 11:59:38 +08:00
    很久没手动安装了,我是 sudo apt install libboost1.58-all-dev
    northisland
        2
    northisland  
       2019-12-14 12:04:17 +08:00
    你用的是智能指针,应该包含<boost/shared_ptr.hpp>

    另外今年是 2019 年,你可以用用<memory>里的 std::shared_ptr
    FrankHB
        3
    FrankHB  
       2019-12-14 15:00:13 +08:00
    g++ 哪有包的概念。强行要包也得 pkg-config 好吧。
    这种情况不是先 -v 自己看搜索路径么。
    nightwitch
        4
    nightwitch  
       2019-12-14 15:19:06 +08:00
    从代码上来看,boost::shared_ptr 需要的头文件你没有包含进去
    其次,你如果用 g++编译,至少上个 g++的编译参数上来才能让人帮你诊断问题吧
    gramyang
        5
    gramyang  
    OP
       2019-12-14 19:44:49 +08:00
    按照楼上大哥的建议:环境 ubuntu18,用 apt install libboost-all-dev 安装了 boost。路径在 /usr/include/boost,里面感觉也不分头文件和库文件,都混在一起。

    测试程序:
    #include <iostream>
    #include <boost/asio.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>

    int main()
    {
    boost::asio::io_service io;

    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    t.wait();

    std::cout << "Hello, world!\n";

    return 0;
    }

    编译命令:g++ t.cpp -lboost_asio -lboost_date_time -o t
    结果报错:/usr/bin/ld: cannot find -lboost_asio

    求大佬。。。
    gramyang
        6
    gramyang  
    OP
       2019-12-14 19:49:49 +08:00
    @nightwitch 求大佬帮忙,新的代码在楼中
    gramyang
        7
    gramyang  
    OP
       2019-12-14 19:50:16 +08:00
    @northisland 按照大佬的建议,重新安装了 boost,但是还是有问题,求帮助!
    lcdtyph
        8
    lcdtyph  
       2019-12-14 19:58:51 +08:00
    asio 需要 -lboost_system ……
    lcdtyph
        9
    lcdtyph  
       2019-12-14 20:00:03 +08:00
    准确得说 asio 本身是 header only 的,但是它引用了 boost::system 的一些东西,而 boost::system 默认不是 header only 的,所以链接的时候需要加上 -lboost_system
    gramyang
        10
    gramyang  
    OP
       2019-12-14 20:03:38 +08:00
    @lcdtyph 牛批!!!可以了!!
    gramyang
        11
    gramyang  
    OP
       2019-12-14 20:12:40 +08:00
    @lcdtyph 再请教一下,在 clion 上远程连接 ubuntu,但是运行失败,cmake:
    cmake_minimum_required(VERSION 3.1)
    project(Ctest)

    include_directories(/usr/include/boost)
    link_directories(/usr/include/boost)

    add_executable(D test/test.cpp)

    target_link_libraries(D boost_system boost_date_time)

    这个 test.cpp 和上面是一样的代码。。。
    lcdtyph
        12
    lcdtyph  
       2019-12-14 20:22:36 +08:00 via iPhone
    @gramyang
    报错呢…
    gramyang
        13
    gramyang  
    OP
       2019-12-14 20:27:06 +08:00
    @lcdtyph 额,搞定了,把 boost_date_time 换到 boost_system 前面就可以了
    nightwitch
        14
    nightwitch  
       2019-12-14 23:18:50 +08:00
    include_directories 这种方式已经是 cmake 不推荐使用的方式了
    建议学一下怎么用 find_package 模式来链接 boost
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1801 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:31 · PVG 00:31 · LAX 09:31 · JFK 12:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.