本文介绍了生成多个输出文件时无法指定-o [C错误]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我对使用gcc生成文件有疑问,以下是我在makefile中的代码.生成多个输出文件时,我无法指定-o错误,但我只是看不出问题出在哪里.有人可以指出我的错误吗?谢谢!

I have a question about make file using gcc, below is my code in makefile. I got cannot specify -o when generating multiple output files error, but I just cant see where the problem is. Can someone point out my mistakes? Thanks!

BASE_FILES=bwtsearch.c bwtsearch.h bwttext.c bwttext.h chartable.c chartable.h common.h occtable.c occtable.h plset.c plset.h strbuf.c strbuf.h
BASE_ENCODER_FILES=bwtencoder.h bwtencoder.c
BWTSEARCH_FILES=${BASE_FILES} main_bwtsearch.c
BENCODE_FILES=${BASE_ENCODER_FILES} main_bencode.c
PSEARCH_FILES=${BASE_FILES} main_psearch.c
PSEARCH_NL_FILES=${BASE_FILES} main_psearch_nl.c
PENCODE_FILES=${BASE_ENCODER_FILES} main_pencode.c
PENCODE_NL_FILES=${BASE_ENCODER_FILES} main_pencode_nl.c
DEBUG_FILES=${BASE_FILES} main_debug.c

all: bwtsearch psearch psearch_nl pencode pencode_nl bencode

clean:
    rm psearch psearch_nl bwtsearch pencode pencode_nl bencode bwt_debug

bwtsearch: ${BWTSEARCH_FILES}
    gcc -o bwtsearch ${BWTSEARCH_FILES}

bencode: ${BENCODE_FILES}
    gcc -o bencode ${BENCODE_FILES}

psearch: ${PSEARCH_FILES}
    gcc -o psearch ${PSEARCH_FILES}

psearch_nl: ${PSEARCH_NL_FILES}
    gcc -o psearch_nl ${PSEARCH_NL_FILES}

pencode: ${PENCODE_FILES}
    gcc -o pencode ${PENCODE_FILES}

pencode_nl: ${PENCODE_NL_FILES}
    gcc -o pencode_nl ${PENCODE_NL_FILES}

debug: ${DEBUG_FILES}
    gcc -o bwt_debug ${DEBUG_FILES}

下面是控制台的输出:)

below is the output of the console :)

gcc -o bwtsearch bwtsearch.c bwtsearch.h bwttext.c bwttext.h chartable.c chartable.h common.h occtable.c occtable.h plset.c plset.h strbuf.c strbuf.h main_bwtsearch.c
clang: error: cannot specify -o when generating multiple output files
make: *** [bwtsearch] Error 1

推荐答案

由于样式正确的头文件不会生成可执行代码,因此无需将头文件放在要编译的文件列表中.

There should be no need to put header files in the list of files to be compiled, since a stylistically correct header file generates no executable code.

过去通常情况下,将头文件放在gcc命令行中几乎是无害的,因为编译器不会在解析和编译头后将任何可执行内容添加到输出文件中.但是,由于gcc版本为4左右,并且使用clang的时间大致相同,因此命令行上的头文件被编译为预编译的头,以供以后的编译步骤使用.

It used to be the case that it was mostly harmless to put header files in the gcc command line, because the compiler would add no executable content to the output file as a result of parsing and compiling a header. However, since gcc version 4 or so, and for roughly the same amount of time for clang, header files on the command-line are compiled into precompiled headers for use in later compile steps.

这意味着编译

gcc x.c y.h

将创建两个产品:从x.c生成的可执行文件和从y.h生成的预编译头.

will create two products: an executable generated from x.c and a precompiled header generated from y.h.

Gcc(至少在6.3版之前)可以指定一个显式的输出文件名,尽管我相信其结果是预编译的头文件被编写为x,然后被可执行文件覆盖. (在大多数其他情况下,例如,当您使用-c-S-E选项为每个输入生成输出时,它不允许您指定显式的输出名称.) ,当您使用-o选项显式输出文件名并且具有多个输出时,即使该输出是预编译的标头(您可能不打算产生),也会产生错误.

Gcc (at least up to version 6.3) lets you specify an explicit output filename in this case, although I believe the consequence is that the precompiled header file is written as x and then overwritten by the executable. (It doesn't let you specify an explicit output name in most other cases, such as when you use the -c, -S or -E options to produce an output for every input.) But clang, possibly more sensibly, produces an error when you an explicit output filename with the -o option and you have more than one output, even when that output is a precompiled header (which you possibly didn't intend to produce).

(令人困惑的是,在Mac OS X上,命令gcc通常会调用clang编译器.我想这样做是为了避免破坏错误地认为gcc是C编译器通用名称的脚本.)

(Confusingly, on Mac OS X, the command gcc normally invokes the clang compiler. I suppose this is to avoid breaking scripts which incorrectly believe that gcc is the generic name for a C compiler.)

解决方案是从要编译的文件列表中删除头文件.

The solution is to remove the header files from the list of files to be compiled.

这篇关于生成多个输出文件时无法指定-o [C错误]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:36