gcc编译,ld连接

来源:互联网 发布:ui设计网站 知乎 编辑:程序博客网 时间:2024/05/20 21:39

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

1:  #include <stdio.h>
2:  
3:  int main(void)
4:  {
5:      printf("Hello world!/n");
6:  
7:      return 0;
8:  }
.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}.codearea pre.alt{ background-color:#f7f7ff !important}.codearea .lnum{color:#4f81bd;line-height:18px}

直接编译很简单:

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

编译就可以了。

原创粉丝点击