Ubuntu下编译boost1.53

本文详细介绍如何在Linux环境下下载、编译并配置Boost库,包括环境变量设置、全局变量创建及项目中Boost库的使用步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.从boost官网下载boost 1.53.0

2.执行 apt-get install build-essential (目的是为了安装好象gcc,g++等一些工具进行编译)
      注:我是在root用户下进行的操作,普通用户执行 sudo apt-get install build-essential 


3.解压boost库到/usr/share/
      將下载好的boost 1.53.0.tar.gz(我用的是这个版本) 复制到/usr/share/ 目录下,然后解压,会自动生成一个boost_1_53_0这个文件夹(或者用命令解压,这里就不提了,可以自己去查解压命令)

4.编译bjam(这个东东是用来编译boost库的)
      编译之前先做如下操作,由于这版本是编译msvc版的bjam,我们把它改成gcc版的bjam......
      进入boost1.53.0解压目录找到bootstrap.bat 用记事本打开,然后修改:
      第13行將call .\build.bat > ..\..\..\bjam.log 改为call .\build.bat gcc > ..\..\..\bjam.log
     第33行將set toolset=msvc改为set toolset=gcc
     保存退出,然后双击 bootstrap.sh在终端中执行,稍等片刻会在boost_1_53_0中生成bjam

     或者直接生成bjam。在terminal中输入

                      ./bootstrap.sh

5.编译boost库: 在终端中进入/usr/share/boost_1_53_0目录
     在终端中输入 cd /usr/share/boost_1_53_10
     然后我在root用户下输入 ./bjam toolset=gcc --layout=tagged --build-type=complete stage 进行完全编译
     (普通用户的话执行: sudo ./bjam toolset=gcc --layout=tagged --build-type=complete stage )
      经过漫长的等 待,命令执行完毕后,将会生成所有版本的库,并存储在:
         /usr/share/boost_1_53_0/stage 下
