How to compile C-code on a 64-bit Linux system for an ARM 32-bit executable

875 Views Asked by At

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
1

There are 1 best solutions below

10
n. m. could be an AI On

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).