Can't find libraries when cross compiling

I am trying to cross compile a program that I've written for OpenWRT. It's simple enough that I've never bothered with a Makefile, let alone any of the GNU autotools. The "regular" build process, which includes building and linking to one non-standard library, is documented here.

I've run through the process of building an OpenWRT image, including libnml, and I've also managed to build my library, so I've got the following files:

  • Compiler — ${BUILDROOT}/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/bin/mips-openwrt-linux-musl-gcc
  • My non-standard library — ${BUILDROOT}/libsavl/
  • JSON-C — ${BUILDROOT}/staging_dir/target-mips_24kc_musl/usr/lib/
  • libmnl${BUILDROOT}staging_dir/target-mips_24kc_musl/usr/lib/

I'm now at the stage of trying to link my executable, and I cannot figure out how to tell the compiler where the libraries are. For example, -Wl,-rpath=${STAGING_DIR}/staging_dir/target-mips_24kc_musl/usr/lib should add that directory to the library search path and enable the linker to find the JSON-C library, but I'm still getting a link error about that library:

$ ls ${STAGING_DIR}/target-mips_24kc_musl/usr/lib/libjson-c*

$ mips-openwrt-linux-musl-gcc -O3 -Wall -Wextra -Wcast-align -o fdfd -I${STAGING_DIR}/target-mips_24kc_musl/usr/include -I${BUILDROOT}/libsavl *.c -Wl,-rpath=${STAGING_DIR}/target-mips_24kc_musl/usr/lib -ljson-c -lsavl -ldl
/mnt/kernel/openwrt/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/bin/../lib/gcc/mips-openwrt-linux-musl/8.4.0/../../../../mips-openwrt-linux-musl/bin/ld: cannot find -ljson-c
/mnt/kernel/openwrt/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/bin/../lib/gcc/mips-openwrt-linux-musl/8.4.0/../../../../mips-openwrt-linux-musl/bin/ld: cannot find -lsavl
collect2: error: ld returned 1 exit status

(The error about -lsavl is expected.)

What in the world is the correct syntax to tell the cross compiler where to look for libraries?


That kind of heavy lifting is what the buildsystems and creating Makefiles for your packages would get off your shoulders. Yes, the toolchain can be re-used in different venues, but that's difficult (and the intricacies depend heavily on the buildsystem of your desired addon package) and there's little general advice.

I'm not sure that -Wl,-rpath option (relative path for searching libs at runtime) overwrite -L option (path for searching libs at build time)

1 Like

A simple-ish approach might be to might be to use the mechanism that allows the OpenWrt build to reference a package in a local git repository.

Adding a package descriptor file should allow your package to show up in the build menus, then turn on the developer options and sym-link to the git repository as described here: Other parts of that guide might help too, although I haven't read them in detail.

Or see if sym-linking from a "standard" library folder to where the ones you've built are - I'm not sure if you just want to see if it runs, or get to a reasonably durable recipe for others to use.

I'm not sure that -Wl,-rpath option (relative path for searching libs at runtime) overwrite -L option (path for searching libs at build time)

-L is the key! I wasn't aware of it, and had only been searching the ld(1) man page for the correct options. Simply passing -L${STAGING_DIR}/target-mips_24kc_musl/usr/lib eliminates the JSON-C error, so I just need to figure out what symlinks I need to link to my libsavl library.


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.