GCC不想更改输出文件

时间:2012-11-18 20:17:53

标签: gcc compilation

似乎自从我安装了最新版本的GCC后,如果我想更改输出文件,我将无法再编译任何C文件。我们举一个例子,文件hello.c

#include <stdlib.h>
#include <stdio.h>

int main()
{
     printf("hello\n");
}

如果我这样做:

gcc hello.c

它工作正常,我有a.out输出。但是,如果我想更改输出的名称,我基本上应该这样做:

gcc -o hello.c hello

我是对的吗?

如果是这样,我收到此错误:

gcc: error: hello: No such file or directory
gcc: fatal error: no input files
compilation terminated

另一个例子,它完全是WTF:

gcc -o Simplexe.c Simplexe
Simplexe: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
Simplexe: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
Simplexe: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
Simplexe:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
Simplexe: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
Simplexe: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
Simplexe:(.dtors+0x8): first defined here
/usr/bin/ld: error in Simplexe(.eh_frame); no .eh_frame_hdr table will be created.

我从未见过这样的东西,它删除了我的源文件。我被抓了一次,我再也不会了。

2 个答案:

答案 0 :(得分:3)

更改

gcc -o hello.c hello

gcc -o hello hello.c

-o之后是目标,而不是来源。

如果目标Simplexe存在且现在gcc尝试将其再次链接到“目标”Simplexe.c,则可能发生第二种情况,但这只是猜测。

答案 1 :(得分:1)

-o指定的输出文件,在您的情况下为 hello.c ,因此您尝试编译文件 hello ,该文件不存在。正确的命令是:

  

gcc hello.c -o hello