使用 C++使用 Objective-C,如何修复“冲突声明‘typedef int BOOL’”?
我有很多 C++ 代码,最初是在 PC 上构建的。我正在尝试让它在 Mac 上与 Objective-C 一起工作。为此,我创建了一个 Objective-C 框架来容纳 C++ 代码,并添加了一个薄包装器。但我在 C++ 代码中遇到了 typedef 问题。
当我在 PC 上使用 C++ 时,我使用了 WinDef.h 中定义的 BOOL
变量。因此,当我将所有内容移至 Mac 上时,我添加了 typedef int BOOL;
以确保 BOOL
变量仍能按预期进行编译。
但是当我尝试编译时出现错误:“冲突声明'typedef int BOOL'
”。我认为这是因为 BOOL
是 Objective-C 中的一个关键字,因此已经定义了。我也不能只使用 Objective-C BOOL
因为它是一个 unsigned char 而不是 int。
当我寻找可能的解决方案时,我发现一个提到取消定义BOOL,但我找不到如何做到这一点(我也不知道它是否真的有效)。 另一个建议重命名 BOOL C++ 文件指向非关键字的内容。这个建议对我来说不是一个选择,因为其他项目依赖于 C++ 代码。理想情况下,我所做的任何更改都应保留在一个文件中,或者至少不会对 Windows 计算机上的代码产生负面影响。
如何为我的 C++ 文件取消 BOOL 的 Objective-C 定义并使用我添加的 C++ 定义?有没有更好的方法来处理这个问题?
如果它对我使用的有帮助:Xcode 3.2.5,64 位和 Mac OS X 10.6.6
感谢您的指导!
I have a lot of code in C++, originally built on a PC. I'm trying to make it work with Objective-C on a Mac. To that end, I created an Objective-C framework to house the C++ code and added a thin wrapper. But I'm running into a typedef problem in my C++ code.
When I was working with C++ on a PC I used the BOOL
variable defined in WinDef.h. So when I moved everything over the Mac I added in typedef int BOOL;
to make sure the BOOL
variable would still compile as expected.
But when I try to compile I get an error: "Conflicting declaration 'typedef int BOOL'
". I assume this is because BOOL
is a keyword in Objective-C, and so is already defined. I also can't just use the Objective-C BOOL
since it is an unsigned char and not an int.
While I was looking for possible solutions I found one that mentions undefining BOOL, but I couldn't find how to do that (nor do I know if it actually works). Another suggests renaming BOOL in the C++ files to something that isn't a keyword. This suggestion isn't an option for me because other projects rely on the C++ code. Ideally, any changes I make should stay in one file or at least should not negatively impact the code on a Windows machine.
How can I undefine the Objective-C definition of BOOL for my C++ files and use the C++ definition I added? Is there a better way to deal with this problem?
In case it helps I'm using: Xcode 3.2.5, 64-bit and Mac OS X 10.6.6
Thanks for any guidance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对一些讨论有点困惑,但是不使用
typedef int BOOL;
怎么样:如果您使用
typedef
那么#undef< /code> 不起作用,因为它们是两个不同的东西。
#define
/#undef
对执行替换的预处理器符号进行操作,而 typedef 是创建另一种类型的别名的语言的一部分。预处理器符号在任何时候都可以是未定义的,因为它只是预处理器的一条指令,告诉它在执行替换时不再使用该定义。但是,
typedef
不能是未定义的,因为它是在特定范围内创建的,而不是使用预处理器进行的线性处理。 (同样,您也不会期望能够声明一个全局变量int x;
,然后在代码中的某个时刻能够说“停止将 x 识别为变量”。 )我建议在答案中使用 #define 的原因是,ObjectiveC
#define
可能仅在编译某些代码时才被命中。这可以解释为什么当您删除typedef
时,您可能会在 C++ 中遇到错误,但如果它存在,仍然会出现冲突。但是,所以,如果假设是正确的,一旦您在尝试定义定义之前检查定义,您应该能够避免发生冲突的定义。最后一点:在这种特殊情况下,您也可以将
typedef
放入检查中,而不是#define
。然而,我倾向于按照我所做的方式来做这件事,因为这是一个非常常见的习惯用法,而且因为如果它最终被包含两次,该块也会阻止您在 C++ 代码中定义它两次。如果您非常喜欢typedef
并且知道这不是代码中的问题,那么可能这两个原因都不是非常令人信服的。 :)I'm a little confused by some of the discussion, but instead of
typedef int BOOL;
how about just:If you're using
typedef
then#undef
won't work since they are two different things.#define
/#undef
operate on preprocessor symbols that perform replacements, whereas a typedef is part of the language that creates an alias of another type.A preprocessor symbol can be undefined at any point because it is simply an instruction to the preprocessor that tells it to no longer use that definition when performing replacements. However, a
typedef
can't be undefined, as it is something that gets created within a particular scope, rather than the linear processing that occurs using the preprocessor. (In the same way, you wouldn't expect to be able to declare a global variableint x;
and then at some point in your code be able to say 'stop recognizing x as a variable.')The reason I suggest the #define in my answer is that it's possible that the ObjectiveC
#define
is being hit only while compiling some of your code. That could be an explanation as to why you may get errors in your C++ when you removed thetypedef
, but still get a conflict if it is in. But, so, if the assumptions are correct, once you check for the definition prior to trying to define it, you should be able to avoid the conflicting definitions when they occur.As a final note: in this particular situation, you could also just put your
typedef
inside the check instead of a#define
. However, I gravitated toward doing it the way I did both because it's a very common idiom, and because that block will also prevent you from defining it twice in your C++ code if it ends up included twice. Probably neither very compelling reasons if you very much prefer thetypedef
and know it's not an issue in the code. :)AFAIK,
BOOL
在 Objective-C 中也是一个#define
。即使您设法做到了,您也会遇到冲突的BOOL
定义问题,因为您的类型及其类型在大小和“符号”方面不一定匹配。您的
BOOL
必须真的是int
,而不是 Obj-C 定义的任何东西吗?换句话说,你不能省略#define
并简单地使用 Obj-C 吗?AFAIK,
BOOL
is a#define
in Objective-C too. You will have problems with your conflictingBOOL
define, even if you manage tobecause your type and its type do not necessarily match in size and "signedness". Must your
BOOL
really be anint
, instead of whatever it is that Obj-C defines it as? In other words, can't you omit your#define
and simply use the Obj-C one?如果你能回到过去,我会说“不要在你自己的代码中使用
typedef int BOOL
”。不过,既然你已经真正做到了,你就陷入了困境。一般来说,除了与外部代码交互之外,您应该避免在自己的代码中使用外部数据类型。标准类型很好,假设您可以保证在您的目标平台上使用符合标准的编译器进行编译。
最具前瞻性的解决方案是停止使用
BOOL
作为平台无关代码中的类型。同时,您可以使用各种预处理器黑客技术来使用BOOL
进行编译,但如果您不使用BOOL
,您可能会遇到一些奇怪的链接错误到处都是同样的方式(通过#includes)。If you could go back in time, I would say "Don't use
typedef int BOOL
in your own code".Now that you've actually done it, though, you are in a bit of a pickle. Generally, you should avoid using external data types for your own code except to interface with external code. Standard types are fine, assuming that you can guarantee compiling with a standard compliant compiler on every platform you target.
The most forward looking solution is to stop using
BOOL
as a type in your platform agnostic code. In the meantime, you can use all sorts of preprocessor hackery to make your use ofBOOL
compile, but you might hit some strange link errors if you don't useBOOL
in the same way (via #includes) everywhere.我使用 cmake 加载 freeimage 库,并且使用 kubuntu 14.x
我遇到了这个问题
,我认为与遇到此问题的人分享我的解决方案会很好!
在 Linux 上安装 FreeImage:
在我的 CMakeLists.txt 文件中,我有:
在我的 main.cpp 中,我有:
以及一些在磁盘上捕获 OpenGL 帧的额外代码:
我希望这可以帮助那些遇到此问题的人!
问候
卡欣
I use cmake to load the freeimage library and i'm using kubuntu 14.x
I had this problem with
and I thought it would be good to share my solution with people that have this problem!
install FreeImage on Linux:
In my CMakeLists.txt file I have:
And in my main.cpp I have:
And some extra code to capture OpenGl frame on disk:
I hope this helps the ones who have problem with this!
Regards
Kahin