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…

#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…

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

emu
  • 1,597
  • 16
  • 20
Martin Irvine
  • 161
  • 1
  • 2
  • 8

2 Answers2

0

You have to include the double quotes " as part of the define (or <>, as the case may be):

% 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.
%
Kevin
  • 53,822
  • 15
  • 101
  • 132
0

You can use the ugly but classic stringification trick:

#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#include STRINGIFY(INCLUDEFILENAME)
emu
  • 1,597
  • 16
  • 20