Jackie
Jackie

Reputation: 23637

How to #define variable in C

I have a file called config.h with the following...

#define GL_DOOM

Then I have the following in another file m_misc.c...

#include "config.h"
...
#if ((defined GL_DOOM) && (defined _MSC_VER))
LOGD("Using glboom-plus.cfg");
#define BOOM_CFG "glboom-plus.cfg"
#else
LOGD("Using prboom-plus.cfg");
#define BOOM_CFG "prboom-plus.cfg"
#endif

But it says...

05-02 14:40:24.789: D/Doom(2966): Using prboom-plus.cfg

What is the deal here? I am new to C so what am I missing?

Upvotes: 0

Views: 236

Answers (3)

CatchMeIfYouTry
CatchMeIfYouTry

Reputation: 94

The check

#if ((defined GL_DOOM) && (defined _MSC_VER)) 

will only evaluate to true if both the conditions are met. You have not specified whether you have defined _MSC_VER in the header file. That is probably the reason why it is executing the else condition.

If you want to log when either of those are defined in the header file, use the OR operation instead:

#if (defined (GL_DOOM)) || (defined (_MSC_VER))

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 82026

Let's take the following code:

#define GL_DOOM
#define _MSC_VER

#if ((defined GL_DOOM) && (defined _MSC_VER))
LOGD("Using glboom-plus.cfg");
#else
LOGD("Using prboom-plus.cfg");
#endif

I can compile that code with g++ -E which will output the result of the preprocessor. Let's look at that output.

# 1 "blah.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "blah.c"

LOGD("Using glboom-plus.cfg");

So, to me, this implies that you probably don't have both GL_DOOM and _MSC_VER defined.


You might verify this with a test that looks something like:

#include "config.h"

#ifndef GL_DOOM
#error GL_DOOM is not defined
#endif

#ifndef _MSC_VER
#error _MSC_VER is not defined
#endif

It's also worth noting something. _MSC_VER is a preprocessor symbol that is defined almost strictly by Microsoft Visual Studio. If you're not compiling with that software, then the expectation would be that it would not be defined.

Upvotes: 4

Daniel Fischer
Daniel Fischer

Reputation: 183978

#include "config.h"

that includes the #define GL_DOOM.

...
#if ((defined GL_DOOM) && (defined _MSC_VER))

That checks whether both, GL_DOOM and _MSC_VER are defined.

Since GL_DOOM is defined, either your preprocessor is totally broken, or _MSC_VER is not defined.

Upvotes: 2

Related Questions