Windows environment cmake references boost library

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Boost is a general term for some C++ libraries that provide extensions to the C++ language standard library. The Boost library is a portable C++ library that provides source code. As a backup of the standard library, it is one of the development engines of the C++ standardization process. It is a general term for some C++ libraries that provide extensions to the C++ language standard library.
The Boost library was initiated by members of the C++ Standards Committee Library Working Group, and some of its content is expected to become the next generation of C++ standard library content. It has a great influence in the C++ community and is an out-and-out "quasi" standard library.

The use of the boost library in the linux environment is very simple, especially for distributions such as ubuntu and cenntos. The software warehouse has a special deb or rpm package. Here we only mention one sentence for the linux environment and will not go into details.


1. Download the source code

Go directly to the official website to download the source code, just select the version you need.
Boost official website
This needs to click Download to find the required version. The official website not only has the source code of Boost, but also Document and Example for reference. If you don't want to bother to find it, here is a collection of versions, just choose the right one.
Version collection
insert image description here
Select the required version, such as 1.71.0 (the default version of Ubuntu-20.04)
insert image description here

Click source to enter, and you can find zip or tar. It is recommended to download zip in the windows environment.
insert image description here

.json里面是验证串,你首先必须保证下载下来的zip是完整的。

2. Preparation before compilation

Our method here is to compile through gcc, and then directly reference through cmake; another method is to reference from Virtual Studio (not in this article). Since the two methods are quite different, I only talk about my environment.

1. Operating system

I am using Windows11, theoretically Windows10 is also possible, but I do not have a Win10 environment, I hope you can test it yourself. Windows7 should also work, but I can't find a Win7 environment around me at all, and I don't want to install a virtual machine or something. I hope everyone can keep it as consistent as possible with my environment.

2.gcc environment

The Clion-2022.2.4 environment I installed comes with this tool chain. You can follow my example to install Clion, and then configure the environment variables. Of course, it is also possible to install the environment by yourself, both mingw and cygwin; I won’t talk about how to install it here, just search if you don’t know it.
The following is a screenshot of my environment:
insert image description here
This is the mingw environment built into Clion, and only need to configure an environment variable.
insert image description here

3. Create an installation directory

It is best to prepare a target directory in advance to store the compiled library.
D:\Work\C++_libs\boost_1_71_0\boost_build

3. Start compiling

1. Unzip the source code

Unzip the source code to a storage space not less than 5G to store the intermediate files generated by compilation. Here we take version 1.71.0 as an example:
insert image description here
this is the directory structure before compilation, open a command line and enter this directory. I use Terminal, you can use PowerShell, but I don't guarantee that CMD will work too.

2. Start compiling

Advance to the root directory of Boost (the one shown above), and execute the following commands in sequence:

cd ./tools/build
./bootstrap.bat
#等待执行完成

If no error is reported (generally no error is reported), a b2.exe program will be generated in the directory. The official statement is to add it into it 环境变量. Here we do not add environment variables and use full path navigation directly.
Remember the installation directory I defined above? D:\Work\C++_libs\boost_1_71_0\boost_build, this is my directory, you need to create a directory yourself, it is best not to have one 空格.
At this time, you retreat to the root directory of Boost (not tools/build), and execute the following command after completion:

#--prefix=<你的安装目录>
.\tools\build\b2.exe --prefix=D:\Work\C++_libs\boost_1_65_1\boost_build toolset=gcc install

Very good, it takes about ten minutes to compile, it depends mainly on you 配置.

4. Start using

If all goes well (and I have almost never reported errors), your library will be installed in the specified directory. Your directory structure is roughly equivalent to the following structure:
insert image description here
insert image description here
insert image description here
I use it here 静态库, you can also compile it 动态库, depending on your personal preference. This is not the point, the point is how to reference Boost in Clion. There are some differences between this and the reference in Ubuntu. To be precise, it is not as convenient as Ubuntu's direct installation.

1.Clion create project

You must use a version above C++11, so let's use C++11 here. Please see the configuration below:

cmake_minimum_required(VERSION 3.23)
project(Class28)

set(CMAKE_CXX_STANDARD 11)
set(BOOST_ROOT "D:\\Work\\C++_libs\\boost_1_71_0")    # 指定Boost库的根目录
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_ARCHITECTURE "-x64")    # 指定库架构为x64
find_package(Boost COMPONENTS atomic REQUIRED)    # 查找Boost库

include_directories(${Boost_INCLUDE_DIRS})    # 包含Boost库的头文件目录

MESSAGE(STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}")
MESSAGE(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}")
MESSAGE(STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}")

add_executable(Class28 main.cpp)
target_link_libraries(Class28 ${Boost_LIBRARIES})    # 链接Boost库

The compilation environment I use is x86_64, so I specified Boost_ARCHITECTURE that if you are in an x86 environment, theoretically the library you compile should be the best x86, instead of having two versions (x86 and x86_64) like me.

x86_64向下兼容x86,而x86不能运行x86_64编译出来的软件,这一点很重要!

If cmake succeeds, you should see the correct prompt like mine.
insert image description here

2. Differences in Boost versions

One more thing to mention here, the Boost library is very important to C++, and it is also developing forward. Different versions of the library may have different components. For example, the 1.82.0 I use has a json component, but there is no such component in 1.71.0, and an error will be reported if a direct reference is made. There are still many such problems, you can refer to the official documentation to see the situation of the components.

Here I give a relatively stupid solution: Take the json component as an example, if you directly quote it in CMakeLists.txt, cmake will directly report an error when building it, which will prevent you from entering the next step, that is, compiling.

find_package(Boost COMPONENTS json REQUIRED)    # 查找Boost库

insert image description here
This error means that this cannot be found 组件, let's go to the library to have a look. Comparing 1.71.0 and 1.82.0, it is found that 1.71.0 does not have this component. So what to do? There are actually many methods, the simplest is to use the third-party jsoncpp, which is how I use it.

不要过度依赖boost

3. Boost version adaptation

Sometimes programmers have an unavoidable problem. Maybe you want to use a new version of boost, but find that some things do not fit the old code. This method is also available. Of course, except for missing components, I have not encountered this situation.

#if BOOST_VERSION >= 108200
    cout << "1.82.0" << endl;
#include <boost/json.hpp>
#elif BOOST_VERSION >= 107100
    cout << "1.71.0" << endl;
#elif BOOST_VERSION >= 106510
    cout<<"1.65.1"<<endl;
#endif

BOOST_VERSION This macro is <boost/version.hpp>in it, and it must be included in each boost version. There are two main fields:
insert image description here
Version adaptation can be done by judging BOOST_VERSION.


Summarize

1. No mistakes were encountered in the whole process, and it was completed in one go.
2. If you have any questions or report an error, you can comment, and I will help you solve it after seeing it.

Guess you like

Origin blog.csdn.net/jiexijihe945/article/details/131303313