在目标 c 中使用 c++ 静态库 [英] using c++ static library in objective c

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

问题描述

我有一个静态库,它的代码是用 C++ 编写的.我想在一个客观的 C 应用程序中使用它.

I have a static library, whose code was in c++. I want to use it in an objective C app.

(我之前一直在使用 C 库,它们运行良好.)

(I have been using C libraries preiously, they have been working fine.)

我添加了库并尝试构建,但无法构建,因为在头文件中的某处使用了命名空间"和向量.因此,我不得不将目标 C 文件的类型更改为目标 C++.(我没有将扩展名更改为 .mm).由于此文件要包含在另一个文件中,因此该文件也必须更改为 c++,对于其他几个文件也是如此.

I added the library and tried to build, it couldn't because 'namespace' and vectors were somewhere used in the header files. So, I had to change the type of my objective C file to objective c++. (I didn't change the extension to .mm). Since this file was to be included in another file, that file also had to be changed to c++, similarly for few other files.

现在没有命名空间错误.但是现在当我构建时,它发现找不到引用的符号.我现在将扩展名更改为 .mm,仍然相同.我做了一些搜索,我读了一些关于 mangling 的东西.我不明白那是什么,但这是我尝试过的东西,

Now there is no namespace error. But now when I build, it cribbs that it can't find the referenced symbols. I now changed the extension to .mm, still the same. I did some searching, I read some things about mangling. I don't understand what that is, but here is something i tried,

我没有直接调用 c++ 函数,而是创建了一个 C 函数,它的声明前面带有 'extern "C"',并且库调用存在于这个 C 函数中.还是一样.我之前通过'extern "C"'实现了c函数,还是一样.

Instead of calling the c++ function directly, I created a C function, whose declaration was preceded by 'extern "C"', and the library call was present in this C function. Still the same. I preceded the implementation of the c function by 'extern "C"', still the same.

我还读到,如果 xcode 看到 .mm 扩展名,那么它才使用 g++ 编译器.在这种情况下,不需要 extern "C".是吗?

I also read that if xcode sees .mm extension, only then it uses g++ compiler. and in that case there is no need for extern "C". is it?

我是否需要在其他编译器标志目标设置中添加一些编译器标志?

Do I need to add some compiler flags in the Other compiler flags target setting?

ld: warning: directory not found for option '-F-F/Users/username/Desktop/projectFolder'
Undefined symbols for architecture armv7:
  "IIS::Image::Image::Image(unsigned int, unsigned int, ImageFormat)", referenced from:
      -[ImageEditorSupport loadToolKitForImage:width:height:length:] in ImageEditorSupport.o
  "IIS::Image::Image::getNumComponents(unsigned int&) const", referenced from:
      -[ImageEditorSupport loadToolKitForImage:width:height:length:] in ImageEditorSupport.o
  "IIS::Image::ToolKit::adjustSaturation(IIS::Image::Image const&, unsigned int, IIS::Image::Image&)", referenced from:
      -[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
  "IIS::Image::ToolKit::adjustContrast(IIS::Image::Image const&, unsigned int, IIS::Image::Image&)", referenced from:
      -[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
  "IIS::Image::Image::Image(unsigned int, unsigned int, ImageFormat, void*, unsigned int)", referenced from:
      _create_image_using_buffer in ImageEditorSupport.o
  "IIS::Image::ToolKit::adjustColorTemp(IIS::Image::Image const&, int, IIS::Image::Image&)", referenced from:
      -[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我还多次检查我是否添加了正确的库搜索路径.我对这两个-F-F"表示怀疑.我只期待一个.

I have also checked several times that i have added the correct library search paths. I am doubtful about the two '-F-F'. I was expecting only one.

**在警告 -FF/Users/username/Desktop/projectFolder 中是否意味着它正在搜索路径 -F/用户/用户名/桌面/项目文件夹 而不是 /Users/username/Desktop/projectFolder ?额外的 -F 是怎么来的?

*Edit :*In the warning -F-F/Users/username/Desktop/projectFolder does it mean that it is searching for a path -F/Users/username/Desktop/projectFolder instead of /Users/username/Desktop/projectFolder ? How could that extra -F come?

推荐答案

您在其中使用 c++ 库的 UIViewController 应具有扩展名 .mm 代替 .m (这是默认值).另外你的 main.m 现在应该 main.mm

The UIViewController in which you're using that c++ library should have the extension .mm instead .m (thats default). Also your main.m should now main.mm

这是因为您在 Objective-c 中使用了 c++ 代码.

This is because you're using c++ code in Objective-c.

这篇关于在目标 c 中使用 c++ 静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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