繁体   English   中英

使用gcc与多个文件

[英]Using gcc with multiple files

我试图从ac文件test.c生成可执行文件测试。

test.c包含main()函数,但我也希望包含stuff.c函数。 我被告知在stuff.h声明函数然后运行命令gcc -Wall -std=c99 stuff.c stuff.h test.c -o test

我知道这个的每个组件是做什么的,但这是我第一次尝试将gcc与多个输入文件一起使用。 当我尝试运行此命令时,它失败了,我得到错误clang: error: cannot specify -o when generating multiple output files

据我所知,我只是想生成一个输出文件,所以我假设这个命令设置不正确。 我该如何解决?

差异似乎是由于gccclang之间的行为差​​异,如下例所示。

~/test $ ls -ltr
total 4
-rw-rw-r-- 1 arusaha arusaha  0 Apr  3 13:19 b.h
-rw-rw-r-- 1 arusaha arusaha  0 Apr  3 13:19 b.c
-rw-rw-r-- 1 arusaha arusaha 14 Apr  3 13:20 a.c

~/test $ clang -Wall -std=c99 b.c b.h a.c -o ab
clang: error: cannot specify -o when generating multiple output files

~/test $ gcc -Wall -std=c99 b.c b.h a.c -o ab
~/test $ ls -ltr
total 16
-rw-rw-r-- 1 arusaha arusaha    0 Apr  3 13:19 b.h
-rw-rw-r-- 1 arusaha arusaha    0 Apr  3 13:19 b.c
-rw-rw-r-- 1 arusaha arusaha   14 Apr  3 13:20 a.c
-rwxrwxr-x 1 arusaha arusaha 8576 Apr  3 13:21 ab
~/test $ 

gcc支持编译多个文件,请访问http://www.network-theory.co.uk/docs/gccintro/gccintro_11.html

我很好奇gcc命令如何抛出一个clang错误,结果证明是一个线索:)。 OP的gcc可能指向clang (这可能令人困惑)。

来自http://man7.org/linux/man-pages/man1/gcc.1.html的 gcc

  -o file Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code. If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output. 

来自https://clang.llvm.org/docs/CommandGuide/clang.html的 clang联机帮助页

 -o <file> Write output to file. 

对我来说,仅仅阅读man页时,行为的差异并不完全清楚,但我可能是错的。

这是你的错误

  1. 你不是想在c中编译.h文件所以试试这个:

    gcc -Wall -std=c99 stuff.c test.c -o test

  2. gcc还取决于您运行它的环境。 在我的系统上如果我做gcc -Wall -std=c99 stuff.c stuff.h test.c -o test它会起作用,因为编译器是这样设置的。 在某些机器上可能会有所不同。 因此,如果由于一些奇怪的原因#1不起作用,你必须做以下事情:

    gcc -Wall -std=c99 -c stuff.c test.c

    gcc -Wall -std=c99 stuff.o test.o -o test

    ./test

  3. 但你的错误clang: error: cannot specify -o when generating multiple output files是由于某些代码错误,也许是头文件问题。 所以发布你的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM