[Original] How to Use boost to download, install, and compile configurations (including windows, Linux, and ARM Linux)

Source: Internet
Author: User
Tags vc9

-- Updated on February 19,. At present, I have a new understanding of boost installation and use. Therefore, I will make significant changes to the article I wrote two years ago, and the online reposted version is rampant, please refer to this document.

-- Updated on, and added the compilation and configuration methods in ARM Linux.

Theoretically, this article applies to various boost versions, especially the latest version 1.48.0. It applies to various C ++ compilers, such as vc6.0 (not supported by some libraries), vs2003, vs2005, vs2008, vs2010, v2012, GCC, arm-Linux-GCC, and C ++ builder.

First, we will summarize the Windows system.

1. Download

First, go to the boost official homepage. I usually use SVN now, which makes it easy to update and build, instead of downloading the new installation package and rebuild each time.

Ii. Installation

If the downloaded installation package is used, extract the boost installation package to the local directory, for example, E: \ SDK \ boost. If SVN is used, you can also run the boost code checkout to this directory. Because some classes in boost need to be compiled into libraries for use, we also need to prepare a dedicated boost compilation auxiliary tool bjam. The bootstrap.batcommand under the root directory is executed in the command prompt ().exe.pdf the compiled bjam.exe will be automatically copied to the directory (bjam must be in the same directory as the boost-build.jam ).

Iii. Compilation

Next is the most important compilation step. Open the command prompt (cmd.exe) window and run bjam. You can use the -- help parameter to view the Command help. The command line parameters of bjam are described in detail here, because it is very important. First, it involves the establishment of the programming environment. You need to select the appropriate command line parameters based on your specific use environment in the future. Second, it affects your hard disk space, if you have more than two ides at the same time (such as vc6 and vc9 coexist) and boost is required, you can calculate the number of hard disks used ...... Although hard disk space is not a problem today, as I do, there should be a lot of people who advocate reasonable use of resources and are not used to extravagance and waste and advocate frugal children's shoes, therefore, you do not need to compile the configuration and libraries you do not need. Based on the above two factors, the bjam command I use is as follows: Bjam stage -- toolset = msvc-9.0 -- without-graph -- without-graph_parallel -- without-math -- without-MPI -- without-Python -- without-serialization -- without-wave -- stagedir = "E: \ SDK \ boost \ bin \ vc9 "link = static runtime-link = shared runtime-link = static threading = multi debug release

 

The following describes the meaning of each parameter in detail:

