Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Macro Redefinition

I am currently using three servers (deploy, live_testing, and local). I am using macros to define a serie of domain locations:

#define __LIVE_TESTING // Here I chose what domain to use

#ifdef __PRODUCTION
#define DOMAIN @"http://192.168.10.228/rest/"
#define DOMAINCOMET @"http://192.168.10.228/"
#endif

#ifdef __LIVE_TESTING
#define DOMAIN @"http://192.168.10.229/rest/"
#define DOMAINCOMET @"http://192.168.10.229/"
#endif

...

The issue I am having are compiler issues relating to the redefinition of DOMAIN and DOMAINCOMET. Is there a workaround for these warnings?

Thanks in advance, Clinton

like image 403
r1d3h4rd Avatar asked May 03 '12 12:05

r1d3h4rd


2 Answers

#undef is your friend:

#ifdef __LIVE_TESTING

    #if defined(DOMAIN) && defined(DOMAINCOMET)
        #undef DOMAIN
        #undef DOMAINCOMET
    #endif

    #define DOMAIN @"http://192.168.10.229/rest/"
    #define DOMAINCOMET @"http://192.168.10.229/"

#endif 
like image 103
Richard J. Ross III Avatar answered Nov 07 '22 07:11

Richard J. Ross III


If you are getting redefinition errors, you must be defining the macro more than once. If this code is the only place that DOMAIN and DOMAINCOMET are defined, then it is possible that your both control flags are set.

This could happen if both __PRODUCTION and __LIVE_TESTING are defined to any value - even 0 since you are using #ifdef to see if they are defined, and not testing the actual value they are assigned.

For example, even:

#define __PRODUCTION 0
#define __LIVE_TESTING 1

will cause both blocks to be evaluated according to your code, and thus trigger the redefinition error.

If you want the two to be mutually exclusive, you should check their value, thus:

#if __PRODUCTION==1
#define DOMAIN @"http://192.168.10.228/rest/"
#define DOMAINCOMET @"http://192.168.10.228/"    
#elif __LIVE_TESTING==1
#define DOMAIN @"http://192.168.10.229/rest/"
#define DOMAINCOMET @"http://192.168.10.229/"
#endif
like image 40
gavinb Avatar answered Nov 07 '22 06:11

gavinb