简体   繁体   English

使用#ifdef和#ifndef移植C代码(C预处理程序)

[英]Porting C code with #ifdef and #ifndef (C preprocessor)

I'm porting an iPhone game to Mac and I'm writing a file with common defines that has the following: 我正在将iPhone游戏移植到Mac,并且正在编写具有以下内容的通用定义文件:

// first reset all defines

#undef TARGET_IPHONE
#undef TARGET_MAC

// set defines

#if TARGET_OS_MAC
#if TARGET_OS_IPHONE
#define TARGET_IPHONE
#else
#define TARGET_MAC
#endif
#endif

#ifdef TARGET_IPHONE
#error err1
#endif

#ifndef TARGET_IPHONE
#error err2
#endif

But when building for iPhone, both err1 and err2 are thrown by the compiler. 但是在为iPhone进行构建时,编译器会同时抛出err1和err2。

I don't get it, what's the problem there? 我不明白,那是什么问题?

EDIT: After about an hour of trying things with no luck, I had to add my own define to xcode build options. 编辑:经过一个小时的尝试,没有运气之后,我不得不将自己的定义添加到xcode构建选项中。

Your code is fine. 您的代码很好。 On my compiler (gcc mingw) #error err2 is thrown. 在我的编译器(gcc mingw)上,抛出#error err2 And if i insert 如果我插入

#define TARGET_OS_MAC 1
#define TARGET_OS_IPHONE 1

where your // set defines is, #error err1 is thrown. 您的// set defines所在的位置将引发#error err1

This looks like either a compiler bug, or you need special permission from Apple to use #ifndef (that's a joke (I think).) 这看起来像是编译器错误,或者您需要获得Apple的特殊许可才能使用#ifndef(这是个玩笑(我认为)。)

You might try this equivalent syntax that might work with Apple's compiler: 您可以尝试以下等效语法,该语法可能适用于Apple的编译器:

#if !defined(TARGET_IPHONE) #if!defined(TARGET_IPHONE)

or 要么

#if !TARGET_IPHONE #if!TARGET_IPHONE

or even 甚至

#ifdef TARGET_IPHONE #ifdef TARGET_IPHONE

#else #其他

This is worrisome, though - I'd look for an updated or different compiler. 但是,这令人担忧-我会寻找更新的或不同的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM