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:

1
./bootstrap.sh --prefix=output

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 options parameter to fine-tune the configuration. All of Boost.Build’s standard compiler toolsets accept four options cflags, cxxflags, compileflags and linkflags as options specifying flags that will be always passed to the corresponding tools. Values of the cflags feature are passed directly to the C compiler, values of the cxxflags feature are passed directly to the C++ compiler, and values of the compileflags feature are passed to both.

Many toolsets have an options parameter for fine-tuning the configuration. All of Boost.Build’s standard compiler toolsets accept four options cflags, cxxflags, compileflags and linkflags that specify the flags always passed to the corresponding tools. The value of the cflags feature is passed directly to the C compiler, the value of the cxxflags feature is passed directly to the C++ compiler, and the value of the compileflags feature is passed to both.

For example:

1
        using gcc : 3.4 : : <compileflags>-m64 <linkflags>-m64 ;

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:

1
2
3
4
5
if ! gcc in [ feature.values <toolset> ]
{
    using gcc ; 
    using gcc : arm : aarch64-poky-linux-g++ : <compileflags>--sysroot=/your/sysroot/path/aarch64-poky-linux <linkflags>--sysroot=/your/sysroot/path/aarch64-poky-linux <compileflags>-compiler-flags <compileflags>--other-flags ; 
}

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 sysroot for 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:

1
source /your/cross-compile-toolchain/config-script

Then run the following command to build Boost:

1
./b2 toolset=gcc-arm target-os=linux link=static threading=multi install
  • toolset=toolset: Indicate the toolset to build with. | The compiler version to use, corresponding to the setting in project-config.jam
  • target-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 here
  • link=static|shared: Whether to build static or shared libraries | Whether the built artifacts are dynamic or static libraries, depending on your needs
  • threading=single|multi: Whether to build single or multithreaded binaries | Whether to use multithreading
  • install: Installs into the output/ 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.

References