gcc编译,ld连接

来源:互联网 发布:linux shell脚本 pause 编辑:程序博客网 时间:2024/05/03 19:41

/*****************************************************************************************************************************

原文地址:http://blog.csdn.net/lanxinju/article/details/6078045

评价:照做了,但是没有成功。不过也算找到同路人了。。。

*******************************************************************************************************************************/


编写一个hello world的简单程序(hello.c):

1:  #include <stdio.h>
2:  
3:  int main(void)
4:  {
5:      printf("Hello world!/n");
6:  
7:      return 0;
8:  }

直接编译很简单:

gcc –o hello.o hello.c

但是如果使用gcc编译,然后使用ld连接,就会出错:

gcc –c –o hello.o hello.c 
ld –o hello hello.o 
连接时警告没有入口_start(程序原始的入口为_start,执行一些堆栈初始化工作,然后再跳入main函数),生成的hello文件无法执行。

说明分开连接的时候少做了一些事,使用gcc -v选项查看gcc编译时的信息: 
gcc –o hello.o hello.c –v

最终的解决方案是:

gcc –c –o hello.o hello.c 
ld –o hello –dynamic-linker /lib/ld-linker.so.2 /usr/lib/crt1.o /usr/lib/crti.o –l hello.o /usr/lib/ctrn.o

编译就可以了。


原创粉丝点击