In this lesson, you will learn how to configure and build third-party open-source libraries so they function correctly within your target system’s unique hardware architecture.
When developing for embedded Linux, the libraries you find in your desktop environment (like libpng, openssl, or curl) cannot simply be copied over; they must be recompiled using the cross-toolchain you previously established. Because your target (e.g., ARM) differs from your host (e.g., x86_64), the build system must be instructed to ignore the host's headers and libraries in favor of the target-specific sysroot.
The Build Workflow
Most open-source libraries use autotools or CMake. The key to cross-compilation is passing the correct environment variables and flags to these build systems. Specifically, you must point the build process to your cross-compiler and the root filesystem directory where the target's dependencies reside.
For a typical configure script, the command looks like this:
./configure --host=arm-linux-gnueabihf \
--prefix=/usr \
CC=arm-linux-gnueabihf-gcc \
LDFLAGS="-L/path/to/target/rootfs/lib" \
CPPFLAGS="-I/path/to/target/rootfs/include"
--host: Informs the build system that the resulting binary is intended for your specific architecture.--prefix: Defines where the library will live on the target (usually/usror/usr/local).LDFLAGS/CPPFLAGS: These ensure the linker and preprocessor find the correct headers and shared objects for your specific target kernel and system libraries, rather than the ones on your development machine.
Installation and Deployment
Once compiled, you must install these files into your staging area—a directory that mimics the target's root filesystem structure. You would typically use make DESTDIR=/path/to/target/rootfs install. By setting DESTDIR, you ensure that the files are placed in the correct subdirectories (/lib, /usr/include, etc.) relative to your root filesystem image rather than overwriting your host system files. After installation, ensure your library paths on the target are managed correctly, as discussed in our lesson on managing shared libraries and dynamic linkers.
Key Takeaway: To cross-compile successfully, you must explicitly point your build tools to the cross-compiler and the target's sysroot, ensuring the library is built for the correct architecture and installed into the staging area of your root filesystem.