2

I made a cocoa touch static library in iOS in which I have C++ classes (.h and .cpp files).

I built the project successfully, but when I include this library (having .a extension) and any .h file, I get a compilation Error.

How can I add this library in my objective-C project and use the C++ classes?

canolucas
  • 1,482
  • 1
  • 15
  • 32
  • what kind of compile error? can you add a listing of how the compiler (or linker) is complaining to your question? – Michael Dautermann Oct 23 '11 at 20:15
  • This should definitely be doable. If you could tell us a little more about your error we could help you. You can call C++ code from Objective-C++ (.mm) files, which can use any syntax from Objective-C and C++. – Luke Oct 23 '11 at 21:14
  • Definitely we need more information about your compile errors. Did you consider that there are different binary formats (e.g. Mac OS and iOS use Mach-O and Android uses ELF)? – Marc Schlösser Nov 16 '11 at 19:55

3 Answers3

1

Your problem is likely that the .h header contains C++ code. You should read up on "compilation units". In short, there's no way to tell what language a header file is written in for the compiler. Therefore, it always uses the language of the source file that includes the header. So if you include a C++ header from a .m file, it will not work.

But there is a solution: Apple invented a "new language" it calls Objective-C++ that lets you write both C++ and Objective-C statements in the same file. For every ObjC file that uses a C++ header, you have to change the file name suffix of the source file that uses it from .m (ObjC) to .mm (ObjC++), which means the source files will be able to compile both ObjC and C++ headers.

Of course, you may not want to change all your files to be ObjC++. For one, C++ (and by extension Objective-C++) is a language with much more complex syntax than C and Objective-C, so your compile times will be longer, and also, C++ behaves differently in some aspects than C (and by extension, ObjC++ behaves a bit differently than ObjC).

What people usually do is constrain the C++ parts to their implementation files, and keep C++ out of the header. They write an Objective-C class that "wraps around" the C++ class, and provides methods that call the corresponding C++ methods on the C++ object. Then any ObjC file in your project can include that class, without having to turn on the ObjC++ compiler itself, which internally ("secretly") uses ObjC++ to call the C++ code.

For some useful tricks on how to hide C++ code inside an ObjC class, see Can I separate C++ main function and classes from Objective-C and/or C routines at compile and link?

Community
  • 1
  • 1
uliwitness
  • 8,532
  • 36
  • 58
0

Most probably you just should rename your .m files which are objective-c specific to .mm files that can accept C++ code (objective-c++).

Second is to check if everything it depends on is properly included before your library header.

Also check the architecture you have built your library for. If you run on an emulator - it should be x86, if for deploying to device - arm.

strannik
  • 1,595
  • 1
  • 13
  • 22
0

If you built the .a on the same system, there shouldn't be a problem #includeing its headers and linking against it.

Richard
  • 3,316
  • 30
  • 41