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

Home Posts Topics Members FAQ

Preprocessor directive including another one

Hi all C speakers,

I would like to implement a macro which sets a field in a struct only
if a certain preprocessor symbol has been defined.

That is how one would naively implement this:
---------------------------------8<-------------------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct thing {
int bar;
int foo;
} Thing;

#ifndef CONFIG_NOFOO
#define REGISTER_FOO(foo_) \
.foo = foo_ \
#endif

Thing thing1 = {
.bar= 1,
REGISTER_FOO(42),
};

void thing_print(Thing *t)
{
printf("bar: %d\n", t->bar);
printf("foo: %d\n", t->foo);
}

int main(void)
{
thing_print(&thing1);
exit(0);
}
---------------------------------8<-------------------------------

And this is the (obvious) error message I got when compiling it:
gcc -I/home/stefano/include/ -g -pg -I/home/stefano/include/ -L/home/stefano/lib recmacro.c -o recmacro
recmacro.c:12:2: error: '#' is not followed by a macro parameter
recmacro.c:16: error: initializer element is not constant
recmacro.c:16: error: (near initialization for ‘thing1.foo’)
recmacro.c:9:1: error: unterminated #ifndef
make: *** [recmacro] Error 1

So I wonder if there is some way to escape the '#' sign, also if this
is possible will the preprocessor expand again the result of the
first expansion?

If this is not the right approach to implement this, can you suggest a
valid one?

Many thanks in advance, regards.
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
Jun 27 '08 #1
4 2054
On 2008-05-29, Stefano Sabatini <st**************@caos.orgwrote:
Hi all C speakers,

I would like to implement a macro which sets a field in a struct only
if a certain preprocessor symbol has been defined.

That is how one would naively implement this:
---------------------------------8<-------------------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct thing {
int bar;
int foo;
} Thing;

#ifndef CONFIG_NOFOO
#define REGISTER_FOO(foo_) \
.foo = foo_ \
#endif
Well, I really meant:

#define REGISTER_FOO(foo_) \
#ifndef CONFIG_NOFOO \
.foo = foo_ \
#endif
>
Thing thing1 = {
.bar= 1,
REGISTER_FOO(42),
};

void thing_print(Thing *t)
{
printf("bar: %d\n", t->bar);
printf("foo: %d\n", t->foo);
}

int main(void)
{
thing_print(&thing1);
exit(0);
}
---------------------------------8<-------------------------------

And this is the (obvious) error message I got when compiling it:
gcc -I/home/stefano/include/ -g -pg -I/home/stefano/include/ -L/home/stefano/lib recmacro.c -o recmacro
recmacro.c:12:2: error: '#' is not followed by a macro parameter
recmacro.c:16: error: initializer element is not constant
recmacro.c:16: error: (near initialization for ‘thing1.foo’)
recmacro.c:9:1: error: unterminated #ifndef
make: *** [recmacro] Error 1

So I wonder if there is some way to escape the '#' sign, also if this
is possible will the preprocessor expand again the result of the
first expansion?

If this is not the right approach to implement this, can you suggest a
valid one?

Many thanks in advance, regards.
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
Jun 27 '08 #2
Dan
#ifndef CONFIG_NOFOO
#define REGISTER_FOO(foo_) \
.foo = foo_ \
#endif
#ifndef CONFIG_NOFOO
#define REGISTER_FOO(x) foo = x;
#else
#define REGISTER_FOO(x)
#endif
Jun 27 '08 #3

"Stefano Sabatini" <st**************@caos.orgwrote in message
news:sl*****************************@geppetto.reil abs.com...
Hi all C speakers,

I would like to implement a macro which sets a field in a struct only
if a certain preprocessor symbol has been defined.
[...]

The following works for me in C99 mode:
__________________________________________________ _____________
#include <stdio.h>
#include <stdlib.h>

typedef struct thing {
int bar;
int foo;
} Thing;

#ifndef CONFIG_NOFOO
#define REGISTER_FOO(foo_) .foo = foo_
#else
#define REGISTER_FOO(foo_)
#endif

Thing thing1 = {
.bar = 1,
REGISTER_FOO(42)
};

void thing_print(Thing *t)
{
printf("bar: %d\n", t->bar);
printf("foo: %d\n", t->foo);
}

int main(void)
{
thing_print(&thing1);
getchar();
return 0;
}

__________________________________________________ _____________

Jun 27 '08 #4
On 2008-05-29, Chris Thomasson <cr*****@comcast.netwrote:
"Stefano Sabatini" <st**************@caos.orgwrote in message
news:sl*****************************@geppetto.reil abs.com...
>Hi all C speakers,

I would like to implement a macro which sets a field in a struct only
if a certain preprocessor symbol has been defined.
[...]

The following works for me in C99 mode:
__________________________________________________ _____________
#include <stdio.h>
#include <stdlib.h>

typedef struct thing {
int bar;
int foo;
} Thing;

#ifndef CONFIG_NOFOO
#define REGISTER_FOO(foo_) .foo = foo_
#else
#define REGISTER_FOO(foo_)
#endif
Thanks Dan and Chris!

This works fine (I just simplified the no-no logic of the macro and
explicitely set the macro expansion when the CONFIG_NOFOO is expanded
to avoind a ",," syntax error):

--------------8<--------------------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct thing {
int bar;
int foo;
} Thing;

#define CONFIG_NOFOO

#ifdef CONFIG_NOFOO
#define REGISTER_FOO(foo_) .foo = NULL
#else
#define REGISTER_FOO(foo_) .foo = foo_
#endif

Thing thing1 = {
.bar= 1,
REGISTER_FOO(42),
};

void thing_print(Thing *t)
{
printf("bar: %d\n", t->bar);
printf("foo: %d\n", t->foo);
}

int main(void)
{
thing_print(&thing1);
return 0;
}
--------------8<--------------------------------

Best regards
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
Jun 27 '08 #5

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

Similar topics

19
by: qazmlp | last post by:
I hope comp.lang.c will not find the following question as a complete off-topic. I would like to remove ie.comment out the 'cout' statements during compilation(actually preprocessing) time. ...
5
by: Boris Kuznetsov | last post by:
This occurs in an empty project when I add the following string: #using <mscorlib.dll> Can anyone tell me why would #using ... not be working???? MSDN says nothing about this error. Please...
16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
2
by: Gustavo L. Fabro | last post by:
Greetings. Is there a way to run the preprocessor twice? Rephrasing that, is there a way to: #define SOMETHING #pragma OTHERTHING and have the preprocessor in somecode.cpp evaluate: ...
6
by: olivier.grant | last post by:
Hi All, I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE ) and end up with the following preprocessed code : #include NAME.hpp
3
by: artifact.one | last post by:
Hi. What's the practical difference between: #include <header.h> and: #include "header.h"
6
by: 2112 | last post by:
I'm compiling a dll that imports msado15.dll. When I'm using Windows in English, the msado15.dll is located at <drive>:\Program Files\Common Files\System\ADO\msado15.dll". When using Windows in...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
5
by: _dwin | last post by:
Hi, Does anyone know how to implement your own preprocessor directive? For instance, I would like to have a directive which goes like: #<directive_name<parameters, ...> ie. #compress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.