Stage/install:Stage indicates that only the library (DLL and Lib) is generated. Install also generates the include directory containing the header file. I recommend using stage, because the include directory generated by install is actually the boost directory after the boost installation package is decompressed (E: \ SDK \ boost, only a few non-HPP files are smaller than the include directory, so they can be used directly, and different ides can use the same pullover file, which saves Compilation Time, it also saves hard disk space. Toolset:Specify the compiler, optional such as Borland, GCC, msvc (vc6), msvc-9.0 (vs2008) and so on. Without/:Select which libraries are not compiled/compiled. I don't need any libraries such as Python and MPI, so I want to exclude them. There are also static lib compiled by libraries such as wave, graph, math, RegEx, test, program_options, serialization, and signals, which can be dropped without any need. This can be selected based on your needs. By default, all files are compiled. However, if you select to compile python, you must download and install python at http://www.python.org. The command to view the boost include library is Bjam -- show-libraries. Stagedir/Prefix:Stagedir is used for stage, and prefix is used for install to compile the path of the generated file. We recommend that you specify different directories for different ides. For example, vs2008 corresponds to E: \ SDK \ boost \ bin \ vc9, and vc6 corresponds to E: \ SDK \ boost \ bin \ vc6; otherwise, all are generated under a directory, which is difficult to manage. If the install parameter is used, the header file directory will also be generated. vc9 corresponds to E: \ SDK \ boost \ bin \ vc9 \ include \ boost-1_46 \ boost, vc6 is similar (the path here is cumbersome, and it is better to use stage ). Build-Dir:Path of the intermediate file generated by compilation. This is useless here. By default, the directory name is bin in the root directory (E: \ SDK \ boost. v2. After compilation, you can delete all the directories (useless), so you do not need to set them. Link:Generate a dynamic or static Link Library. To generate a dynamic link library, you must use the shared method. To generate a static link library, you must use the static method. Generally, the boost library may be compiled in static mode, because the DLL with boost will be cumbersome for the final release program. Runtime-link:Dynamic/static links to the C/C ++ Runtime Library. There are also two methods: shared and static. In this way, runtime-link and link can generate four combination methods, and each user can choose to compile based on their own needs. Generally, if Link is only static, you only need to compile two combinations, that is, link = static runtime-link = shared and link = static runtime-link = static, I generally compile these two combinations. Threading:Single/multi-thread compilation. Generally, multiple threads 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. You can use the single method. Debug/release:Compile the debug/release version. It is usually the debug version of the library corresponding to the debug version of the program, so both are compiled. After I have compiled static links and dynamic links in the above methods, the entire E: \ SDK \ boost directory is 1.28 GB. If you do not plan to upgrade the boost version in the future, you can compile the intermediate file bin. delete the V2 directory, so that the entire directory (including the package decompression file and the compiled library file) will be reduced to less than 800 mb. If runtime-link is selected, the entire directory is only 600 mb. In fact, after compilation, in addition to the boost and bin directories, other directories and files can be deleted, which can free up MB of space. However, I have studied it again. In fact, the libs directory is also very useful. It provides examples for all boost classes and can be used as a reference; in addition, the doc directory is a complete boost help document. Of course, it is best not to delete it. The other directories and files add up to dozens of megabytes. Leave a new path for them simply. A complete and perfect boost directory was born.

Note that if you use vs2008 to compile boost (other versions of VC are not tested, there may be similar issues ), if you add the _ bind_to_current_vclibs_version pre-defined macro to force the use of the latest Windows CRT library to call the boost main program, then your boost also needs to add this pre-defined macro for compilation, otherwise, the program prompts "running on some machines" because the application configuration is incorrect because the CRT library version used by each module is inconsistent ", for this question, please refer to my other article: [original] solve the problem of running the program compiled by vs2008 on some machines. The prompt "because the application configuration is incorrect, failed to start the application. To add this predefined macro, open boost \ config \ compiler \ VisualC. HPP under the boost installation root directory and add the following at the top:

// Added by Terry, 2011/4/19, force the linker to use the latest version of CRT/mfc/ATL DLL
# Ifndef _ bind_to_current_vclibs_version
# DEFINE _ bind_to_current_vclibs_version 1
# Endif/_ bind_to_current_vclibs_version

 

This method modifies the boost source code, which is not what I want, but does not find a better method. If someone knows that they want to leave a message to tell me.

If you don't want to know so much about it, you can use the following command to make it easy: Bjam -- toolset = msvc-9.0 -- Build-type = complete directly specifies the compiler to compile in full mode, which can meet all future use cases, but the consequence is: 1. Takes up more than 3 GB of hard disk space
2. Several hours of Compilation Time
3. the header file and library file are stored in c: \ boost (I am very disgusted)

4. Many generated files can never be used.

Iv. Configuration

Include Directory: e: \ SDK \ boostlibrary Directory: e: \ SDK \ boost \ bin \ vc9 \ Lib

Add it to the corresponding path of the IDE.

V. Use

Example: # Include <boost \ thread. HPP> in this case, the library file is not included. The boost auto-Link Mechanism will automatically help us include the corresponding static Lib. That is to say, boost is linked in static mode by default, so our project property should also be set to multi-threaded (Debug ). If you want to use DLL Dynamic Link, you need to define the macro in advance: # Define boost_all_dyn_link. At this time, boost will also include the corresponding lib by default. If you do not want to use the auto-Link Mechanism provided by boost, or do not feel at ease with its automatic link (in fact, you do not have to worry about it), you can define the macro in advance: # Define boost_all_no_lib and use the following link: # Pragma comment (Lib, "boost_thread-vc90-mt-1_47.lib") or # Pragma comment (Lib, "boost_thread-vc90-mt.lib") the two Lib is actually the same, I really do not understand why each library needs to copy a copy during boost compilation, is it because the latter does not need to change the code after upgrading the boost version? There is also a useful macro: # Define boost_lib_diagnostic: it allows VC to output the boost Library and the link sequence of the program in the output window during compilation.

For details about the auto-Link Mechanism of boost, refer to the Code in boost \ config \ auto_link.hpp, which is easy to understand and worth learning.

Vi. Differences between compiling and configuration in Linux:

My Linux and Windows use the shared directory to map the E: \ SDK directory under windows to the/mnt/HGFS/SDK directory under Linux. So the root directory of my boost source file is/mnt/HGFS/SDK/Boost and CD. After compiling bjam:./Bootstrap. Sh, bjam is generated in the current directory. Then, compile boost: . /Bjam stage -- toolset = GCC -- with-date_time -- With-thread -- With-filesystem -- with-program_options -- stagedir = "/mnt/HGFS/SDK/Boost/bin/GCC" link = static Runtime -Link = static threading = multi debug release these parameters have the same meaning as in windows, only the compiler is changed to GCC, and other options are set as needed. The generated library file is in the bin/GCC/lib directory, which is the same as that compiled by vc9. If the boost version will not be upgraded in the future, you can delete all the intermediate files, including the bin. V2 directory and the tools/JAM/stage directory. For the use of the boost library, in order not to write makefile every time, the boost header file and library file path are carried, and to make cross-platform programs share a set of code, you can add the boost header file and library file path to the Linux environment variables. Open/etc/profile and add the following GCC environment variables: Cplus_include_path = $ cplus_include_path: "/mnt/HGFS/SDK/Boost"
Export cplus_include_path

LIBRARY_PATH = $ LIBRARY_PATH: "/mnt/HGFS/SDK/Boost/bin/GCC/lib"
Export LIBRARY_PATH

In this way, you can directly use the boost header file and library file, and share a set of code with windows, which is very convenient.

VII. ArmDifferences between compilation and configuration in Linux:

Edit the project-config.jam and modify the using gcc line. Changed:
Using GCC: arm: Arm-Linux-GCC;

Note that spaces cannot be deleted and must be retained; arm-Linux-GCC can be replaced with your own compiler. Start compiling with bjam:

./bjam stage

Note that the compilation of arm cannot contain many parameters as in Windows and Linux; otherwise, the compilation fails; on the Internet, we can see that others can use the -- with or -- without attribute. I didn't try it, yes, but other parameters may not be supported. If you are interested, try again.

8. Several popular online articles for reference:

Vernacular C ++ installation of Boost: http://www.d2school.com/bhcpp_book/2_5.phpWindows vc6 compilation and installation of boost Library: v1.35.0 Visual Studio 2008 Compilation Guide: unzip 43_0 in arm-Linux compile: http://blog.csdn.net/yujun_huoxu/article/details/7913135
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.