Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using #define to include another file in C++/C

Tags:

c++

c

I want to define a macro which includes another header file like so:

#define MY_MACRO (text) #include "__FILE__##_inline.inl"

So that when the preprocessor parses file person.h, MY_MACRO(blahblah) expands to

#include "person.h.inline.inl"

any hints on how to do this ?

like image 439
Ahmad Mushtaq Avatar asked Jul 05 '10 11:07

Ahmad Mushtaq


People also ask

What is the English meaning of uses?

transitive verb. 1 : to put into action or service : avail oneself of : employ. 2 : to expend or consume by putting to use —often used with up. 3 : stand sense 1d the house could use a coat of paint. 4 : to consume or take (liquor, drugs, etc.)

What is the verb of using?

verb (used with object), used, us·ing. to employ for some purpose; put into service; make use of: to use a knife. to avail oneself of; apply to one's own purposes: to use the facilities. to expend or consume in use: We have used the money provided.

What is the word for using someone?

An exploiter is a user, someone who takes advantage of other people or things for their own gain. Being an exploiter is selfish and unethical. To exploit someone is to use them in a way that's wrong, like an employer who pays low wages but demands long hours.


3 Answers

It's not possible to use #define to construct other preprocessor directives, unless you run the preprocessor twice.

But in your case even running the preprocessor twice won't help because the #include must be a single string of the form "..." or <...>.

like image 85
kennytm Avatar answered Oct 22 '22 01:10

kennytm


You cannot use __FILE__ because that is already quoted, and #include doesn't support string concatenation. But you can use macros after #include:

#define STRINGIZE_AUX(a) #a
#define STRINGIZE(a) STRINGIZE_AUX(a)
#define CAT_AUX(a, b) a##b
#define CAT(a, b) CAT_AUX(a, b)
#define MY_MACRO(file, name) STRINGIZE(CAT(file, CAT(name, _inline.inl)))
#include MY_MACRO(aaaa, qqq)

You should use the equivalent Boost.Preprocessor macros instead of CAT and STRINGIZE to prevent global namespace pollution.

like image 42
Philipp Avatar answered Oct 21 '22 23:10

Philipp


You can't write other pre-processor directives using the pre-processor. However, I believe you could define just the file name:

#define MY_MACRO(name) "__FILE__##name_inline.inl"

#include MY_MACRO(name)

The pre-processor runs multiple times until there are no further substitutions it can make, so it should expand the name first and then #include the referenced file.

EDIT: I just tried it and the pre-processor can't handle the quotes like that.

#define MY_MACRO(x) <__FILE__##x_inline.inl>
#include MY_MACRO(foo)

works OK, but <> may not be what you wanted.

EDIT2: As pointed out by sth in comments, the __FILE__ does not expand correctly, which makes this probably not what you want after all. Sorry.

like image 3
Vicky Avatar answered Oct 21 '22 23:10

Vicky