未经优化的 gcc 会出现错误(但我必须省略 -o 才能启用 gdb 功能)
我想在没有 -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 上运行。无论如何我可以解决这个问题吗?
谢谢你!
I want to compile my c code without -o optimization (like $ gcc -g test test.c),I don't think the code is causing the problem because I tested simple code like the following and it won't work either:
#include<stdio.h>
main()
{
printf("hello\n");
}
and I got a whole bunch of errs like:
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
I googled and found that is because I didn't put -o there. But I have to, otherwise commands like "print var" wouldn't work on gdb. Is there anyway I can get around with this?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所做的如下:
创建一个可执行测试(
-o
表示输出到参数,在本例中为test
)。为了进行优化,您可以使用-O
(大写的O)。现在你要做的:
它告诉
gcc
使用-g
开关进行编译(启用调试符号)并使用文件test
和test.c
用于编译。因此,您的错误消息是由 gcc 尝试编译可执行文件test
引起的,这显然不是 C 源文件。只需输入以下内容即可获得类似的结果:运行。
相反,按照评论中已建议的方式
What you did is the following:
Which creates an executable test (
-o
means the output goes to the argument, in this casetest
). For optimisations you use-O
(a uppercase O).Now you do:
Which tells
gcc
to compile using the-g
switch (enable debug symbols) and to use the filestest
andtest.c
for compilation. So your error messages arise by gcc trying to compile the executable filetest
, which obviously is no C source file. You would get a similar result just by typing:Instead run
as already suggested in the comments.
您正在考虑控制优化的 -O 标志(大写字母“O”)。 -o 标志只是为您的可执行文件提供一个名称。如果您不指定该标志,您的可执行文件会自动命名为 a.out
我认为您可能不小心输入了类似的内容,
这会导致重复的符号错误,如上面所示的错误。正确的 gcc 调用是:
如果您希望获得最大的调试信息,请尝试使用
-ggdb3
而不是-g
You're thinking of the -O flags, (capital letter "O"), which control optimization. The -o flag just gives your executable a name. If you don't specify that flag, your executable automatically gets named a.out
I am thinking you might have accidentally typed something like
which would cause duplicate symbol errors like the ones you showed above. The correct gcc invocation would be:
If you are looking to get maximum debugging information try
-ggdb3
instead of-g
如果您想完全禁用优化,则默认优化级别为 -O1,
您还必须将 -O0 传递给 gcc,即字母大写 O 和数字零。
the default optimization level is -O1 if you want to disable optimizations entirely,
you must also pass -O0 to gcc, that is the letter capital O and the number zero.