简体   繁体   English

在Objective-C中包装C ++类

[英]Wrapping C++ class in Objective-C

I have a C++ library that doesn't use namespaces. 我有一个不使用命名空间的C ++库。 I want to port it to Objective-C. 我想将它移植到Objective-C。 The problem is the name collisions between the two. 问题是两者之间的名称冲突。 I want it to have the same name in Objective-C that is the name of the C++ object. 我希望它在Objective-C中具有相同的名称,即C ++对象的名称。 But, I just can't figure out the best way to do this without it just becoming a mess. 但是,我只是无法找到最好的方法来做到这一点,而不是它变得一团糟。 I am hoping for a solution to this. 我希望能解决这个问题。

以与在Mac上编写框架相同的方式为您的所有Objective-C类添加前缀,而Apple则使用其框架。

You can use #define and #undef to temporarily rename the classes/functions etc in your code - just make sure they are undef'ed when calling the C++ code. 您可以使用#define#undef临时重命名代码中的类/函数等 - 只需确保在调用C ++代码时它们是未定义的。

eg in the header(s) for your wrapper have #define Bar_Function Foo_Function , then surround the bits of code that call the library's Bar_Function with #undef Bar_Function and #define Bar_Function Foo_Function 例如,在你的包装器的头文件中有#define Bar_Function Foo_Function ,然后用#undef Bar_Function#define Bar_Function Foo_Function包围调用库的Bar_Function的代码位。

Now the only problem comes when you need to export your own Bar_Function... 现在唯一的问题是你需要导出自己的Bar_Function ...

Don't do this though - its silly, just work around it. 不要这样做 - 它很愚蠢,只是解决它。 :) :)

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

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