Anna Koneva

Dr. Dobb's Bloggers

Problems When Linking Objective-C and C++ Code

February 20, 2014

Interoperating between Objective-C and C/C++ code presents several linker challenges, some of which are addressed in these recipes.

This article explains problems that occur interoperating between C, Objective-C, C++, and Objective-C++ code. All the samples below were tested on XCode 4.6.2 on the Apple LLVM 4.2 (Clang) and LLVM GCC 4.2 compilers, and also on the latest XCode 5. XCode 5.0 does not contain the GCC compiler anymore, so those examples were checked with Apple LLVM 5.0 (based on Clang).

Calling a C++ Function from Objective-C Code

Suppose you have a C++ function, which you need to call from Objective-C code. Here are the header file and the body code:

Compiling this, you will get a linker error stating that the method foo is not found.

Solution: Inlined C functions are a new feature of C99/GNU99, which is turned on in the XCode project settings by default. If all of the declarations of an inline function in a module do not have a storage class specifier, then the module will not produce a callable copy of the function. This creates linker errors.

One way to fix it is to add a static modifier for the function foo. Another way is to change the C dialect to GNU89 in the settings.

Oddities While Linking an Objective-C Project for Mac OS X and a C++ Static Library

And you use this library in a simple console Objective-C application. If your compiler is the default Apple LLVM compiler, the result depends on which settings you use for the standard C++ library. Different combinations for using the application, library, and C++ runtime are shown below.

Solution: Library libstdc++ is a part of GCC. It's old, and Apple has not supported GCC on Mac OS X for a long time. Although you can update it from the ports, you'll be better served by using libc++, which is newer and is supported by Apple.

Future posts will look at a few more kinds of interoperability errors between C/C++ and Objective-C.

Anna Koneva is a software engineer from Moscow, Russia. Her areas of interest and work include programming languages, especially C++, C#, and Objective-C. She develops desktop applications for Windows and mobile applications for the iPhone and iPad.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!