|
|
preprocessor macro problem
Hi,
I want to have a macro that includes a bunch of
include files and functions into my code when called.
Here is an example:
#define MY_MACRO() #include <a.h>\
#include <b.h>\
...
f()\
{\
printf("Hello\n");\
}\
This is not syntacally right since the "#" before the include
is interpreted as a stringizig char and an error is flagged by
the compiler. Also, all the code is placed n a single line,
the includes flag an error. How can you write a macro like this??
I have a large number of files in which I need to include
code like this. Any help is greatly appreciated.
|
|
0
|
|
|
|
Reply
|
Erik
|
1/30/2011 7:15:16 PM |
|
In article <ii4dc4$rp2$1@speranza.aioe.org>,
Erik Arle <nospam@nospam.com> wrote:
>Hi,
> I want to have a macro that includes a bunch of
> include files and functions into my code when called.
> Here is an example:
>
>#define MY_MACRO() #include <a.h>\
>#include <b.h>\
>..
>f()\
>{\
> printf("Hello\n");\
>
>}\
Here is a suggestion. Let's say your file is myfile.c.
Make a file myfile-includes.c that has all the material
that you want to include. Then in myfile.c put
#include "myfile-includes.c"
--
rr
|
|
0
|
|
|
|
Reply
|
rouben
|
1/30/2011 7:26:53 PM
|
|
On 1/30/2011 2:15 PM, Erik Arle wrote:
> Hi,
> I want to have a macro that includes a bunch of
> include files and functions into my code when called.
> Here is an example:
>
> #define MY_MACRO() #include<a.h>\
> #include<b.h>\
> ..
> f()\
> {\
> printf("Hello\n");\
>
> }\
>
> This is not syntacally right since the "#" before the include
> is interpreted as a stringizig char and an error is flagged by
> the compiler. Also, all the code is placed n a single line,
> the includes flag an error. How can you write a macro like this??
Only by using some other language than C. In C, macro
expansion cannot produce a preprocessor directive, even if it
yields a sequence of tokens that look like one.
> I have a large number of files in which I need to include
> code like this. Any help is greatly appreciated.
Without knowing what you "need" and why, I can't offer much
besides generic suggestions. The main one is to jettison MY_MACRO
altogether, since even if it worked it wouldn't be very useful.
(You couldn't use it more than once in a source file, and you
need to #define it someplace, and if you're using it in "a large
number of files" you'll probably #define it in a header, and if
you're going to #include that header it might as well #include the
others without all this running around.)
If you've got a use case that you think isn't addressed by a
perfectly ordinary #include, please explain the situation more
fully. It's quite likely that someone can suggest a solution if
you'll describe your problem.
--
Eric Sosman
esosman@ieee-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
Eric
|
1/30/2011 8:25:44 PM
|
|
Erik Arle <nos...@nospam.com> wrote:
> Hi,
> =A0 =A0 =A0 =A0 I want to have a macro that includes a bunch of
> =A0 =A0 =A0 =A0 include files and functions into my code when called.
Whatever your problem, your attempted solution is wrong in many
ways. I'm reasonably sure there is a solution, but you should
mention what the actual problem is to get the right one.
> =A0 =A0 =A0 =A0 Here is an example:
>
> #define MY_MACRO() #include <a.h>\
> #include <b.h>\
> ..
> f()\
> {\
> =A0 =A0 =A0 =A0 printf("Hello\n");\
>
> }\
There are many acceptable ways of performing conditional
inclusion, but macros that define functions are relatively
rare, especially if the macro takes no parameters.
The function definition can only exist once in the entire
program, so what is the point of making it a macro?
> This is not syntacally right ...
Indeed.
> How can you write a macro like this??
Why are you trying to is a better question.
> I have a large number of files in which I need to include
> code like this.
I seriously doubt you need to include code like that. Rather
than describing your ideal solution, you should show us the
real problem. Don't be frightened to put in less contrived
code.
--
Peter
|
|
0
|
|
|
|
Reply
|
Peter
|
1/31/2011 1:37:40 AM
|
|
|
3 Replies
152 Views
(page loaded in 0.032 seconds)
Similiar Articles: TAO_IDL/tao_idl: preprocessor CC returned with an error - comp ...Hi, > Hi problem here is i am using it for some old applications new ... same behavior occurs for the TAO_IDL_PREPROCESSOR_ARGS and ACE_CC_PREPROCESSOR_ARGS macros. ... Wide Preprocessor Concatenation - comp.lang.c++.moderated ...This works fine -- some obvious problems with double ... of gfortran is the ability to use the C preprocessor of ... was related to trigraphs in C, or cpp macro ... Linking consistency check - comp.lang.c++.moderatedUsing MSVC and wxWindows, I had a problem where a couple wxWindows projects were defining different preprocessor macros, which led to an inconsistent class that over ... Custom assertion macros - comp.lang.c++.moderated... file.name() << ": open failed with error " << error ); Since preprocessor substitutes macro ... I do not use exceptions in my code so it's not real problem at least for ... Variadic Macros - comp.lang.c++.moderatedAvoiding problems with short-circuit primitive operator&& versus ... ... feature of some computer programming languages, especially the C preprocessor, whereby a macro ... Conditional compile in VHDL - comp.lang.vhdlA "partial solution" to this problem is the package generics proposal: http ... www.vhdl.org VHDL has no standard macro preprocessor and no ifdef for conditional ... Is this a macro bug??? - comp.soft-sys.sas... mname = break) There is no good fix for the problem since macros ... macro bug??? - comp.soft-sys.sas LCC (CPP) preprocessor bug - comp.compilers.lcc Is this a macro bug ... Commercial Fortran Compilers - comp.lang.fortran... that is used by many codes is the use of the C > preprocessor. I think there is a language problem ... of ... was related to trigraphs in C, or cpp macro ... error C2375 - redefinition; different linkage - comp.lang.c ...... malloc)' won't find it since it's not a #define preprocessor ... Do you know of any particular problem using this kind ... to #undef > something that isn't defined as a macro. How to simulate variadic templates? - comp.lang.c++.moderated ...Hi all, With a current problem, I found the variadic ... about replication of coude you can (ab)use the preprocessor ... time (just define NEW_MAX_ARGUMENTS_NUMBER macro). Chapter 6: Preprocessor - Steve Oualline, The Practical ProgrammerThis another example of how the C preprocessor can hide problems. Clearly LENGTH is 10 ... As you've already seen, the #define statement is a problem. SIZE is a macro and ... The C Preprocessor: 1. The C PreprocessorThe C preprocessor is a macro processor that is used automatically by the C ... Parentheses around the entire macro definition can prevent such problems. 7/16/2012 4:48:20 PM
|
|
|
|
|
|
|
|
|