3

Recently I've been learning linker and trying to link simple files using ld, but then, I failed to do so (probably because of architecture x64?... I cannot really tell). Here are the files I used:

#include<stdio.h>

int fun(int x);

int main(){
    int i = fun(10);
    printf("%d\n",i);
    return 0;
}

and:

int fun(int x){
    return x+10;
}

I compiled them with gcc -c commands and then linked them into a single file with command:

ld -r -o a.o main.o fun.o

and, finally, I wanted to make the a.o file into an executable. I have read /usr/lib/ctr0.o is needed, but not having found it, I tried /x86_64-linux-gnu/crt1.o (and learned it did not work). What is a replacement for crt0.o then? I do not have crt0.o in my system (I did my search, but probably not where I should have). What other file can I use in its place, so that the command:

ld a.o <SOME_FILE> -lc

is going to produce executable output?

Matt_Caner
  • 31
  • 2

1 Answers1

0

trying to link simple files using ld

You should never ever use ld directly on UNIX, unless you are linking some special program, like the kernel or the boot loader.

For "regular" user-level programs, always use the appropriate compiler frontend (gcc for "C", g++ for "C++").

P.S.

If you want to know where crt0.o comes from, try gcc -v a.o, and gcc will show you.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • I know I should never use ld in 'normal' projects, but the case is I am learning how does it work. And yes, I did tried verbose but there is no crt0.o there – Matt_Caner Mar 10 '20 at 06:19
  • "there is no crt0.o"-- yes, *there is*. It's just called `crt1.o`, or maybe `Scrt1.o`. The *point* is that the exact name and location depends on a lot of things, which the compiler driver knows about, and you don't. "learning how ..." -- this is good, but your should have mentioned that in your question. – Employed Russian Mar 10 '20 at 13:55