Introduction, installation and environment configuration of Boost


1. Introduction to Boost library

In 1998, Beman G. Dawes (one of the members of the C++ Standards Committee) initiated the initiative and established the Boost community, the purpose of which is to provide C++ programmers with free, peer-reviewed, portable, high-quality C++ source libraries .

The Boost library is an efficient C++ cross-platform development library. The official version of the Boost library provides a wealth of template classes and function interfaces, and implements common functions such as smart pointers, containers, and algorithms. The Boost library also supports a variety of operating systems and compiler environments, and provides detailed documentation and sample codes for users to quickly learn and use

It makes C++ programming more elegant, more dynamic, and more productive. Two-thirds of the C++11 standard comes from the boost library. The Boost library works well with the C++ standard library, so the Boost library is suitable for eventual standardization.

2. Boost installation and compilation

(1) Download and decompress

Official website address:

http://www.boost.org/

insert image description here

insert image description here

If you use the official website, you may not be able to download it, because access is restricted in China. Basically, we do these things like this. You can use the following:

Boost C++ Library - Browse /boost-binaries at SourceForge.net

For the sake of stability, I did not choose the latest version

insert image description here

insert image description here

After downloading, unzip it to your favorite path

After decompression, you can see the whole structure:

superstructure

#存放配置脚本和说明文件
├── boost                 # 最重要的目录,90%以上的Boost程序库源码都在这里
├── doc                   # HTML 格式的文档,也可以生成PDF格式文档
├── libs                  # 所有组件的示例、测试、编译代码和说明文档
├── more                  # 库作者相关的文档
├── status                # 用于测试Boost库的各个组件
└── tools                 # b2、quickbook等自带工具

Boost directory structure: (In most cases, we only need to care about the boost subdirectory, which stores the library code we want to use in the form of header files)

├── accumulators          # 累加器库
├── algorithm             # 算法库
├── align                 # 内存对齐库
├── archive               # 序列化库
├── asio                  # 异步并发库
├── assert                # 断言库
├── assign                # 赋值初始化库
├── atomic                # 原子操作库
├── beast				  # 高级网络通信库
├── bimap                 # 双向关联数组
├── bind                  # bind表达式
├── chrono                # 时间处理库
...						  # 其他库
├── python                # python库   (把C++类和函数映射到Python之中)
├── yap                   # 表达式模板库
...

(2) Compile the static library

Most components of the boost library do not need to be compiled and linked. We can directly include the header files in our own source code. For example, if you use boost::timer, you only need to write the following statement (in the ide environment, you need to configure it first environment, mentioned in step 3)

#include <boost/timer.hpp>
using namespace boost

1. The header file of the Boost library is different from the header file (*.h) we usually use or the header file of the C++ standard library (no suffix), which is the uniqueness of Boost. It puts the declaration and implementation of the C++ class in one file instead of two files, namely .h+.cpp, so the suffix of the file is .hpp.
 
2. It needs to be explained here. After getting the compressed package and decompressing it at the beginning, I thought that it could be directly copied to the project directory for inclusion and reference like the GDAL library. However, it was found that the referenced file reported an error and could not be opened. Therefore, the boost library cannot be directly copied and used. For some files, it is still necessary to compile a static library and then reference it. The compilation here is different from the installation of GDAL and OpenCV. The compilation and installation here only generates a static library under the corresponding file.

Step 1: After downloading and decompressing, there will be a bootstrap.bat file, double-click to execute it, and a b2.exe file will be generated; if not, enter the cmd, and then enter this directory to enter bootstrap.bat to run:

insert image description here

Step 2: Double-click b2.exe to run: wait for the program to be compiled, and two folders, bin.v2 and stage, will be generated in the directory. Among them, bin.v2 is the generated intermediate file with a size of about 2.4G, which can be deleted directly . The lib file is generated under the stage. The file is as follows:

insert image description here

illustrate:

