Cross-Compiler Naming Convention
OpenWrt cross-compiler binaries follow this naming pattern:STAGING_DIR and the Sysroot
TheSTAGING_DIR environment variable points to the directory that contains headers and libraries for the target system. It acts as a sysroot: the cross-compiler looks here for #include files and -l libraries instead of the host system paths.
Using the Toolchain for External Packages
You can use the OpenWrt toolchain to cross-compile any external software. The key is to set upSTAGING_DIR and add the toolchain’s bin/ directory to PATH.
Basic Setup
Using pkg-config
OpenWrt installs a hostpkg-config in staging_dir/host/bin/ configured to search the target sysroot:
Cross-Compiling a Configure-Based Package
Build Environment Variables
When building packages inside the OpenWrt build system, these variables are automatically set:| Variable | Value |
|---|---|
TARGET_CROSS | Cross-compiler prefix, e.g. mips-openwrt-linux-musl- |
TARGET_CC | $(TARGET_CROSS)gcc |
TARGET_CXX | $(TARGET_CROSS)g++ |
TARGET_AR | $(TARGET_CROSS)gcc-ar |
TARGET_RANLIB | $(TARGET_CROSS)gcc-ranlib |
TARGET_CFLAGS | Target optimization and arch flags |
TARGET_CXXFLAGS | Same as TARGET_CFLAGS |
TARGET_LDFLAGS | Linker flags including toolchain library paths |
STAGING_DIR | Target sysroot directory |
PKG_CONFIG | Host pkg-config configured for the sysroot |
Using the Image Builder’s External Toolchain
Thescripts/ext-toolchain.sh script can help configure an external toolchain for use with OpenWrt:
When cross-compiling packages for use with OpenWrt, always link against the libraries in
staging_dir/ rather than host system libraries to ensure ABI compatibility with the target device.