macro containing include statement

J

Joakim Hove

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 -
actually includes MPI source */

#MPIinclude(FileName) /* Alternative two -
a noop */



So that the body of the code can contain:

MPIinclude("path/to/mpi_code/mpi_file.c")

However, when preprosessing the macro definition above I get the
following error message (from gnu cpp):

fphs.c:20:32: '#' is not followed by a macro parameter

and the resulting code later fails in the eventual compilation.

Is this possible?

Joakim
 
C

Capstar

Joakim said:
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 -
actually includes MPI source */

#MPIinclude(FileName) /* Alternative two -
a noop */



So that the body of the code can contain:

MPIinclude("path/to/mpi_code/mpi_file.c")

However, when preprosessing the macro definition above I get the
following error message (from gnu cpp):

fphs.c:20:32: '#' is not followed by a macro parameter

and the resulting code later fails in the eventual compilation.

Is this possible?

Joakim

Try something like this:

#ifdef USE_MPI_SOURCE
#define MPI(file) "file"
#else
#define MPI(file)
#endif

#include MPI(path/to/mpi_code/mpi_file.c)

Mark
 
J

Joakim Hove

Hello,

thanks for answering. However unfortunately it does not work (at least
not with gnu cpp):
Try something like this:

#ifdef USE_MPI_SOURCE
#define MPI(file) "file"
#else
#define MPI(file)
#endif

[...]
#include MPI(path/to/mpi_code/mpi_file.c)

When the variable USE_MPI_SOURCE is defined I get:

bash% cpp src_file.c
src_file.c:405:35: file: No such file or directory

whereas when it is not defined I get:

bash% cpp src_file.c
src_file.c:405:35: #include expects "FILENAME" or <FILENAME>

It seemed such a simple construction, but it does contain nested
preprocessor directives, and that is maybe illegal?

Regards

Joakim
 
F

Fred L. Kleinschmidt

Joakim said:
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 -
actually includes MPI source */

#MPIinclude(FileName) /* Alternative two -
a noop */

So that the body of the code can contain:

MPIinclude("path/to/mpi_code/mpi_file.c")

However, when preprosessing the macro definition above I get the
following error message (from gnu cpp):

fphs.c:20:32: '#' is not followed by a macro parameter

and the resulting code later fails in the eventual compilation.

Is this possible?

Joakim

--
Joakim Hove
hove AT ift uib no
+47 (55 5)8 27 90
http://www.ift.uib.no/~hove/

#ifdef USE_ALTERNATIVE_ONE
#include "path/to/mpi_code/mpi_file.c"
#endif
 
C

Capstar

Joakim said:
Hello,

thanks for answering. However unfortunately it does not work (at least
not with gnu cpp):

Try something like this:

#ifdef USE_MPI_SOURCE
#define MPI(file) "file"
#else
#define MPI(file)
#endif

[...]


#include MPI(path/to/mpi_code/mpi_file.c)


When the variable USE_MPI_SOURCE is defined I get:

bash% cpp src_file.c
src_file.c:405:35: file: No such file or directory

Here you're probably just referencing the wrong file.
whereas when it is not defined I get:

bash% cpp src_file.c
src_file.c:405:35: #include expects "FILENAME" or <FILENAME>

It seemed such a simple construction, but it does contain nested
preprocessor directives, and that is maybe illegal?

This makes sence actually, if you don't define USE_MPI_SOURCE, the line

#include MPI(something)

will expand into

#include

And there must be something after #include

So what you can do is make a empty header file called nothing.h and do
the following:

#ifdef USE_MPI_SOURCE
#define MPI(file) "file"
#else
#define MPI(file) "nothing.h"
#endif

I got this from the following thread, the first time by heart, the
second time I looked it up. :)

http://groups.google.nl/groups?hl=nl&lr=&ie=UTF-8&[email protected]

Mark
 
J

Joakim Hove

Eric said:
Why not using a common way:

#ifdef USE_MPI_SOURCE
#include "file"
#endif

That is what I ended up doing. The reason I posed the question was
that I dislike having preprocessor directives sparsely distributed
through the whole code.

Anyway - thanks for taking time on my post :)

Joakim
 
E

Eric Sosman

Joakim said:
That is what I ended up doing. The reason I posed the question was
that I dislike having preprocessor directives sparsely distributed
through the whole code.

Perhaps you could put the #ifdef/#endif in the
included file itself, instead of at each inclusion.
That is,

/* mpi_file.h */
#ifdef USE_MPI_SOURCE
...
#endif

/* anyfile.c */
#include "mpi_file.h"
...

/* otherfile.c */
#include "mpi_file.h"
...
 
A

Arthur J. O'Dwyer

Here you're probably just referencing the wrong file.

Here, Mark made a brain fart. Forread
#define MPI(file) #file
This makes sence actually, if you don't define USE_MPI_SOURCE, the line

#include MPI(something)

will expand into

#include

And there must be something after #include

So what you can do is make a empty header file called nothing.h and do the
following:

#ifdef USE_MPI_SOURCE #define MPI(file) #file
#else
#define MPI(file) "nothing.h"
#endif


This works, but it's not nearly as simple as just writing

#ifdef USE_MPI_SOURCE
#include "path/to/mpi/file.h"
#endif

in the first place.

-Arthur
 
C

Capstar

Arthur said:
On Wed, 18 Aug 2004, Capstar wrote:


Here, Mark made a brain fart. For


read
#define MPI(file) #file

Yup, thanks for correcting that.
#define MPI(file) #file




This works, but it's not nearly as simple as just writing

#ifdef USE_MPI_SOURCE
#include "path/to/mpi/file.h"
#endif

True, that's what I'd also use. But it's not what the OP was asking for.
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top