buildroot file system & cross compiling: dynamically linked application fails but static ok. How to link against uClibc
Asked Answered
M

1

3

I have a buildroot created file system, and I am trying to execute some c-code on lighttpd server.

I have created a simple C module that just prints out few lines of html. Code looks like this:

#include "stdio.h"

int main(void) {
  printf( "Content-Type: text/plain\n\n" );
  printf("testing C code\n");
  return 0;
}

I'm using compiler from Ubuntu repository, to compile it arm-linux-gnueabi-gcc -o test.cgi test.c. If I compile with -static flag, it will run just fine, and I can see the html displayed. But compiling without the static flag, the execution fails like this:

# ./test.cgi 
-sh: ./first.cgi: not found

I am wondering if the reason is wrong a c-library, and I should link against the uClibc that was compiled by buildroot. In my buildroot folder output/host/usr/arm-unknown-linux-uclibcgnueabi/sysroot/lib I can see following files:

-rwxr-xr-x 1 jlumme jlumme  25444 Jan 26 17:13 ld-uClibc-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     21 Jan 26 17:12 ld-uClibc.so.0 -> ld-uClibc-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme  13191 Jan 26 17:13 libcrypt-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     20 Jan 26 17:13 libcrypt.so.0 -> libcrypt-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     21 Jan 26 17:13 libc.so.0 -> libuClibc-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme  13383 Jan 26 17:13 libdl-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     17 Jan 26 17:13 libdl.so.0 -> libdl-0.9.33.2.so
-rw-r--r-- 1 jlumme jlumme    135 Jan 26 17:15 libgcc_s.so
-rw-r--r-- 1 jlumme jlumme 180152 Jan 26 17:15 libgcc_s.so.1
-rwxr-xr-x 1 jlumme jlumme  64555 Jan 26 17:13 libm-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     16 Jan 26 17:13 libm.so.0 -> libm-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme   4772 Jan 26 17:13 libnsl-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     18 Jan 26 17:13 libnsl.so.0 -> libnsl-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme  79059 Jan 26 17:13 libpthread-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     22 Jan 26 17:13 libpthread.so.0 -> libpthread-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme   4776 Jan 26 17:13 libresolv-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     21 Jan 26 17:13 libresolv.so.0 -> libresolv-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme  13927 Jan 26 17:13 librt-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     17 Jan 26 17:13 librt.so.0 -> librt-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme 284451 Jan 26 17:13 libuClibc-0.9.33.2.so
-rwxr-xr-x 1 jlumme jlumme   4955 Jan 26 17:13 libutil-0.9.33.2.so
lrwxrwxrwx 1 jlumme jlumme     19 Jan 26 17:13 libutil.so.0 -> libutil-0.9.33.2.so

Is the libuClibc-0.9.33.2.so file that I'm looking for ? How can I link my application against that ?

In my target board, I can see this in /usr/lib

lrwxrwxrwx    1 root     root            15 Jan 26  2013 libbz2.so -> libbz2.so.1.0.6
lrwxrwxrwx    1 root     root            15 Jan 26  2013 libbz2.so.1 -> libbz2.so.1.0.6
lrwxrwxrwx    1 root     root            15 Jan 26  2013 libbz2.so.1.0 -> libbz2.so.1.0.6
-rwxrwxr-x    1 root     root         57924 Jan 26  2013 libbz2.so.1.0.6
lrwxrwxrwx    1 root     root            16 Jan 26  2013 libelf.so -> libelf.so.0.8.12
lrwxrwxrwx    1 root     root            16 Jan 26  2013 libelf.so.0 -> libelf.so.0.8.12
-rwxr-xr-x    1 root     root         69924 Jan 26  2013 libelf.so.0.8.12
lrwxrwxrwx    1 root     root            16 Jan 26  2013 libpcre.so -> libpcre.so.1.0.0
lrwxrwxrwx    1 root     root            16 Jan 26  2013 libpcre.so.1 -> libpcre.so.1.0.0
-rwxr-xr-x    1 root     root         88804 Jan 26  2013 libpcre.so.1.0.0
lrwxrwxrwx    1 root     root            19 Jan 26  2013 libpcrecpp.so -> libpcrecpp.so.0.0.0
lrwxrwxrwx    1 root     root            19 Jan 26  2013 libpcrecpp.so.0 -> libpcrecpp.so.0.0.0
-rwxr-xr-x    1 root     root         27436 Jan 26  2013 libpcrecpp.so.0.0.0
lrwxrwxrwx    1 root     root            21 Jan 26  2013 libpcreposix.so -> libpcreposix.so.0.0.0
lrwxrwxrwx    1 root     root            21 Jan 26  2013 libpcreposix.so.0 -> libpcreposix.so.0.0.0
-rwxr-xr-x    1 root     root          5576 Jan 26  2013 libpcreposix.so.0.0.0
lrwxrwxrwx    1 root     root            19 Jan 26  2013 libsqlite3.so -> libsqlite3.so.0.8.6
lrwxrwxrwx    1 root     root            19 Jan 26  2013 libsqlite3.so.0 -> libsqlite3.so.0.8.6
-rwxr-xr-x    1 root     root        516472 Jan 26  2013 libsqlite3.so.0.8.6
lrwxrwxrwx    1 root     root            19 Jan 26  2013 libstdc++.so -> libstdc++.so.6.0.14
lrwxrwxrwx    1 root     root            19 Jan 26  2013 libstdc++.so.6 -> libstdc++.so.6.0.14
-rwxr-xr-x    1 root     root        418936 Jan 26  2013 libstdc++.so.6.0.14
-rw-r--r--    1 root     root          2422 Jan 26  2013 libstdc++.so.6.0.14-gdb.py
lrwxrwxrwx    1 root     root            16 Jan 26  2013 libxml2.so -> libxml2.so.2.8.0
lrwxrwxrwx    1 root     root            16 Jan 26  2013 libxml2.so.2 -> libxml2.so.2.8.0
-rwxr-xr-x    1 root     root       1063556 Jan 26  2013 libxml2.so.2.8.0
drwxrwxr-x    2 root     root          4096 Jan 26  2013 lighttpd
-rw-r--r--    1 root     root           195 Jan 26  2013 xml2Conf.sh
Mckinleymckinney answered 26/1, 2013 at 10:11 Comment(6)
I don't know how you make "buildroot" transfer the file you want, but yes, you want libuClibc-0.9.33.2.so - also, make sure it's compiled for ARM!Cochabamba
@Mats, I transfer files to my target board via scp. I tried compiling like this: arm-linux-gnueabi-gcc -o first.cgi -L/home/jlumme/projects/buildroot/output/host/usr/arm-unknown-linux-uclibcgnueabi/sysroot/lib -luClibc-0.9.33.2 Error: (/usr/lib/gcc/arm-linux-gnueabi/4.7/../../../../arm-linux-gnueabi/bin/ld: warning: ld-uClibc.so.0, needed by /LIB_TO_SYSROOT_AS_B4/libuClibc-0.9.33.2.so, not found (try using -rpath or -rpath-link) /usr/lib/gcc/arm-linux-gnueabi/4.7/../../../../arm-linux-gnueabi/lib/../lib/crt1.o: In function _start': (.text+0x34): undefined reference to main' )Mckinleymckinney
Right, so I have no idea how to build .cgi code - but surely there should be a "main" somewhere in the code?Cochabamba
@MatsPetersson Well, actually, it can be just a normal C executable, it will work in lightpdd as well. I am trying to run the application in console, and the statically one works ok (in console, and via browser window)Mckinleymckinney
Yes, which is why I said that I don't know how you convince "buildroot" to include the correct library. I'm sure there is a way. The reason it works when you build it statically is that it includes the library itself into your binary, so you don't need the shared library. It is clear from your second directory listing that there is no libuclibc.so - so it must have not been included in your build.Cochabamba
the root cause: unix.stackexchange.com/questions/18061/… | Similar: #14536397Maihem
O
3

To build your application, you have to use the cross-compiler that Buildroot has generated, located in output/host/usr/bin/<name-of-architecture>-gcc.

Here, you're using the Ubuntu ARM cross-compiler, that targets a system that uses the eglibc C library. But the system generated with Buildroot uses the uClibc C library. Therefore, there is absolutely no chance for a binary built with the Ubuntu ARM cross-compiler to run on this Buildroot generated system.

Just use the cross-compiler Buildroot has generated for you.

Overturn answered 23/3, 2013 at 19:0 Comment(1)
Hi Tomas, This is exactly what was happening, and now my library links properly as expected!Mckinleymckinney

© 2022 - 2024 — McMap. All rights reserved.