473,379 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Macro to cause build failure if parameter not defined?

Is there a way to cause a compile/preprocessor error in a macro if a
preprocessor symbol is not defined? I want to do the equivalent of
this in a macro:

#if( !defined( SOME_FLAG ) )
#error not defined
#endif

What I want is a way to test if a feature flag is defined to 1 such
that if it's not defined at all (as opposed to defined to 0), I'll get
a compile error. This is to catch cases where people test for a
feature flag without including the right header file, using the right
compile flags, or they just typed it wrong. I want to use it like
this:

#if( HAS_FEATURE( FEATURE_X ) )
... do something if FEATURE_X is defined
#endif

I'll #define FEATURE_X to 1 if supported or #define FEATURE_X to 0 if
not suported. If not defined at all, I want to generate a compile
error.

I know some compilers have options to warn about #if used with
undefined symbols, but I wanted something more portable.
Jun 27 '08 #1
7 6015
Hi

On Wed, 25 Jun 2008 14:54:18 -0700, sk*******@gmail.com wrote:
Is there a way to cause a compile/preprocessor error in a macro if a
preprocessor symbol is not defined? I want to do the equivalent of this
in a macro:

#if( !defined( SOME_FLAG ) )
#error not defined
#endif
What's wrong with:

#ifndef SOME_FLAG
#error not defined
#endif

Not all preprocessors have #error, but it should be an error in any case.

viza
Jun 27 '08 #2
On Jun 25, 3:17*pm, viza <tom.v...@gmil.comwrote:
What's wrong with:

#ifndef SOME_FLAG
#error not defined
#endif
That would require a lot of extra code to test for features (#error if
not defined then test for the feature). I'm trying to automate it so
you just use the macro and it does it all in one shot.
Jun 27 '08 #3
"sk*******@gmail.com" <sk*******@gmail.comwrites:
On Jun 25, 3:17*pm, viza <tom.v...@gmil.comwrote:
What's wrong with:

#ifndef SOME_FLAG
#error not defined
#endif

That would require a lot of extra code to test for features (#error if
not defined then test for the feature). I'm trying to automate it so
you just use the macro and it does it all in one shot.
The expansion of a macro cannot contain preprocessor directives.

But it's not all that much extra code. It's 3 lines per feature
rather than 1. I'm not sure what you mean by "#error if not defined
then test for the feature"; are you making this more complicated than
it needs to be?

You can even test multiple features in one line, at the expense of not
being able to have separate error messages:

#if ! ( defined FLAG1 && ! defined FLAG2 && ! defined FLAG3 )
#error "Something is missing"
#endif

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #4
sk*******@gmail.com wrote:
...What I want is a way to test if a feature flag is defined to
1 such that if it's not defined at all (as opposed to defined
to 0), I'll get a compile error. This is to catch cases where
people test for a feature flag without including the right
header file, using the right compile flags, or they just typed
it wrong. I want to use it like this:

#if( HAS_FEATURE( FEATURE_X ) )
... do something if FEATURE_X is defined
#endif

I'll #define FEATURE_X to 1 if supported or #define
FEATURE_X to 0 if not suported. If not defined at all, I
want to generate a compile error.
Nothing is guaranteed to produce an error (not even #error
in C90,) but you can try (untested)...

#define CAT(a,b) a ## b
#define CAT2(a,b) CAT(a,b)
#define HAS_FEATURE_CHECK_0 1
#define HAS_FEATURE_CHECK_1 1
#define HAS_FEATURE(X) (X / CAT2(HAS_FEATURE_CHECK_,X))

Will also fail (or at least diagnose division by zero) if the
feature is not defined to be 0 or 1.

--
Peter
Jun 27 '08 #5
On 25 Jun, 23:17, viza <tom.v...@gmil.comwrote:
Not all preprocessors have #error [...]
all ISO-C compliant one do though

--
Nick Keighley
Jun 27 '08 #6
sk*******@gmail.com wrote:
Is there a way to cause a compile/preprocessor error in a macro if a
preprocessor symbol is not defined? I want to do the equivalent of
this in a macro:

#if( !defined( SOME_FLAG ) )
#error not defined
#endif

What I want is a way to test if a feature flag is defined to 1 such
that if it's not defined at all (as opposed to defined to 0), I'll get
a compile error. This is to catch cases where people test for a
feature flag without including the right header file, using the right
compile flags, or they just typed it wrong. I want to use it like
this:

#if( HAS_FEATURE( FEATURE_X ) )
... do something if FEATURE_X is defined
#endif

I'll #define FEATURE_X to 1 if supported or #define FEATURE_X to 0 if
not suported. If not defined at all, I want to generate a compile
error.
I think

#define HAS_FEATURE(macro) ( (macro) / defined(macro) )

.... might suit you, if you don't mind an uninformative or maybe
even misleading diagnostic.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #7
Eric Sosman wrote:
sk*******@gmail.com wrote:
#if( HAS_FEATURE( FEATURE_X ) )
... do something if FEATURE_X is defined
#endif

I'll #define FEATURE_X to 1 if supported or #define FEATURE_X to 0 if
not suported. If not defined at all, I want to generate a compile
error.

I think

#define HAS_FEATURE(macro) ( (macro) / defined(macro) )

... might suit you, if you don't mind an uninformative or maybe
even misleading diagnostic.
That runs afoul of 6.10.1p4 [n1256]: "If the token defined is
generated as
a result of [the] replacement process ... the behavior is undefined."

--
Peter
Jun 27 '08 #8

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

Similar topics

0
by: Jim Zafrani | last post by:
I have an interesting problem. I have a vb.net project that uses COM objects which use ADO 2.8. The vb.net project itself uses ADO 2.6. When I rebuild the COM objects (C++ projects) and then...
7
by: tomtailor | last post by:
Hello! I set up DB2 Personal Edition on Fedora C4 Linux. During the Installprocess everything looks fine. But if I try to create a Database DB2 says: "Routine "db2spcat" (specific name...
0
by: Spiro Trikaliotis | last post by:
Hello, assume I define a macro like #if <somecondition> # define MACRO(_x, _y) _x #else # define MACRO(_x, _y) _y #endif
0
by: Sunny | last post by:
Hi I have made a Database Application for which i am trying to create a SETUP Project. The application uses three custom Ms access Databases. Those database i added in the correct folder in the...
0
by: Jared | last post by:
Anyone no the setting for that?
3
by: SANJINJIT | last post by:
Hi, I have a spreadsheet looks like this: QID C1 C2 C3 C4 %a %b %c %d 13123 b b b b 0% 100% 0% 0% 13124 d d d d 0% 0% 0% ...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: M.-A. Lemburg | last post by:
On 2008-10-25 08:39, Akira Kitada wrote: Please post a bug report on python.org about these failures. The multiprocessing module is still fairly new and obviously needs more fine tuning for...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.