cannot specify -o when generating multiple output

2019-05-11 21:19发布

问题:

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

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

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.

That means that compiling

gcc x.c y.h

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

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

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



标签: c makefile clang