tags:

views:

160

answers:

6
#define cimg_use_jpeg 2

#ifndef cimg_use_jpeg

//code goes here

#endif

I really don't understand...

A: 

It's only executed the first time it's included, when cimg_use_jpeg2 it's not already defined. After that it's defined so you can use that code without getting a "symbol already defined" error.

Oooops, I though it was an include guard. Forget that

sui
A: 

Either you write

#define yoursymbol

#ifdef yoursymbol

or you write

#define yoursymbol somevalue

#if yousymbol == somevalue

don't mix them

EDIT: since people seem to misunderstand what I meant here's a clarification: when you program it is important to code so that other people understand what your intentions are, so if your intention is only to check whether a symbol is defined it is confusing to specify a numerical value.

Anders K.
-1: Off topic, doesn't answer question. He does NOT want the code to execute.
mathepic
You can mix them. See my answer (because I can't put code in a comment).
cape1232
@mathepic was just showing the principle, not off topic. Actually it isn't good style to mix. Either you want to use the value of the macro symbol or you just want to know whether the symbol itself is defined but by mixing you make it unclear for the reader what the intention is with the macro.
Anders K.
@cape1232 just because you can doesn't mean you should
Anders K.
@Anders K. True. Especially since the symbol is defined as 2. However, I like `#define FOO 1` and `#if FOO` better than `#define FOO` and `#ifdef FOO` because then I can toggle the code on and off by changing one character (`1` to `0`) rather than having to change `#define` to `#undef` (or worse deleting the line entirely). Plus I find `#if FOO` easier on my eyes than `#ifdef FOO`. This is all subjective, of course. I do agree, though, that you should pick one or the other approach.
cape1232
A: 

This should definately work as you expect. The code in the #ifndef/#endif block will not even be compiled by the compiler, assuming the cimg_use_jpeg is defined as expected.

So the question is, is the cimg_use_jpeg defined as you expect? Or are you simplifying your question and by virtue of the simplification providing a sample that actually works?

Chris Taylor
+1  A: 

It cannot be executed. Maybe you run incorrect executable version. Printing __DATE__ and __TIME__ to trace or log helps to detect such errors.

Alex Farber
`__DATE__` and `__TIME__` will display correctly if you surround them with backticks. I fixed that for you.
sepp2k
+6  A: 

Every time things similar to this (so-called impossible things) happen me, the reason is: The code I see in my editor is not the code that runs. This can happen in many ways.

  • forgetting to make
  • forgetting to save
  • saving a copy of the code
  • running in a different evironment from the one you compiled to
  • the new code ran but I opened and am looking at the old output
Peter G.
+1 When I'm debugging, I'll `#if 0 /* old code */ #else /* new code */ #endif` so I can try out the fix but be able to revert to the original easily. I sometimes accidentally make changes in `/* old code */` thinking that I'm changing `/* new code */`, and then nothing changes when I recompile.
cape1232
@cape1232 why not use source control? the whole purpose of source control is to allow you to revert, and it's often more manageable than your manual version control...
atk
@atk I do it like cape1232 for changes I excpect only to last a couple of minutes. Then came the phone call ...
Peter G.
@atk I use both. Like Peter G said, the `#if` is good for quick changes, without polluting your commit logs.
cape1232
A: 

@Anders K. This works, i.e. the proper things are not executed. The code produces "BAZ is undefined"

#define FOO 2
#define BAR

int main(int argc, char* argv[])
{
#ifndef FOO
  std::cout << "FOO is undefined" << std::endl;
#endif

#ifndef BAR
  std::cout << "BAR is undefined" << std::endl;
#endif

#ifndef BAZ
  std::cout << "BAZ is undefined" << std::endl;
#endif
}
cape1232
just because it works doesn't mean you should. it is confusing for the reader to know the point of the '2' if you are just looking to see if the symbol is defined (that was what I meant in my post)
Anders K.
Yep. I should have changed the 2 to a 1. That 2 is confusing.
cape1232