0

I am using Linux Ubuntu 64-bit and compiling C code to be run in a Linux Armbian 32-bit system. I am using:

arm-linux-gnueabi-gcc -c test.c -o test.o
arm-linux-gnueabi-g++ -o test test.o -static

When I test the code on Ubuntu I use gcc/g++, but when I'm finished I compile an link with above "arm.." and scp "test" to the Armbian 32-system.

This works with simple "Hello World" -code provide the the "-static" is there. If I leave "-static" out even the simple executable won't run on the Armbian 32-system.

But if I include code using, say, "gethostbyname" I'll get get warning and the resulting executable won't run on the Armbian 32-bit. What should my compile & link be so that I could compile in Ubuntu 64-bit and copy the executable to the Armbian 32-bit?

The only solution that I've found is do all compile & link on the Armbian, but I want to avoid that since all my tools are not there. Here is the code

#include <stdio.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
struct hostent *lh = gethostbyname("localhost");
if (lh)
    puts(lh->h_name);
else
    herror("gethostbyname");
return 0;
}

When linking with:

arm-linux-gnueabi-g++ -o test test.o -static -lrt

I get a warning:

test.c:(.text+0x18): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the libc version used for linking

When running it on 32-bit Armbian I get an error:

gethostbyname: Resolver internal error

When I link with:

arm-linux-gnueabi-g++ -o test test.o -lrt

I don't get any warning. However when running it on 32-bit Armbian I get an error:

unable to execute ./test: No such file or directory
user1231247
  • 193
  • 1
  • 8
  • https://stackoverflow.com/questions/15165306/compile-a-static-binary-which-code-there-a-function-gethostbyname `hat should my compile & link be so that I could compile in Ubuntu 64-bit and copy the executable to the Armbian 32-bit?` So remove `-static`? – KamilCuk Nov 21 '21 at 10:18
  • If remove "-static" I get "File not found". The problem (I believe) is that when when linking from Ubuntu "arm-linux-gnueabi-g++" adds dynamic addresses of library functions that are not found in 32-bit Armbian. – user1231247 Nov 21 '21 at 14:03
  • `I'll get get warning: Using 'gethostbyname' ` `get "File not found".` What file? Please post the full verbatim error messages into the question. – KamilCuk Nov 21 '21 at 14:07
  • I edited my question and put al messages & source – user1231247 Nov 21 '21 at 17:28

1 Answers1

1

The "No such file or directory" error when you are trying to run a program that very clearly exists is an evidence of incorrectly set interpreter a.k.a. dynamic linker. On your arm machine, do:

 file ./test
 file /bin/ls (or any other dynamically linked executable that works)

and compare the outputs.

Use --dynamic-linker= option of GNU ld to set the correct dynamic linker.

Do not try to statically link with glibc, it doesn't work (or it doesn't work the way most people expect it to).

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • I haven't used ld much. Is "--dynamic-linker=" available for g++? What is the the complete linking command in this simple case? ld ... ? – user1231247 Nov 22 '21 at 05:52
  • Do not use ld directly. gcc can pass any option to ld with `-Wl,`, e.g. `g++ -Wl,--dynamic-linker=...`. – n. m. could be an AI Nov 22 '21 at 05:56
  • what comes after "--dynamic-linker=" – user1231247 Nov 22 '21 at 07:06
  • 1
    the filename of the dynamic linker obviously. What does `file /bin/ls` say? – n. m. could be an AI Nov 22 '21 at 10:03
  • I says on the 64-bit: "/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=9567f9a28e66f4d7ec4baf31cfbf68d0410f0ae6, stripped" and on the 32-bit: "/bin/ls: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=4757593a0f8e0a78cab30d64c9022e00af06338c, stripped" – user1231247 Nov 22 '21 at 14:34
  • So you need to set it to `/lib/ld-linux-armhf.so.3` because that's what your arm machine needs. – n. m. could be an AI Nov 22 '21 at 19:22
  • What would my link command be? I tied "arm-linux-gnueabi-g++ -WL,dynamic-linker=/lib/ld-linux-armhf.so.3 -o test2 test.o -lrt" but I got "error: unrecognized command line option ‘-WL,dynamic-linker=/lib/ld-linux-armhf.so.3’" – user1231247 Nov 22 '21 at 19:33
  • You need `-Wl,--dynamic-linker=...`. I strongly recommend studying command line options of your tools, you will need them. – n. m. could be an AI Nov 22 '21 at 19:35
  • Can you provide a more complete link command in addition or for replacing "arm-linux-gnueabi-g++ -o test test.o -lrt" – user1231247 Nov 22 '21 at 20:35
  • 1
    Maybe try `arm-linux-gnueabi-g++ -o test test.o -lrt -Wl,--dynamic-linker=/lib/ld-linux-armhf.so.3`. I don't have an arm system to check this. – n. m. could be an AI Nov 22 '21 at 21:21