乐闻世界logo
搜索文章和话题

How to install Boost on Ubuntu

1个答案

1

In Ubuntu, installing the Boost library can be accomplished through several methods. Here, I will outline two primary approaches: installing a precompiled version via Ubuntu's package manager APT, and compiling from source.

Method 1: Using APT Package Manager

  1. Update your package list First, open the terminal and run the following command to ensure your package list is current:
    shell

sudo apt update

shell
2. **Install the Boost library** Next, install the libboost-all-dev package, which includes most of the available Boost library modules. Run the following command:

sudo apt install libboost-all-dev

shell
This command installs the Boost version available in Ubuntu's repositories. If you require a specific Boost version, you may need to add a PPA or compile from source. ### Method 2: Compiling from Source If the Boost version you need is unavailable in the APT repositories or if you require a custom installation (e.g., specifying compiler options), compiling from source is often preferable. 1. **Download the Boost source code** Visit the Boost official website at [Boost website](https://www.boost.org/) or use the `wget` command to download directly. For example, download Boost 1.75.0:

wget https://boostorg.jfrog.io/artifactory/main/release/1.75.0/source/boost_1_75_0.tar.bz2

shell
2. **Extract the source files** Use the `tar` command to extract the downloaded file:

tar --bzip2 -xf boost_1_75_0.tar.bz2

shell
3. **Prepare for compilation** Navigate into the extracted directory and run the `bootstrap.sh` script to prepare for compilation:

cd boost_1_75_0 ./bootstrap.sh

shell
4. **Compile and Install** Use the `./b2` command to initiate compilation. You can specify options such as `--prefix=/usr/local` to set the installation path or `threading=multi` to enable multi-threading support.

./b2 sudo ./b2 install

shell
After installation, you may need to configure environment variables like `LD_LIBRARY_PATH` or `BOOST_ROOT` to ensure other programs can locate the Boost library. ### Verify Installation Regardless of the method used, verify the installation by compiling and running a small Boost program. For example, create a simple C++ file that utilizes Boost features, then compile and execute it. #### Example Program ```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) << " "); }

Compile the above program:

shell
g++ -o test_boost test_boost.cpp -lboost_system -lboost_filesystem

Run and inspect the output to confirm successful execution.

By following these steps, you can successfully install and validate the Boost library on Ubuntu.

2024年7月22日 20:48 回复

你的答案