6.boost 已经编译好了,下面调用boost库
    1.在/usr/include/下生成一个boost库的include文件夹连接:
       ln -s /usr/share/boos t_1_53_0/boost /usr/include/boost
    2.在/usr/lib/ 下生成所有boost编译出的lib库文件的对应连接 切换到stage目录下,执行
       find $PWD/lib/*.* -type f -exec ln -s {} /usr/lib/ \;
       (普通用户执行: sudo find $PWD/lib/*.* -type f -exec ln -s {} /usr/lib/ \; )

添加环境变量(刚改完要重启或者注销一下来更新刚修改过的环境变量)
   两种方法:
   (1)修改/etc/profie文件 末尾添加
           export BOOST_INCLUDE=/usr/local/include/boost-1_52
           export BOOST_LIB=/usr/local/lib
   (2)在/etc/profile.d/ 中新建一个shell文件boost.sh
            #!/bin/sh 
            export BOOST_INCLUDE=/usr/local/include/boost-1_52
            export BOOST_LIB=/usr/local/lib

测试:
test.cpp

#include<boost/lexical_cast.hpp>
#include <iostream>
int main()
{
       using boost::lexical_cast;
       int a =lexical_cast<int>("123");
       double b =lexical_cast<double>("123.12");
       std::cout<<a<<std::endl;
       std::cout<<b<<std::endl;
       return 0;
}

执行完以上操作就算ok了



=================================【转载】============================================
http://www.cppfun.com/2012/03/31/codeblocks-with-boost-under-linux.html

Set up a Code::Blocks global variable for Boost
global variable

global variable

This step only needs to be performed once, after whichthe globalvariable you’ve created will be available for anyproject.

  • Open the Settings menu and select “Global variables…”
  • Click the “New” button next to the Current variable list,specify a name like “boost”, and hit OK
  • In the “base” field of the Builtin fields section, browse forthe base of your Boost location — the path you specified in the–prefix option of the build command(*mandatory)
  • In the “include” field, browse for the header files location —the path can same with the “base” or like “/usr/local/include” or/usr/include.
  • In the “lib” field, browse for the “stage/lib” subfolder ofyour Boost installation — it should be the path in the “base” fieldwith “/stage/lib” tacked on. (This is the folder that containseither multiple lib*.aor *.lib files.)[maybe a path like "/usr/local/lib" or "/usr/lib"].
  • Hit the Close button to save your global variable
Add Boost search directories to your project
search directories - compiler

search directories - compiler

search directories - linker

search directories - linker

  • Right-click your project’s name in the Projects section of theManagement window and select “Build options…”
  • Highlight the root of your project in the tree on the left sideof the Project build options window
  • Select the “Search directories” tab
  • With the “Compiler” subtab selected, click the Add button,enter “$(#boost.include)” (without thequotes), and hit OK
  • With the “Linker” subtab selected, click the Add button, enter“$(#boost.lib)”(without the quotes), and hit OK
Include Boost headers and link with Boost libraries

Your project is now ready to use the Boost libraries. For eachlibrary you want to use, do the following:

  • #include<boost/*.hpp> in yoursource file
  • In your project’s build options, highlight the root of yourproject, select the “Linker settings” tab, and add“boost_*” to your Link libraries[or another way under"Other link options" something like "-lboost_system -lboost_thread-..."]
Add Other libraries into Code::Blocks

The same way like boost, just do as above steps, enjoy.


<think>好的,我现在需要帮助用户解决在编译Gazebo和camera_models时遇到的Boost文件系统相关的链接错误。用户提到了未定义的引用,这通常意味着链接器找不到Boost库的相关符号。我需要逐步分析可能的原因,并提供相应的解决方案。 首先,用户的问题是关于编译错误,特别是与Boost文件系统相关的未定义引用。我需要回忆常见的导致这类错误的原因。首先想到的是Boost库没有被正确安装或者编译器找不到库的位置。另外,可能是在编译时没有正确链接Boost的文件系统库。 接下来,我需要考虑系统环境。用户提到的是Gazebo和camera_models,这两个项目可能依赖特定版本的Boost。可能用户安装Boost版本与项目所需的版本不兼容,或者安装路径不在默认搜索路径中。 然后,我需要检查用户是否在编译命令中正确指定了Boost库的链接选项。通常,使用Boost的文件系统需要添加链接选项`-lboost_filesystem`,有时还需要`-lboost_system`。此外,如果Boost安装在非标准位置,可能需要使用`-L`指定库路径,以及`-I`指定头文件路径。 另一个可能性是用户安装了多个版本的Boost,导致链接器使用了错误的版本。需要检查系统中安装Boost版本,并确保编译时指向正确的版本。 此外,动态库和静态库的问题也可能导致链接错误。比如,如果项目需要静态链接Boost库,而系统默认使用的是动态库,可能需要显式指定静态库。 最后,用户可能缺少某些开发包,比如libboost-filesystem-dev,需要确认是否已安装这些包。 现在,我需要将这些思考整理成一个结构化的回答,分步骤指导用户检查安装、确认链接选项、检查版本兼容性、处理多个版本、以及可能的静态库链接。同时,生成相关问题,帮助用户进一步排查其他可能性。 需要确保回答中使用正确的中文术语,并且LaTeX公式的格式符合要求,但用户的问题中没有涉及数学公式,可能不需要使用。但按照系统指令,所有行内数学表达式需要用$...$,如果有的话。不过在这个问题中可能不需要,所以可以忽略。 最后,检查是否遗漏了任何可能导致链接错误的常见原因,比如拼写错误、环境变量设置等,并确保引用相关的错误信息,如用户提供的引用[^1]中的路径错误。</think>### 编译Gazebo和camera_models时Boost文件系统未定义引用的解决方案 #### 1. **确认Boost安装与版本** - **安装完整性**: 确保系统中安装了`libboost-filesystem-dev`和`libboost-system-dev`开发包。例如在Ubuntu/Debian中运行: ```bash sudo apt-get install libboost-filesystem-dev libboost-system-dev ``` - **版本兼容性**: Gazebo和`camera_models`可能依赖特定Boost版本(如1.531.65等)。通过`apt list --installed | grep boost`查看已安装版本,若版本不匹配需手动编译对应版本[^1]。 #### 2. **检查编译链接参数** - **添加链接选项**: 在项目的CMakeLists.txt或编译命令中显式链接Boost文件系统库: ```cmake find_package(Boost REQUIRED COMPONENTS filesystem system) target_link_libraries(your_target ${Boost_LIBRARIES}) ``` - **手动指定路径**: 若Boost安装在非标准路径(如`/usr/local/Cellar/boost/1.53.0`),需在编译命令中添加: ```bash -I/usr/local/Cellar/boost/1.53.0/include -L/usr/local/Cellar/boost/1.53.0/lib -lboost_filesystem -lboost_system ``` #### 3. **处理多版本冲突** - **符号链接覆盖**: 若系统存在多个Boost版本,可通过`update-alternatives`设置默认版本: ```bash sudo update-alternatives --config libboost_filesystem.so ``` - **强制指定静态库**: 如果动态库存在问题,尝试链接静态库: ```cmake target_link_libraries(your_target /path/to/libboost_filesystem.a) ``` #### 4. **验证环境变量** - **设置`LD_LIBRARY_PATH`**: 确保运行时能找到动态库: ```bash export LD_LIBRARY_PATH=/usr/local/Cellar/boost/1.53.0/lib:$LD_LIBRARY_PATH ``` #### 5. **重新生成构建系统** - **清理缓存**: 删除`build/`目录并重新运行`cmake`,避免旧配置残留: ```bash rm -rf build/ && mkdir build && cd build && cmake .. && make ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值