Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro substitution in #include directive

I would like to use an #include directive with a file name that is passed as an externally defined macro.

E.g.

#include #FILE".h"

where FILE would be defined as the string MyFile (without quotes), resulting in

#include "MyFile.h"

The stringizing operator # cannot be used here as the symbol FILE is not a macro argument. I have tried other approaches, to no avail.

Do you see a solution ?

like image 494
Yves Daoust Avatar asked Oct 27 '15 08:10

Yves Daoust


People also ask

What is Micro substitution?

In microeconomics, two goods are substitutes if the products could be used for the same purpose by the consumers. That is, a consumer perceives both goods as similar or comparable, so that having more of one good causes the consumer to desire less of the other good.

What is macro in C with example?

A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive. Here's an example. Here, when we use c in our program, it is replaced with 299792458 .

What is #define in mql4?

The #define directive substitutes expression for all further found entries of identifier in the source text. The identifier is replaced only if it is a separate token. The identifier is not replaced if it is part of a comment, part of a string, or part of another longer identifier.

What are macros in programming?

In computer programming, a macro (short for "macro instruction"; from Greek μακρο- 'long, large') is a rule or pattern that specifies how a certain input should be mapped to a replacement output. Applying a macro to an input is known as macro expansion.


1 Answers

String literal concatenation happens two translation phases after #include-directives are resolved; your approach cannot work. Instead, try something along the lines of

#define STRINGIZE_(a) #a
#define STRINGIZE(a) STRINGIZE_(a)

#define MYFILE stdio
#include STRINGIZE(MYFILE.h)

int main() {
    printf("asdf");
}

Demo.

like image 145
Columbo Avatar answered Oct 29 '22 18:10

Columbo