Reputation: 3378
I have problems generating a small hello-world program in C with ld as linker.
These are my steps so far:
gcc -c hello.c
ld -o hello hello.o -lc
./hello
-bash: ./hello: no such file or directory
hello.c
's source is here:
#include <stdio.h>
int main(){
puts("Hello, world!");
return 0;
}
It seems I have missed an important part here. Neither gcc
nor ld
had any errors and both ended with return value 0.
Please do not tell me "just use gcc -o hello hello.c
"! I have browsed like 10 boards and people always gave that answer. I want to know how to do it the ld-way.
Upvotes: 1
Views: 1365
Reputation: 8491
If you want to understand what GCC is actually doing, run it with the -v
flag. (For example, gcc -v -o hello hello.c
.)
You're missing some pieces of code that GCC would normally instruct the linker to include. If you look at the output of gcc -v
, you'll see things like crt1.o -lgcc -lgcc_s
and others.
See also the GCC documentation for options such as -nostartfiles
, -nodefaultlibs
, and -nostdlib
for some context on these extra bits of code that are being linked in behind the scenes.
Upvotes: 3