Compile and use boost library under Linux

     The Boost library is a portable, source code C++ library that serves as a fallback to the standard library and is one of the development engines for the C++ standardization process. The Boost library was initiated by members of the C++ Standards Committee's Library Working Group, and some of it is expected to become the next generation of the C++ Standard Library content. It has a great influence in the C++ community and is an uncompromising "quasi" standard library. Boost, because of its emphasis on cross-platform, emphasis on standard C++, is not platform-independent. Most of the boost library functions only need to include the corresponding header files, and a few (such as regular expression libraries, file system libraries, etc.) need to link libraries.

        How to compile all modules that use Boost under Linux .

1. Go to the Boost official website to download the latest Boost version , I downloaded the boost_1_56_0 version and unzip it .

2. Enter the decompressed directory : cd boost_1_56_0, execute the following command :

 

$ ./bootstrap.sh --prefix=path/to/installation/prefix

 

The value of prefix is ​​the path where you want to install boost . If this parameter is not enabled, it will be installed in /usr/local by default . I installed it in the /home/xzz/boost_1_56_0 directory :

 

$ ./bootstrap.sh --prefix=/home/johnchen/boost_1_56_0

 

Note: Do not use ~ to indicate the home directory , the compilation script does not recognize ~, and a new directory named '~' will be created at the current time . 

Then execute :

 

$ ./b2 install

 

This command installs the boost header file folder include/ in the directory defined by prefix , and will compile all boost modules , and put the compiled library folder lib/ in the directory defined by prefix . All if successful If compiled , the prefix directory is /home/johnchen/boost_1_56_0 directory should contain include/ and lib/ folders .

3. Test
//test-boost.cpp
#include <stdio.h>
#include <iostream>  
#include <boost/filesystem.hpp>

using namespace boost::filesystem;
int main(int argc, char *argv[])  
{  
	    if (argc < 2)
	    {  
        std::cout << "Usage: tut1 path\n";  
        return 1;  
    	}  
    	
 		std::cout << argv[1] << " " << file_size(argv[1]) << std::endl;  
    return 0;  

}
编译 命令
g ++ test-boost.cpp -o test -I / home / johnchen / boost_1_56_0 / include -L / home / johnchen / boost_1_56_0 / lib -lboost_system -lboost_filesystem
Here the boost_filesystem library depends on the boost_system library, so boost_filesystem should be written later, otherwise a symbol parsing error may occur.
Execute ./test, this time there will be a problem:
 
./test: error while loading shared libraries: libboost_system.so.1.56 .0: cannot open shared object file: No such file or directory
The reason is that when there are dynamic libraries and static libraries, gcc uses dynamic libraries first, but the default search paths for dynamic libraries under linux are /lib, /usr/lib, / usr/local/lib. So there is an error when the program is running. The solution is to copy the dynamic library to the search path of the dynamic library. You can also specify a static library and use the following command to compile.
g ++ test-boost.cpp -o test -I / home / johnchen / boost_1_56_0 / include -L / home / johnchen / boost_1_56_0 / lib -l: libboost_system.a -l: libboost_filesystem.a

//thread demo
#include <boost/thread.hpp>
#include <iostream>

void task1() {
    // do stuff
    std::cout << "This is task1!" << std::endl;
}

void task2() {
    // do stuff
    std::cout << "This is task2!" << std::endl;
}

int main (int argc, char ** argv) {
    using namespace boost;
    thread thread_1 = thread(task1);
    thread thread_2 = thread(task2);
       
    //sleep
    boost::this_thread::sleep(boost::posix_time::seconds(2));

    // do other stuff
    thread_2.join();
    thread_1.join();
    return 0;
}
g++ boost2.cpp -o test2 -I /home/johnchen/boost_1_56_0/include -L /home/johnchen/boost_1_56_0/lib -lboost_system -lboost_thread
After compiling, an example executable file will appear, you can run: ./example , the result Display:
This is task2! This is
task1!
Maybe you will get this error at runtime: error while loading shared libraries: libboost_system.so.1.56.0: cannot open shared object file: No such file or directory
The dynamic library used is not in the default environment variable, you can use the following command to add:
export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH
After adding, execute ./test2
You can also use the specified static library to link to generate object files. The command is as follows:
g++ boost2.cpp -o test2 -I /home/johnchen/boost_1_56_0/include -L /home/johnchen/boost_1_56_0/lib -l:libboost_system.a -l:libboost_date_time.a -l:libboost_thread.a
found to be linking time went wrong.
/home/johnchen/boost_1_56_0/lib/libboost_thread.a(thread.o): In function `boost::detail::(anonymous namespace)::create_current_thread_tls_key()':
thread.cpp:(.text+0x13): undefined Reference to `pthread_key_create'
found that clock_gettime is in the real time library (real time), and an error was reported because the library was not linked when linking.
/home/johnchen/boost_1_56_0/lib/libboost_thread.a(thread.o): In function `boost::this_thread::no_interruption_point::hiden::sleep_until(timespec const&)':
thread.cpp:(.text+0x7ab) : undefined reference to `clock_gettime'
g++ boost2.cpp -o test2 -I /home/johnchen/boost_1_56_0/include -L /home/johnchen/boost_1_56_0/lib -l:libboost_system.a -l:libboost_date_time.a -l:libboost_thread.a -lrt
found in Error linking. After searching, it is found that clock_gettime is in the real time library (real time), and an error is reported because this library is not linked when linking.
Solution:
      Just add -lrt when we compile.
     
-lz -lrt -lm -lc what library is
libz compression library (Z)
librt real time library (real time)
libm math library (math)
libc standard C library (C lib)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325505314&siteId=291194637