#define cimg_use_jpeg 2
#ifndef cimg_use_jpeg
//code goes here
#endif
I really don't understand...
#define cimg_use_jpeg 2
#ifndef cimg_use_jpeg
//code goes here
#endif
I really don't understand...
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
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.
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?
It cannot be executed. Maybe you run incorrect executable version. Printing __DATE__
and __TIME__
to trace or log helps to detect such errors.
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.
@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
}