Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a preprocessor variable in #include directive?

Tags:

c++

This is what I'm trying to do:

$ c++ -D GENERATED=build/generated-content main.cpp

My main.cpp file:

#include "GENERATED/header.h"
void f() { /* something */ }

Currently this code fails to compile. How should I fix it? And whether it's possible at all?

like image 535
yegor256 Avatar asked Dec 08 '22 02:12

yegor256


1 Answers

It seems you want to use different headers depending on some "compilation profile".

Instead of the -Dsolution, I would rather suggest using the -I directive to specify the include directories.

Given you have the following file tree:

/
  debug/
    header.h
  release/
    header.h

main.cpp:

#include "header.h"

/* some instructions, not relevant here */

And in your Makefile (or whatever tool you use), just specify the proper include directory to use, depending on whatever reason you want:

g++ -I debug main.cpp // Debug mode

g++ -I release main.cpp // Release mode

Important foot note: I don't know if you intended to use this as a debug/release switch. However, doing so would be weird: the interface (the included .h files) shouldn't change between release and debug. If you ever need this, the usual way is to enable/disable some parts of the code using defines, mostly in .c (and .cpp) files.

like image 159
ereOn Avatar answered Dec 09 '22 14:12

ereOn