简体   繁体   English

ld:找不到lc -ERROR

[英]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" 我的目的是使用自己的链接描述文件链接两个代码,为此,我创建了一个简单的链接描述文件(来自互联网的所有帮助),这是我的链接描述文件“ link.lds”

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

and my two simple C codes are as follows. 我的两个简单的C代码如下。 1) l1.c 1)l1.c

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

AND 2) l2.c 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, 在删除lc时,

undefined reference to printf().

I also tried using -L/dirname which took me back to the undef reference to printf error. 我还尝试使用-L / dirname,这使我回到了undef对printf错误的引用。

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. 我可能会对此有所帮助和指导... PS- Im知道我在概念上可能出错和/或可能不知道链接程序执行/工作的正确顺序。 Any help on the same would be highly appreciated. 对此的任何帮助将不胜感激。 Many Thanks! 非常感谢!

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. 由于您是直接调用ld ,因此不会提供默认库或默认库搜索路径,就像通过gcc工具驱动程序以通常方式间接调用ld一样。

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. 所以,以及告诉ld明确链接libc-lc ),你必须明确地还会告诉它在哪里可以找到libc ,使用-L选项。 (It seems that you think L<path> is an alternative to -l<libname> . It's not.) (似乎您认为L<path>-l<libname>替代方法 。不是。)

Therefore find out where libc.so is located on your system. 因此,找出libc.so在系统上的位置。 You can do this with: 您可以执行以下操作:

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

(Note, not --print-file-name-libc.so . There is a typo in @nm's comment) (注意,不是--print-file-name-libc.so 。@nm的注释中有错字)

Suppose it is /usr/lib/x86_64-linux-gnu/libc.so , as on my system. 假设它是/usr/lib/x86_64-linux-gnu/libc.so ,就像在我的系统上一样。 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). 这样可以解决您所提出的问题(但不一定解决任何其他问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM