错误信息:
在函数'_start'中:(.text+0x20):对'main'未定义的引用collect2 :error: ld returned 1 exit status
问题:
在文章《linux下编译c文件成为可执行文件的实例和详细过程》中说到,gcc编译分四步,预编译、编译、汇编、链接,但是在链接时,始终不成功
使用的命令:
预编译:gcc -E hello.c -o hello.i
编译:gcc -S hello.i -o hello.s
汇编:gcc -C hello.s -o hello.o
链接:gcc hello.o -o hello
在执行链接命令gcc hello.o -o hello时,总是提示错误,错误信息提示如下:
而使用命令gcc hello.c -o hello直接生成可执行文件,则没有任何问题。
解决:
经过分析发现,汇编时使用的命令错误,应该是gcc -c hello.s -o hello.o,而非gcc -C hello.s -o hello.o,故做此记录,希望帮到有类似问题的朋友。