【问题标题】:Should I worry about gcc -pedantic option now?我现在应该担心 gcc -pedantic 选项吗?
【发布时间】:2015-06-23 16:15:40
【问题描述】:

我刚刚开始将 C 作为一种爱好来学习。我正在使用“C 编程:一种现代方法”一书来做这件事。那里有一个名为pun.c 的第一个程序。代码如下:

#include <stdio.h>

int main(void)
{
  int int_figure;
  float float_figure;

  int_figure = 12;
  float_figure = 12.0;
  printf("To C or not to C, this is a question\n");
  printf("%d\n", int_figure);
  printf("%.2f\n", float_figure);
  return 0;
}

其实没关系,因为我想问的问题与gcc的任何.c文件编译都是一样的。

所以在书中有一些gcc 的选项允许在编译期间发现错误。其中一个是-Wall,另一个是-pedantic。因此,当我使用此选项编译文件时,终端中的输出如下:

nickdudaev|c $ gcc -o -Wall pun pun.c
pun: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crti.o:(.fini+0x0): first defined here
pun: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crt1.o:(.data+0x0): first defined here
pun: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/crtbegin.o:(.data+0x0): first defined here
pun:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
pun: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crt1.o:(.text+0x0): first defined here
pun: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crti.o:(.init+0x0): first defined here
/tmp/cc2TRR93.o: In function `main':
pun.c:(.text+0x0): multiple definition of `main'
pun:(.text+0xf6): first defined here
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
pun:(.data+0x10): first defined here
/usr/bin/ld: error in pun(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status

程序虽然运行正常。

nickdudaev|c $ ./pun
To C or not to C, this is a question
12
12.00

所以问题。

  1. 在这个(学习)阶段我应该担心这个输出并使用这些选项吗?
  2. 我可能有一些系统错误吗?因为任何文件编译的输出都是相同的。可能没有正确安装?

我试图搜索 Google,但我发现的唯一内容是关于 gcc 选项的描述。但没有关于可能的输出以及如何处理它。

【问题讨论】:

    标签: c gcc compiler-errors compiler-warnings compiler-options


    【解决方案1】:

    这个:

    gcc -o -Wall pun pun.c
    

    看起来不对。你说的是-Wall,你应该在哪里说输出的名称,即-o的参数。

    试试:

    gcc -Wall -o pun pun.c
    

    基本上,您将 pun(旧二进制文件)作为源文件提供给 gcc。

    【讨论】:

    • 是的,这种方式可以正常工作。谢谢你。我没有考虑过错误的选项顺序。
    【解决方案2】:

    gcc中的-o选项用于指定自定义输出文件名,否则,

    如果不指定-o,则默认在a.out中放一个可执行文件。

    推荐格式为-o file-o 之后的下一个预期参数是文件名,而不是另一个开关。有关详细信息,请参阅online manual

    你应该将你的编译语句重写为

    gcc -o pun pun.c -Wall  
    

    【讨论】:

    • 谢谢,现在很清楚我的混合方式不对。两个版本都可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 2013-03-20
    • 2019-07-13
    相关资源
    最近更新 更多