1

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?

neeraj
  • 1,191
  • 4
  • 19
  • 47

2 Answers2

1

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

Mr Bonjour
  • 3,330
  • 2
  • 23
  • 46
  • that i'll check and reply soon – neeraj Oct 10 '12 at 11:41
  • No, while building the library no such thing with the namespace was done. In fact, I have 2 things. one mylibsdk.a, which is an sdk and its code was in c++. and one is a library mylib.a whose code was in C. Now they might make calls to each other. could that make a difference? – neeraj Oct 10 '12 at 11:59
  • can you please tell me what linkinh with 'IIS' would mean? I mean that is a namespace, what does it have to do with the linking? – neeraj Oct 10 '12 at 12:15
  • I thought IIS was another lib or class (i don't know your actual code). You actually need to find where the Image::Image:: getNumComponents(), etc... are compiled and u need to link them in your static lib. Even if they are in a header file, the linker doesn't find the actual implementation, or there a not available in armv7 architecture. That's why you get this warning. – Mr Bonjour Oct 10 '12 at 12:29
  • getNumComponents is in a mylib.a, whose code was in C. Since I have changed my files to c++ and it now is using some different compiler, could it be that it now doesn't even recognize that library? – neeraj Oct 10 '12 at 12:32
  • Do you means there are two lib? myLib.a and the one you are trying to make? find where is mylib.a, check if it's armv7 architecture compliant. When you link mylib.a with your new binary that should work. Watchout: static lib should be name libxxx.a. If you still have problem, try makin the new static library with the (mylib.a) code source. – Mr Bonjour Oct 10 '12 at 12:53
  • There are two libraries. See my third comment. Both libraries might be making calls to each other. I don't even know, I didn't make the code for the libraries. One is called the sdk, whose code was in c++, one library whose code was in C. The `Image()` is in the sdk, and it is the constructor that cribbs. A few other methods I am calling, they are not appearing in the error list, and most probably they are in the sdk. The `adjustContrast` is called through the sdk, but the code lies in the C library. – neeraj Oct 10 '12 at 13:05
  • 1
    Ok, you can't link- fuse two static library. And they can't be both depending on each other. see this link, that could help you: http://stackoverflow.com/questions/2157629/linking-static-libraries-to-other-static-libraries – Mr Bonjour Oct 10 '12 at 13:19
  • Hey. i found something. I was calling the library functions from objective-C functions, I tried moving them to C-type functions and now it builds! – neeraj Oct 10 '12 at 13:41
  • But I want to call those functions from somewhere. If I make an objective C function in the same class, and call the C functions from there, I get the linker errors again. And if I want to call the C functions directly from the class instance, how to do that!!?? – neeraj Oct 10 '12 at 13:45
1

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.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • I renamed all files to .mm. Didn't leave any. I forgot the main.m, but i tried doing it now, it still didn't work. – neeraj Oct 10 '12 at 11:29