Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destringification in the gcc preprocessor

Is it possibly to write a gcc macro that destringifies a string literal argument? I know the opposite is possible with #. Can it be reversed?

For example, __func__ evaluates to the name of the current function in the form of a string literal. Can I remove the double quotation marks around it?

like image 475
PSkocik Avatar asked Aug 04 '15 10:08

PSkocik


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.

Does preprocessor remove all comments?

Removing comments : It removes all the comments. A comment is written only for the humans to understand the code. So, it is obvious that they are of no use to a machine. So, preprocessor removes all of them as they are not required in the execution and won't be executed as well.

What is the gcc option that runs only the preprocessor?

The -E option causes gcc to run the preprocessor, display the expanded output, and then exit without compiling the resulting source code.


1 Answers

No, it is not possible to convert "foo" to foo using the standard C/C++ preprocessor. If you absolutely need this, you would need to create an external preprocessing program, similar to what Qt does with its moc tool.

Not completely on-topic, but the D language has exactly this functionality - it's called mixins. You can execute arbitrary string processing code at compile time and "paste" the result into your source file with the mixin keyword.

like image 115
Krzysztof Kosiński Avatar answered Sep 30 '22 20:09

Krzysztof Kosiński