我想在没有 -o 优化的情况下编译我的 c 代码(比如 $ gcc -g test test.c),我不认为代码是导致问题的原因,因为我测试了像下面这样的简单代码,它不会工作:
#include<stdio.h>
main()
{
printf("hello\n");
}
我得到了一大堆错误,比如:
test: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o:(.text+0x0): first d efined here
test:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o:(.rodata+0x0): first defined here
test: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crti.o:(.fini+0x0): first defined here
test:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o:(.rodata.cst4+0x0): first defined here
test: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o:(.data+0x0): first defined here
我用谷歌搜索,发现那是因为我没有把 -o 放在那里。但我必须这样做,否则像“print var”这样的命令在 gdb 上不起作用。无论如何我可以解决这个问题吗?
谢谢!
最佳答案
您所做的如下:
gcc -o test test.c
这会创建一个可执行测试(-o
表示输出转到参数,在本例中为 test
)。对于优化,您使用 -O
(大写 O)。
现在你可以:
gcc -g test test.c
它告诉 gcc
使用 -g
开关(启用调试符号)进行编译并使用文件 test
和 test .c
用于编译。所以你的错误消息是由 gcc 试图编译可执行文件 test
引起的,这显然不是 C 源文件。只需键入以下内容,您就会得到类似的结果:
gcc test
改为运行
gcc -g -o test test.c
正如评论中所建议的那样。
关于c - 没有优化的 gcc 会出错(但我必须省略 -o 才能启用 gdb 函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7449783/