I'm trying to generate include file name in macro. This is supposed to be legal in C++:
#define INCLUDE_FILE "module_impl_win.hpp" #include INCLUDE_FILE
this works fine, but as soon as I try to generated file name it failes to compile
#define INCLUDE_FILE(M) M##"_impl_win.hpp" #include INCLUDE_FILE("module")
Actually it gives me warning on MSVC2010
warning C4067: unexpected tokens following preprocessor directive - expected a newlin
but it doesn't include the file.
What is the problem? How can I get rid of it?
__FILE__ This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in ' #include ' or as the input file name argument. For example, "/usr/local/include/myheader.
Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z' , 'A-Z' , '0-9' , and '_' , and the first character should not be a digit.
__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
Macros and its types in C/C++ A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro.
I'd do this using a helper quoting macro. Something like this will give you what you want:
#define QUOTEME(M) #M #define INCLUDE_FILE(M) QUOTEME(M##_impl_win.hpp) #include INCLUDE_FILE(module)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With