preprocessor macro problem

E

Erik Arle

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.
 
N

none

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"
 
E

Eric Sosman

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.
 
P

Peter Nilsson

Erik Arle said:
Hi,
        I want to have a macro that includes a bunch of
        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.
        Here is an example:

#define MY_MACRO() #include <a.h>\
#include <b.h>\
..
f()\
{\
        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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

preprocessor bug? 2
Order of preprocessor macro replacement 3
Preprocessor 0
Preprocessor commands usage. 11
preprocessor 2
Command Line Arguments 0
C Preprocessor 13
Odd Macro Problem 3

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top