Question

I am trying to compile a simplified C source file by "gcc -S" -> "as" -> "ld" on x86_64 platform.

The process finished with no error, but when executed, "No such file or direcotry" error message is shown.

ctest.c
int main()
{
    return 0;
}

> gcc -S ctest.c
> as -o ctest.o ctest.s
> ld -o ctest  /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o
> ./ctest 
bash: ./ctest: No such file or directory

> uname -a
Linux mkb3 2.6.27.48-0.3-default #1 SMP 2010-09-20 11:03:26 -0400 x86_64 x86_64 x86_64 GNU/Linux

I also tried to add dynamic link as stated in some google results.

> ld -o ctest -dynamic-linker /lib64/ld-linux.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o

But the error remained.

Comments and suggestions are appreciated.

Edit: I made a mistake the /lib64/ld-linux.so.2 doesn't exist in my linux box. I should have used /lib64/ld-2.9.so. Didn't know that ld doesn't report error with a non-existing library file specified.

Was it helpful?

Solution

-dynamic-linker works for me, but I have a 32-bit system.

Run gcc -v -o ctest ctest.o and look at the last line of the output. Try to run it as a command. If that works, start to simplify it, throwing out parts until it no longer works. Then throw out some more parts, etc. That's how I arrived to a command that works.

You can also just use gcc -o ctest ctest.o instead.

OTHER TIPS

You should link using -dynamic-linker /lib/ld-linux-x86-64.so.2 rather than /lib64/ld-linux.so.2 which is for 32-bit.

/lib64 $ file -L ld*
ld-linux.so.2:        ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped
ld-linux-x86-64.so.2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, stripped

All in all, this works for me:

$ gcc -S ctest.c
$ as -o ctest.o ctest.s
$ ld -o ctest -dynamic-linker /lib/ld-linux-x86-64.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o

If that still doesn't work follow the suggestion of @n.m. and check the output of gcc -v -o ctest ctest.c.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top