未经优化的 gcc 会出现错误(但我必须省略 -o 才能启用 gdb 功能)

发布于 2024-12-05 01:37:13 字数 1163 浏览 1 评论 0原文

我想在没有 -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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

川水往事 2024-12-12 01:37:13

您所做的如下:

gcc -o test test.c

创建一个可执行测试(-o 表示输出到参数,在本例中为 test)。为了进行优化,您可以使用-O(大写的O)。

现在你要做的:

gcc -g test test.c

它告诉 gcc 使用 -g 开关进行编译(启用调试符号)并使用文件 testtest.c 用于编译。因此,您的错误消息是由 gcc 尝试编译可执行文件 test 引起的,这显然不是 C 源文件。只需输入以下内容即可获得类似的结果:

gcc test

运行。

gcc -g -o test test.c

相反,按照评论中已建议的方式

What you did is the following:

gcc -o test test.c

Which creates an executable test (-o means the output goes to the argument, in this case test). For optimisations you use -O (a uppercase O).

Now you do:

gcc -g test test.c

Which tells gcc to compile using the -g switch (enable debug symbols) and to use the files test and test.c for compilation. So your error messages arise by gcc trying to compile the executable file test, which obviously is no C source file. You would get a similar result just by typing:

gcc test

Instead run

gcc -g -o test test.c

as already suggested in the comments.

芯好空 2024-12-12 01:37:13

您正在考虑控制优化的 -O 标志(大写字母“O”)。 -o 标志只是为您的可执行文件提供一个名称。如果您不指定该标志,您的可执行文件会自动命名为 a.out

我认为您可能不小心输入了类似的内容,

gcc -g test.c test.c

这会导致重复的符号错误,如上面所示的错误。正确的 gcc 调用是:

gcc -g -o test test.c

如果您希望获得最大的调试信息,请尝试使用 -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

gcc -g test.c test.c

which would cause duplicate symbol errors like the ones you showed above. The correct gcc invocation would be:

gcc -g -o test test.c

If you are looking to get maximum debugging information try -ggdb3 instead of -g

脸赞 2024-12-12 01:37:13

如果您想完全禁用优化,则默认优化级别为 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文