Matt_Caner
Matt_Caner

Reputation: 31

Linking an object file that contains stdio functions with ld, no crt0.o found

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?

Upvotes: 3

Views: 1559

Answers (1)

Employed Russian
Employed Russian

Reputation: 213829

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.

Upvotes: -1

Related Questions