【问题标题】:Using gcc with multiple files对多个文件使用 gcc
【发布时间】:2018-04-03 20:09:19
【问题描述】:

我正在尝试从 c 文件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

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

【问题讨论】:

  • 首先——你不需要编译头文件。
  • 您确定您报告的是准确的命令行和准确的错误吗?您提到的gcc 命令应该可以工作。
  • 顺便说一句:你不应该将你的程序命名为“test”。如果您尝试调用它,名称“test”会与 shell 命令“test”发生冲突。所以你每个人都必须输入“./test”来解决这个问题。 :-)
  • @Klaus 无论如何都必须以这种方式运行。我怀疑这样的测试程序会被安装到任何地方的运行路径中。
  • @Klaus 这是人们做的事吗?这对我来说更像是一个危险信号,而不是命名你的测试二进制文件。

标签: c linux shell gcc


【解决方案1】:

这是你的错误

  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 是由于某些代码错误,可能是头文件问题。所以发布你的代码

【讨论】:

    【解决方案2】:

    差异似乎是由于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(这可能会造成混淆)。

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

       -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.
    

    来自clang manpage https://clang.llvm.org/docs/CommandGuide/clang.html

    -o <file> Write output to file.
    

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

    【讨论】:

    • 除了 clang 和 gcc 处理它的方式不同之外,您应该指定问题的原因(对于 clang)为什么它不起作用。
    • 嗯,我的 gcc 可能指向 clang,它可以解释错误,但这是怎么发生的呢?我不记得每个人都故意做那样的事情。
    • @RothX:您可以通过运行which gccgcc --version 来检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2018-06-29
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多