473,469 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

macro definition contains #include directive

Hi folks,
Is there a way to define a macro that may contain #include
directive in its body. If I just put
the "#include", it gives error C2162: "expected macro formal
parameter" since here I am not using
# to concatenate strings. If I use "\# include", then I receive the
following two errors:

error C2017: illegal escape sequence
error C2121: '#' : invalid character : possibly the result of a macro
expansion

Any help?

Thanks,
Bing Jian

Nov 5 '08 #1
6 6951
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bing wrote:
Is there a way to define a macro that may contain #include
directive in its body. If I just put
<snip>

No, preprocessor directives in C++ (and C) are not reflective.

Pawel Dziepak

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkR/5QACgkQPFW+cUiIHNrengCgmsl1wT5R9t4lKh2iu+f2xsPO
uNMAn0Q4VWBUeVUkLN6UsDB0FzkE3AY5
=BCqQ
-----END PGP SIGNATURE-----
Nov 5 '08 #2
Bing wrote:
Hi folks,
Is there a way to define a macro that may contain #include
directive in its body. If I just put
the "#include", it gives error C2162: "expected macro formal
parameter" since here I am not using
# to concatenate strings. If I use "\# include", then I receive the
following two errors:

error C2017: illegal escape sequence
error C2121: '#' : invalid character : possibly the result of a macro
expansion

Any help?
Look at the boost preprocessor metaprogramming library. The
BOOST_PP_ITERATE() macro is able to do it. See what it's doing or maybe
just use it.
Nov 5 '08 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Noah Roberts wrote:
Look at the boost preprocessor metaprogramming library. The
BOOST_PP_ITERATE() macro is able to do it. See what it's doing or maybe
just use it.
Macro can be an argument of #include directive and that's how
BOOST_PP_ITERATE() is used in boost (it's probably the solution for
Bing's problem). However, macro can't contain #include directive.

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkSEnAACgkQPFW+cUiIHNoI5QCfe8p5BsWgNF 5Lc/gkTx+wEkLt
vuMAmgL1KlnhEUdUfeHDUjmapG7VWo5D
=ir8y
-----END PGP SIGNATURE-----
Nov 5 '08 #4
On Nov 5, 4:38 pm, Pawel Dziepak <pdzie...@quarnos.orgwrote:
>
Noah Roberts wrote:
Look at the boost preprocessor metaprogramming library. The
BOOST_PP_ITERATE() macro is able to do it. See what it's doing or maybe
just use it.

Macro can be an argument of #include directive and that's how
BOOST_PP_ITERATE() is used in boost (it's probably the solution for
Bing's problem). However, macro can't contain #include directive.

Pawel Dziepak
Thanks for the answers. Just let you know I started a topic on this at
stackoverflow.com
http://stackoverflow.com/questions/2...lude-directive
Nov 5 '08 #5
On Nov 5, 9:06 pm, Bing <bing.j...@gmail.comwrote:
Is there a way to define a macro that may contain #include
directive in its body. If I just put the "#include", it
gives error C2162: "expected macro formal parameter" since
here I am not using # to concatenate strings. If I use "\#
include", then I receive the following two errors:
error C2017: illegal escape sequence
error C2121: '#' : invalid character : possibly the result of a macro
expansion
Any help?
No. After expansion, "the resulting completely macro-replaced
preprocessing token sequence is not processed as a preprocessing
directive even if it resembles one" [§16.3.4/3]. In other
words, even if there were a way of introducing a sequence
"#include" in the expansion, it wouldn't be an include
directive.

What problem are you trying to solve with this?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 6 '08 #6
Bing <bi*******@gmail.comwrites:
Hi folks,
Is there a way to define a macro that may contain #include
directive in its body. If I just put
the "#include", it gives error C2162: "expected macro formal
parameter" since here I am not using
# to concatenate strings. If I use "\# include", then I receive the
following two errors:

error C2017: illegal escape sequence
error C2121: '#' : invalid character : possibly the result of a macro
expansion

Any help?
It is not possible with cpp, however it's trivial to do with make and
sed (or awk or anything else):

-----------(Makefile)--------------------------

example.c : example.cm
sed -e 's/EXPAND(\(.*\))/@#define SOMEVAR 1@#include <\1.h>@#define SOMEVAR_\1 2@/g' < example.cm \
|tr '@' '\012' example.c

# ...
-----------------------------------------------
----------(example.cm)-------------------------
/* -*- mode:c -*- */

EXPAND(ModuleA)

EXPAND(ModuleB)

/**** THE END ****/
-----------------------------------------------

Then typing make example.c will generate the file:

-------------(example.c)------------------------------
/* -*- mode:c -*- */
#define SOMEVAR 1
#include <ModuleA.h>
#define SOMEVAR_ModuleA 2

#define SOMEVAR 1
#include <ModuleB.h>
#define SOMEVAR_ModuleB 2
/**** THE END ****/
-------------------------------------------------------

--
__Pascal Bourguignon__
Nov 6 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Joakim Hove | last post by:
Hello, I would like to construct a macro, which does conditional include of another source file. i.e. something like this: #MPIinclude(FileName) (#include (FileName)) /* Alternative one -...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
13
by: Rick Anderson | last post by:
Group, I want to define a #include directive from another macro, if possible... For example, the following doesn't work but is basically what I need to do: #define...
3
by: casul | last post by:
Hi All, I was told there were a few macro gurus on this group :) I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE )
19
by: Tor Rustad | last post by:
I'm implementing a parser, where there is a lot of different data elements defined. Now instead of hand-coding X defines, and the massive number of lines needed for table initialization, I decided...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
3
by: Ernie.Pasveer | last post by:
Hi All, Is there a way to create a macro called #redefine that will allow redefinition without having to use #ifdef/#undef For example, I'd like to do something like this : #define THING ...
16
by: mdh | last post by:
I have asked a few questions about Macros...and think what I have been missing ...and which all who have replied clearly understand...is that this is a Pre-processor action. Hopefully the above is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.