crosstools-ng can't find pthread.so

2019-03-20 12:13发布

问题:

I am trying to use crosstools-ng to compile a program that uses pthread, however for some reason the linker can't find the library. I have checked and the libraries are located in the link path specificed by -L is the arguments.

Here is the error:

/home/***/raspberrypi/toolchain/lib/gcc/arm-unknown-linux-gnueabi/4.6.3/../../../.. /arm-unknown-linux-gnueabi/bin/ld: cannot find /lib/arm-linux-gnueabihf/libpthread.so.0

/home/***/raspberrypi/toolchain/lib/gcc/arm-unknown-linux-gnueabi/4.6.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find /usr/lib/arm-linux-gnueabihf/libpthread_nonshared.a

Why can't ld find the file that is within the path?

回答1:

Edit your .../usr/lib/arm-linux-gnueabihf/libpthread.so:

/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf32-littlearm)
GROUP ( /lib/arm-linux-gnueabihf/libpthread.so.0 /usr/lib/arm-linux-gnueabihf/libpthread_nonshared.a )

to

/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf32-littlearm)
GROUP ( libpthread.so.0 libpthread_nonshared.a )


回答2:

See this info page: https://sourceware.org/binutils/docs-2.24/ld/File-Commands.html#File-Commands

Read the definitions of INPUT and GROUP, specifically:

In case a sysroot prefix is configured, and the filename starts with the `/' character, and the script being processed was located inside the sysroot prefix, the filename will be looked for in the sysroot prefix. Otherwise, the linker will try to open the file in the current directory. If it is not found, the linker will search through the archive library search path. See the description of `-L' in Command Line Options.

So your linker script should be:

/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf32-littlearm)
GROUP ( /lib/libpthread.so.0 /usr/lib/libpthread_nonshared.a )

...as your toolchain is using a sysroot prefix.

You can find your sysroot prefix with:

<tuple>-gcc -print-sysroot


回答3:

Using the GCC --sysroot=dir flag should fix the issue. This flag tells GCC to search both headers and libraries under the dir folder.

In your case, if you add --sysroot=/home/user/rpi_root to linker flags, ld will search for /home/user/rpi_root/lib/libpthread.so.0 instead of just /lib/libpthread.so.0.

This is particularly helpful to fix linking with fullpath to library.

When using CMake to generate build system, you should use SET(CMAKE_SYSROOT ${RPI_ROOT_PATH}), where RPI_ROOT_PATH contains the path to the RPi sysroot instead of directly set compiler flags.