Arjun Jain
Arjun Jain

Reputation: 23

ld: cannot find lc -ERROR

My purpose is to link two codes using my own linker script and for the same, i have created a simple linker script (All help from around the internet) This is my linker script, "link.lds"

SECTIONS
{
   . = 0x10000;
   .text : { *(.text) }
   . = 0x8000000;
   .data : { *(.data) }
   .bss : { *(.bss) }
}

and my two simple C codes are as follows. 1) l1.c

#include<stdio.h>
extern int a;
int main()
{
printf("%d",a);
return 0;
}

AND 2) l2.c

int a=111;

The commands that i use are:

gcc -c l1.c l2.c
ld -o output -T link.lds l1.o l2.o -lc

After following the above steps, I encounter the following error:

ld:cannot find lc

on removing lc,

undefined reference to printf().

I also tried using -L/dirname which took me back to the undef reference to printf error.

MAY I GET SOME HELP AND GUIDANCE ON THE SAME... PS- Im aware i could have gone conceptually wrong and/or might not be aware about the correct sequence of linker execution/working. Any help on the same would be highly appreciated. Many Thanks!

Upvotes: 1

Views: 2819

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61610

Since you are invoking ld directly, no default libraries or default library search paths are supplied, as they would be if ld were invoked indirectly by the gcc tool-driver in the usual way.

So as well as telling ld explicitly to link libc (-lc), you must explicitly also tell it where to find libc, using the -L option. (It seems that you think L<path> is an alternative to -l<libname>. It's not.)

Therefore find out where libc.so is located on your system. You can do this with:

realpath $(gcc --print-file-name libc.so)

(Note, not --print-file-name-libc.so. There is a typo in @n.m's comment)

Suppose it is /usr/lib/x86_64-linux-gnu/libc.so, as on my system. Then run:

ld -o output -T link.lds l1.o l2.o -L/usr/lib/x86_64-linux-gnu -lc

That will solve the problem in your question (but not necessarily any others).

Upvotes: 3

Related Questions