Here, it is compiled by double-clicking b2.exe, and the lib folder is generated under the stage by default, which only contains static libraries. If you want to generate a dll dynamic library, you can view the parameter description with ./b2 --help, and then you need to enter the corresponding compilation command such as:

./b2 toolset=msvc-14.1 link=static runtime-link=shared threading=multi variant=debug

in

  • toolset:Specify the compiler, such as minGW, msvc, etc. are optional.

    vs2008 : msvc-9.0,

    vs2010 : msvc-10.0,

    VS2012、VS2013:msvc-12.0

    vs2017 : msvc-14.1,

    vs2015 : msvc-14.0,

    linux :gcc

  • link:Generate dynamic link library/static link library. The shared method is required to generate a dynamic link library, and the static method is required to generate a static link library. Generally, the boost library may be compiled statically, because it will be cumbersome to carry the boost dll in the final release program.

  • runtime-link:Dynamically/statically link the runtime library. There are also shared and static two ways, mark how to connect the C++ runtime library, use static if it is included, and use dynamic when using the system runtime library.

  • threading:Single/multithreaded compilation. Generally, multi-threaded programs are written, of course, the multi method must be specified; if you need to write a single-threaded program, you also need to compile a single-threaded library, and you can use the single method.

  • variant:Compile the debug/release version. Generally, the debug version of the program corresponds to the debug version of the library, so both are compiled.

So if you need to generate a dll, you can use the link=shared method to generate it. Refer to Boost Compilation and Use-Knowledge

Build file naming instructions:

  • The one starting with "lib" is the "link=static" version (static link library version, without dll), while the one directly starting with "boost" is the "link=shared" version (dynamic link library version, including lib and dll).
  • All libraries have a "boost" prefix.
  • This is followed by the boost library name (such as the date_time library).
  • Then comes the compiler version, separated from the library name with "-" instead of underscore "_" (eg -vc120).
  • The one with "mt" is the "threading=multi" version, and the one without it is the "threading=single" version.
  • The one with "s" is the "runtime-link=static" version, and the one without it is the "runtime-link=shared" version.
  • The one with "gd" is the debug version, and the one without it is the release version.
  • All libraries end with the version number of the boost library (such as 1_56, where "." is replaced by an underscore "_")

3. Configure the VS environment

1. Project (right click) --> Properties

2. Click the VC++ directory:

  • Include directory: Add your own boost directory, for example: E:\Mingw\boost_1_78_0
  • Library directory: add your own static library under the stage after boost compilation, for example: E:\Mingw\boost_1_78_0\stage\lib

3. Click Linker --> General --> Additional Library Directory: Also add a static library directory, for example: E:\Mingw\boost_1_78_0\stage\lib

4. Complete, test, and put the test code at the end

insert image description here

Include directory:

insert image description here

Library directory:

insert image description here

insert image description here

Linker --> Additional Library Directories:

insert image description here

4. Configuration of other environments (vscode, DevC++)

(1) Configure the environment for using the boost library in DEVC++

In compiler options:

insert image description here

In the directory-C++ include file, set the address of the boost library, pay attention to the upper layer address of the boost directory inside.

This setting is an include file, which can be considered as a header file. In addition, we may also need to use a static library, so we also need to set the path of the static library file.

image-20210703101638632

Through the above two steps, we have set up the use environment under DEVC++.

(2) Use the boost library in vscode

It should have configured the included directory here, but I don’t know why it keeps reporting fatal error: boost/config.hpp: No such file or directory, readers can try

"includePath": [
                "${workspaceFolder}/**",
                "E:/wenjian/cs/code/boost_1_76_0_2/boost_1_76_0/"
            ]

So, there is no way, I copied the boost directory to D:\MinGW\include, which is the standard include file of the gcc compiler, and found that it can run normally.

image-20210703152732456

image-20210703152809357

Running the build task succeeded

5. The installation process of the boost library under Linux

1. Download the latest boost library

2. Unzip the boost library in an appropriate location. It is recommended to unzip the boost library to /usr/local/: (It will not work if the installation finds that it is not copied to this directory)

