Unterminated conditional directive in Xcode - objective-c

What's wrong here:
#define CONTROLS_OFFSET 100
#ifdef CONTROLS_OFFSET//Unterminated conditional directive it says
#define FIND_MAIN_MENU 3
Why do i get this error?

An #ifdef, like an #if, needs to be balanced by an #endif. In this case, that would probably go immediately after your #define line.

Something like this:
#ifdef DEBUG
NSLog (#"This is a test");
#endif

Related

#if macros in Objective-C

I am trying to define the value of a macro based on some condition like
#define DEV NO
#if DEV == YES
#define API_ENDPOINT_HOST #"https://my-dev.com/"
#else
#define API_ENDPOINT_HOST #"http://my-qa.com/"
#endif
But even though I have defined DEV as No, it is always taking API_ENDPOINT_HOST as #"https://my-dev.com/". What is wrong here?
Even Uli's answer is correct (as everyone expected), I want to explain it in more detail:
In PP phase an undefined identifier in an #if directive is replaced with 0. NO and YES are not defined anymore as macro as it has been in the past, but became literals. So they are undefined in PP phase.
Your second line is:
#if DEV == YES
DEV is replaced with NO …,
#if NO == YES
… what is undefined as YES is. Therefore both are replaced by 0:
#if 0 == 0
That's obviously true.
NO and YES are Objective-C constructs. The preprocessor runs before the Objective-C compiler, so does not know YES or NO yet. Usually people use 0 and 1 in preprocessor defines.
Alternatively, just define your symbol and then use #ifdef instead of #if.
#define DEV
#ifdef DEV
#define API_ENDPOINT_HOST #"https://my-dev.com/"
#else
#define API_ENDPOINT_HOST #"http://my-qa.com/"
#endif

Is #if preprocessor macro "running" #ifdef behind the scenes in Objective-C?

If I create a preprocessor macro named DEBUG for the Debug configuration of a project's target with the value 1, and not for the Release configuration, I find that using
#if DEBUG
...
#endif
compiles for Release builds too.
Firstly, is it checking if DEBUG is defined and after that if it evaluates to true? If yes, is it ok (or recommended) to use it this way instead of
#ifdef DEBUG
#if DEBUG
...
#endif
#endif
?
It's not recommended to use #if VARIABLE without checking that VARIABLE has been defined.
When #if evaluates the expression, it does the following:
It expands all macros in the expression (which means that an symbol which has not been #defined will be unchanged.)
It parses the result as an integer arithmetic expression, replacing any identifier with the integer 0.
So if DEBUG has not been #defined as a macro,
#if DEBUG
will be the same as
#if 0
which will also have the same effect as #ifdef DEBUG.
If you define DEBUG on the command-line with -DDEBUG, then there is an implicit
#define DEBUG 1
before the file is preprocessed, with the result that
#if DEBUG
is the same as
#if 1
which is also the same as #ifdef DEBUG.
However. If you did the following:
#define DEBUG
#if DEBUG
// ... stuff
#endif
the the #if will expand to:
#if
and you'll get an error message:
file.m:2:6: error: #if with no expression
As a result, it is unwise to use #if VARIABLE, unless you know for sure that VARIABLE has been defined as an integer.
Modern Xcode already provides a definition for DEBUG when building in debug mode.
The difference between #ifdef DEBUG and #if DEBUG is that #ifdef DEBUG will be true when the symbol is defined, no matter what value has been defined.
Thus, #define DEBUG=0 will be true for #ifdef DEBUG because it has a defined value (it will also be true for #define DEBUG for the same reason).
However, #if DEBUG will only be true if DEBUG has been defined with a non-zero value.

ObjC macro ifdef and defined

I'm trying to do a macro where if AAA and BBB does not exists. Something like this:
#ifdef !AAA && !BBB
#endif
or this:
#ifndef AAA || BBB
#endif
However, Xcode is throwing me errors, so I've tried
#ifdef !(defined AAA) && !(defined BBB)
or some other such combinations and it seems like Xcode doesn't seems to understand defined. I'm getting "Macro names must be identifiers" or "Extra tokens at the end of #ifdef directive" errors.
Any idea how I could workaround this problem?
Can you just use #if?
#if !defined(AAA) && !defined(BBB)

What does gcc #define when compiling objective-c(++)?

Is there some #define I can look at so that I know when to pull in <OpenGLES/EAGL.h>, for instance? Thanks!
This should work:
#if defined(__OBJC__) && defined(__cplusplus)
For Objective-C just leave off the __cplusplus part
You should be able to determine this by running "gcc -v filename" and looking for the arguments to the "cc1" command (-DTHIS, -DTHAT, ...).

MinGW and "declaration does not declare anything"

I'm working on converting a Linux project of mine to compile on Windows using MinGW. It compiles and runs just fine on Linux, but when I attempt to compile it with MinGW it bombs out with the following error message:
camera.h:11: error: declaration does not declare anything
camera.h:12: error: declaration does not declare anything
I'm kind of baffled why this is happening, because
I'm using the same version of g++ (4.4) on both Linux and Windows (via MinGW).
The contents of camera.h is absurdly simple.
Here's the code. It's choking on lines 11 and 12 where float near; and float far; are defined.
#include "Vector.h"
#ifndef _CAMERA_H_
#define _CAMERA_H_
class Camera{
public:
Vector eye;
Vector lookAt;
float fov;
float near;
float far;
};
#endif
Thanks for your help.
EDIT: Thanks both Dirk and mingos, that was exactly the problem!
Edit If you happen to include windef.h (either directly or indirectly), you will find
#define FAR
#define far
#define NEAR
#define near
there. I think, that this is the culprit.
Try
#undef near
#undef far
before your class definition.
Try giving them different names, like
float my_near;
float my_far;
I recall Borland using "near" and "far" as keywords (my 1992 Turbo C had these, back in MS-DOS era). Dunno if this is the case with gcc, but you can always try that.
In <windef.h>, you'll find on the following lines:
#define NEAR
#define near
Simple answer: you can't #undef them because they're a part of the Windows headers (_WINDEF_H will still be defined even if you #undef those definitions, so it won't be re-included if you try to #include <windef.h> again, not to mention the fact that if you #undef _WINDEF_H before using #include <windef.h> after your class definition, you'll end up with duplicate definitions for things like RECT, LONG, PROC and more), so the only other solution is to change your variable names.