使用C / C ++静态库从iPhone ObjectiveC应用 [英] Using C/C++ static libraries from iPhone ObjectiveC Apps

查看:109
本文介绍了使用C / C ++静态库从iPhone ObjectiveC应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有一个C静态库API,它使用C ++内部和从磁带库的用户隐藏此?

Is it possible to have a C static library API, which uses C++ internally and hide this from users of the library?

我已经writen便携式C ++库我想静态链接到一个iPhone应用程序。

I have writen a portable C++ library I wish to statically link to an iPhone application.

我已经用了最大OS X'静态库'模板的一个X code项目,并复制使用(外部的C)在整个源,以及编写C wapper(以处理异常)

I have created an Xcode project using the Max OS X 'static library' template, and copied the source across, as well as writing a C wapper (to deal with exceptions) using (extern "C").

我想在另一个可可iPhone应用程序使用生成的库(某文件)。

I am trying to use the generated library (.a file) in another Cocoa iPhone application.

一切都工作得很好,如果我使用(.mm)的一些推广对库中的实现类调用ObjectiveC文件和(.cpp)文件。

Everything works well if the I use (.mm) extentions on the calling ObjectiveC file and (.cpp) on the implementation class in the library.

但我得到链接,当我尝试和包装文件更改为(.C)延伸,即使所有的包装功能文件只是C函数解析的符号。

But I get unresolved symbols on linking when I try and change the wrapper file to a (.c) extention, even though all the wrapper function files are only C functions.

只是监守C ++是在图书馆内部使用,是否意味着外部它仍然必须被视为C ++程序。请问有没有无论如何强制执行这一抽象?

Just becuase C++ is used internally in a library, does it mean that externally it still must be treated as a C++ program. Is there not anyway to enforce this abstraction?

编辑:谢谢你的答复,

我一直在使用的externC,我只是不确定的地方需要调用什么项目配置。 IE浏览器。如果预计调用需要知道如果它使用C ++或可能是无知,认为其纯粹的C库。

I had been using extern "C", I was just unsure about what configurations where needed in the calling project. ie. if the calling projected would require to know if it used C++ or could be ignorant and think its a purely C library.

这似乎我不能,我必须使用(.mm)在我的ObjectiveC类的文件。

It would seem I cannot, and I must use (.mm) files on my ObjectiveC classes.

推荐答案

这太难做到这一点的意见,所以我只是要快速演示给你链接的问题是什么,你遇到。当X code遇到的文件,它使用建立基于后缀规则来决定使用哪个编译器。默认情况下,海湾合作委员会链接到标准C库,但不符合标准的C ++库链接。存档文件(静态库)没有链接的决议进行的。他们基本上是被链接的其中的需要对象文件的归档。既然你在你的项目中没有.mm或.cpp文件,G ++永远不会调用和您的文件永远不会链接到标准库。为了解决这个问题,只需添加标准C ++库到您的其他连接标志在你的X code项目,或者只是简单地将它们添加到pre定义的其他标志的选择,因为-l(例如,-lstdc ++)。

It's too hard to do this in comments, so I'm just going to demonstrate for you quickly what the linking issues are that you're having. When Xcode encounters files, it uses build rules based on the suffix to decide which compiler to use. By default, gcc links the files to the standard C library, but does not link with the standard C++ library. Archive files (static libraries) have no linking resolution done at all. They are basically an archive of object files which need to be linked. Since you have no .mm or .cpp files in your project, g++ is never called and your files are never linked to the standard libraries. To correct this, just add the standard C++ libraries to your other linker flags in your Xcode project, or just simply add them to the pre-defined other flags option as -l (e.g., -lstdc++).

下面是一个快速演示:

stw.h:

#ifdef __cplusplus
extern "C"
#endif
void show_the_world(void);

stw.cpp:

stw.cpp:

#include <iostream>
#include "stw.h"
using namespace std;

extern "C" void show_the_world() {
  cout << "Hello, world!\n";
}

构建库:

$ g++ -c stw.cpp -o stw.cpp -O0 -g
$ ar rcs stw.a stw.o

从C应用程序中使用的库:

Using the library from a C application:

myapp.c:

#include "stw.h"

int main() {
  show_the_world();
  return 0;
}

构建C应用程序:

Building the C application:

$ gcc -o myapp myapp.c stw.a -lstdc++ -g -O0
$ ./myapp
Hello, world!
$

如果您尝试没有-lstdc编译++,你会得到所有悬而未决的问题,因为C编译器有绝对不知道它应该链接到C ++运行时(以及为什么会吧!?!?),所以你必须手动添加这一点。你有另一种选择是改变生成规则为您的项目......而不必X code使用GCC构建.c和.m文件,告诉它使用g ++以及您的问题将得到解决。

If you try to compile without the -lstdc++ you will get all the unresolved issues because the C compiler has absolutely NO idea that it should link to the C++ runtime (and why would it, right!?!?) so you have to add this manually. The other option you have is to change the build rule for your project... instead of having Xcode use gcc to build .c and .m files, tell it to use g++ and your issues will be resolved.

这篇关于使用C / C ++静态库从iPhone ObjectiveC应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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