在Objective-C中包装C ++库不会隐藏C ++符号 [英] Wrapping a C++ library in Objective-C is not hiding the C++ symbols

查看:54
本文介绍了在Objective-C中包装C ++库不会隐藏C ++符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Objective-C包装一个C ++库(对于Sybase Ultralite),以便可以将该库导入MonoTouch.我创建了一个Objective-C库,并将编译后的C ++库包含在该项目 libulrt.a 中.为了编译我的项目,我将路径设置为 User Header Search Path ,以指向C ++头文件所在的目录.然后,我不得不将 compile source as 设置为 Objective-C ++ .

I am trying to wrap a C++ library (for Sybase Ultralite) with Objective-C so that the library can be imported into MonoTouch. I have created an Objective-C library and included the compiled C++ library in this project, libulrt.a. To get my project to compile I set the path to the User Header Search Path to point to the directory where the C++ header files are located. I then had to set compile source as to Objective-C++.

现在的问题是,尽管包装器库正确编译,但是一旦将其包含在另一个Xcode项目中,我就必须再次 compile源设置为 Objective-C ++ 否则,使用我的包装器库的项目将给出链接错误.我不明白为什么会这样,因为包装器库的头文件只包含Objective-C代码,而根本不包含C ++代码.仅在包装器库的实现(*.mm文件)中找到C ++代码.我该怎么做才能使C ++实现对于使用包装器库的项目完全透明?换句话说,我希望能够包含我的包装器库,并使用 compile source as 设置为 Objective-C 进行编译.任何人都知道如何执行此操作吗?

The problem now is that, although the wrapper library compiles correctly, once I include it in another Xcode project I have to again set compile source as to Objective-C++ otherwise the project consuming my wrapper library gives linking errors. I don't understand why this is, because the header file for my wrapper library contains only Objective-C code and not C++ code at all. C++ code is only found in the implementation (*.mm file) of the wrapper library. What do I need to do to make the C++ implementation completely transparent to the project consuming the wrapper library? In other words, I want to be able to include my wrapper library and compile with compile source as set to Objective-C. Anyone got any ideas how to do this?

在编译使用包装程序的项目时遇到的链接错误如下: (我删节了错误列表,因为它很长!)

The linking errors that I am getting when compiling the project consuming my wrapper are as follows: (I have abridged the error listing because it is LONG!)

"operator delete(void*)", referenced from:
zc3db40339fee::~zc3db40339fee()in libUltralite.a(ee39bf4763.o)
zb4297ee7d543::~zb4297ee7d543()in libUltralite.a(747e80fdad.o)
z33836a0a6f46::~z33836a0a6f46()in libUltralite.a(f240efda30.o)
"___cxa_pure_virtual", referenced from:
vtable for ze78b0ec59364in libUltralite.a(2c50e8e8ff.o)
vtable for ze78b0ec59364in libUltralite.a(2c50e8e8ff.o)
vtable for ze78b0ec59364in libUltralite.a(2c50e8e8ff.o)

我的包装库的头文件如下:

The header file for my wrapper library is as follows:

#import <Foundation/Foundation.h>

@interface DataAccess : NSObject {}

// Release objects.
- (void)dealloc;

// Singleton instance of the DataAccess class. 
+ (DataAccess*)sharedInstance; 

// Finalize the Database Manager when done with the DB.
+ (void)fini;

// Adds the given name to the database. 
- (void)addName:(NSString *)name;
@end 

任何人都知道如何将其编译为Objective-C ++,但是使用该库的项目仍然将其视为Objective-C吗?

Anyone have any idea how I can compile this as Objective-C++ but still have the projects consuming this library see it as Objective-C?

推荐答案

@Mark Bessey处在正确的位置,但是他不正确,认为设置语言和添加C ++库之间没有什么区别.添加C ++库很便宜,而且是必需的(一种或另一种方式).更改语言的成本很高,因为ObjC ++编译器的速度较慢,并且根据我的经验,通过Xcode在gdb中处理生成的代码有些痛苦.

@Mark Bessey is on the right track here, but he's incorrect that there is little difference between setting the language and adding the C++ library. Adding the C++ library is cheap and required (one way or another). Changing the language is expensive because the ObjC++ compiler is slower, and the resulting code is in my experience a bit of a pain to deal with in gdb through Xcode.

您需要做的就是在消耗项目中将-lstdc ++添加到LD_FLAGS(其他链接器标志)中.确保将"C ++标准库类型"设置为动态".

All you should need is to add -lstdc++ to your LD_FLAGS (Other Linker Flags) in your consuming project. Do make sure that "C++ Standard Library Type" is set to Dynamic.

这篇关于在Objective-C中包装C ++库不会隐藏C ++符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