Search code examples
iphonec++objective-cxcode4objective-c++

Apple Mach-O Linker (Id) Error : While calling c++ file from .m file


I have some cpp file where my functions are written. I want to call that functions in my iPhone app? How can i call c++ methods in my .m file???

Here is my .cpp file

int add(int a, int b)
{
    return a+b;
}

Now in my viewDidLoad event i am using following code just to fetch the result and display in a Label.

NSNumber *n = [[NSNumber alloc] initWithInt:add(5,7)];
[myLabel setText:[NSString stringWithFormat:@"%@",n]];

I received the following Error while compiling:

Undefined symbols for architecture i386:
  "_add", referenced from:
      -[usingc__ViewController viewDidLoad] in usingc__ViewController.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

Where is the error in the code?? What edit is necessary for this??


Solution

  • If you want to use C++ file in your Objective-C file, change the file extension from .m to .mm.