using c++ static library in objective c

戏子无情 提交于 2019-12-11 04:21:41

问题


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

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

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.

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,

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.

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)

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.

*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?


回答1:


Seems it hasn't been compiled for armv7 architecture. You can check with lipo -info myLib.a




回答2:


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

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



来源:https://stackoverflow.com/questions/12816983/using-c-static-library-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!