1

Using GCC under Windows, I would like to be able to specify on the gcc command line (or from a manually managed makefile) the name of a specific include file to be included in the file being compiled. As I see it, ideally the source code file would contain a line something like…</p>

#include INCLUDEFILENAME 

…then a filename specified on the gcc command line would be substituted for the INCLUDEFILENAME text.

It seems I can get close to achieving this by defining a macro called INCLUDEFILENAME on the gcc command line using the -D option (eg. -D INCLUDEFILENAME="C:\TestLib\Test1.h") but when the filename text gets substituted into the #include statement it is not enclosed in double quotes, and without these it is not recognized as a file to be included. Of course…</p>

#include "INCLUDEFILENAME" 

…doesn’t work as INCLUDEFILENAME then becomes a string literal and does not get replaced by the macro value. I have tried other ways of specifying the double quotes (\x22, \", "\"", etc) but these don’t seem to work on the #include line.

I am aware of the gcc -include option which it seems can force a file to be included without it being mentioned in any way in the source file, but I would prefer that the source file indicates that an include file is to be included here but that it’s name is specified “externally” to the source file (ultimately, from the makefile).

Any suggestions on how I can achieve this would be appreciated.

4

2 回答 2

0

您必须包含双引号"作为定义的一部分(或<>,视情况而定):

% cat test.c

#include <stdio.h>
#include OTHERFILE

int main() { printf("%s\n", func()); }

% cat func.c

char *func() { return "It worked."; }

% gcc test.c -DOTHERFILE='"func.c"'
% ./a.out
It worked.
%
于 2012-05-10T17:17:02.197 回答
0

您可以使用丑陋但经典的字符串化技巧:

#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#include STRINGIFY(INCLUDEFILENAME)
于 2016-07-11T15:23:35.553 回答