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)
Related
I have some code wrapped between the following statements:
#if POTATO
(a few lines of c code)
#endif
Unfortunately, autocompletion, highlighting, suggestions and everything else stops working for any code between the #if statements. How can I fix this?
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
I want to compile source codes but I need to add extern "C" to a lot of header files from other package. So I have to keep those intact at the same time.
What I am wondering is, can I add
#ifdef __cplusplus
extern "C" {
#endif
at the header and
#ifdef __cplusplus
}
#endif
to the footer for every header I am interested in?
What I can think of is passing those files to the compiler using $awk but I cannot come up with the details.
Is there any known examples or something I can dig in?
Thank you very much.
You can create wrappers instead that include the headers in question like this:
extern "C" {
#include "header.h"
}
In your own code, you then include the wrappers, not the original headers.
You can automate the creation of those wrapper headers with a one-time script, which should be straightforward. In Bash, for example, I'd just do something like:
for f in include/*; do echo -e "extern \"C\" {\n#include \""$f"\"\n}" > "include_wrappers/wrap_$f"; done
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.
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