3. Execute the following command directly in the directory after Boost decompression: ./bootstrap.sh;Execute again./b2 install

The first command bootstrap.sh is the configuration work before compilation, and the second command starts to actually compile and install Boost.
If no additional options are specified as above, Boost will compile the release version of the library file, install the header file into "/usr/local/include", and install the library file into "/usr/local/lib".

4. You can add parameters to specify our compilation operation

  • Compile fully:./bootstrap --buildtype=complete install

  • Custom compilation:

    ./b2 --show-libraries: You can view all libraries that must be compiled before they can be used.

    ./b2 --with-date_time --buildtype=complete install: On the basis of the full compilation command, use the --with or --without option to turn on or off the compilation of a certain library, and execute the above command to compile and install the date_time library

    It takes time and effort to fully compile Boost, and not all of these libraries will be used during the development process. Therefore, Boost allows users to choose the libraries they want to compile.

5. Compilation verification: also refer to the appendix code

Appendix: Test Code

#include <iostream>
#include <boost/version.hpp>
#include <boost/config.hpp>
 
using namespace std;
int main()
{
    
    
	cout<<BOOST_VERSION<<endl;
	cout<<BOOST_LIB_VERSION<<endl;
	cout<<BOOST_PLATFORM<<endl;
	cout<<BOOST_COMPILER<<endl;
	cout<<BOOST_STDLIB<<endl;
	getchar();
	return 0;
}

insert image description here

There are two macros in the header file <boost/version.hpp>, which define the version number of the currently used boost program

<boost/config.hpp>There are three header files BOOST_STDLIB, BOOST_PLATFORMand BOOST_COMPILER, respectively define the current operating system, compiler and standard library

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
 
#include <boost/timer.hpp>
#include <boost/progress.hpp>
 
#include <libs/date_time/src/gregorian/greg_names.hpp>
#include <libs/date_time/src/gregorian/greg_month.cpp>
#include <libs/date_time/src/gregorian/gregorian_types.cpp>
 
#include <boost/date_time/posix_time/posix_time.hpp>
 
using namespace boost;
 
int main()
{
    
    
	boost::timer t;
 
	boost::progress_display pd(100);
 
	for (int i = 0; i < 100; ++i) //进度条
	{
    
    
		++pd;
	}
 
	boost::gregorian::date dt(2009, 12, 8); //date_time 库
	assert(dt.year() == 2009);
	assert(dt.day() == 8);
	boost::gregorian::date::ymd_type ymd = dt.year_month_day();
	std::cout<<"\n"<<ymd.year<<"/"<<ymd.month<<"/"<<ymd.day<<" the day is "
		<<dt.day_of_year() <<" days of this year"<< std::endl;
 
	std::cout << boost::gregorian::to_iso_extended_string(dt) << std::endl; //转换为其他格式
	std::cout << boost::gregorian::to_iso_string(dt) << std::endl;
	std::cout << boost::gregorian::to_simple_string(dt) << std::endl<<std::endl;
 
	//对数组排序操作
	std::vector<int> test_vc(100);
	std::vector<int>::iterator beg_it = test_vc.begin();
	std::vector<int>::iterator end_it = test_vc.end();
	std::srand(std::time(NULL));
 
	std::for_each(beg_it, end_it, [](int& n){
    
    n = rand(); });
	std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
	std::cout << std::endl << std::endl;
	std::sort(beg_it, end_it, std::greater<int>());
	std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
	std::cout << std::endl<<std::endl;
 
	boost::posix_time::ptime pt(boost::gregorian::date(2005, 2, 6));
 
	std::cout << t.elapsed() << "s" << std::endl; //程序运行时间
 
	system("pause");
 
	return 0;
}

insert image description here


Respect originality, refer to the article:

Installation and use of Boost library under Windows

Environment configuration of boost library on visual studio, DevC++ and vscode

Guess you like

Origin blog.csdn.net/qq_45491628/article/details/131367797