Cross-Compiling Boost
When cross-compiling, be sure to configure sysroot. Everyone online claims they got it built without configuring it — they don’t even pretend. And no one knows who copied whose article.
Getting Started
Download the latest Boost archive from the official website. I tested it with the Yocto cross-compilation toolchain and 1.86.0 works fine.
Unpack it and enter the directory, then run the following command to generate the b2 executable and configure the install destination:
| |
Once it finishes, you will see a new b2 executable and a project-config.jam configuration file in the directory. Edit the configuration file.
Refer to «Boost Cross-compilation»
When using gcc, you first need to specify your cross compiler in user-config.jam (see the section called “Configuration”)
When using gcc, you first need to specify your cross compiler in user-config.jam (see the “Configuration” section)
This is the part that affects cross-compilation — pay close attention to it! It contains the following:
Many of toolsets have an
optionsparameter to fine-tune the configuration. All of Boost.Build’s standard compiler toolsets accept four optionscflags,cxxflags,compileflagsandlinkflagsas options specifying flags that will be always passed to the corresponding tools. Values of thecflagsfeature are passed directly to the C compiler, values of thecxxflagsfeature are passed directly to the C++ compiler, and values of thecompileflagsfeature are passed to both.Many toolsets have an
optionsparameter for fine-tuning the configuration. All of Boost.Build’s standard compiler toolsets accept four optionscflags,cxxflags,compileflagsandlinkflagsthat specify the flags always passed to the corresponding tools. The value of thecflagsfeature is passed directly to the C compiler, the value of thecxxflagsfeature is passed directly to the C++ compiler, and the value of thecompileflagsfeature is passed to both.
For example:
| |
All supported compilers and configuration syntax can be found in «Boost Builtin tools»
Modifying project-config.jam
Back in project-config.jam, change it to the following:
| |
Note the following:
- Keep all the spaces in it; the values are probably concatenated as a single string, and without spaces the concatenation will break
- You need to set
sysrootfor both the compiler and the linker; otherwise the compiler cannot find the header files and the linker cannot find the system libraries - Set the compiler arguments according to what your toolchain provides
These values can be read from environment variables after running source <toolchain-config-script>, and you add them in the form shown above.
The Stack Overflow question «crti.o file missing» mentions:
in cross compiling setting sysroot is crucial. Just setting sysroot in compiler is not enough, as @surajit declared one must also configure in linker setting. In some IDEs it is done in “Miscellaneous” menu.
In cross-compilation, setting sysroot is crucial. Just setting sysroot in the compiler is not enough; as @surajit stated, one must also configure it in the linker settings. In some IDEs, it is done in the “Miscellaneous” menu.
Building
At this point you can source the script to set up the cross-compilation environment. This is unrelated to Boost; the configuration script is provided by the toolchain:
| |
Then run the following command to build Boost:
| |
toolset=toolset: Indicate the toolset to build with. | The compiler version to use, corresponding to the setting inproject-config.jamtarget-os: The operating system on which the artifacts run. If the target OS differs from the host, you need to specify it separately; it can be ignored herelink=static|shared: Whether to build static or shared libraries | Whether the built artifacts are dynamic or static libraries, depending on your needsthreading=single|multi: Whether to build single or multithreaded binaries | Whether to use multithreadinginstall: Installs into theoutput/folder set earlier
For more, run b2 --help.
Miscellaneous
As for customizations such as trimming components, you can set them when running bootstrap.sh; I won’t go into detail here.
Be sure to configure sysroot when cross-compiling.