Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate include file name in a macro

Tags:

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?

like image 364
ledokol Avatar asked Jan 20 '11 05:01

ledokol


People also ask

What is __ file __ in C++?

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

What can you not include in a macro name?

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.

What is __ LINE __ in C?

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

What is file macro in C?

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.


1 Answers

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) 
like image 80
Andrew Avatar answered Oct 05 '22 14:10

Andrew