3

I have Objective-C Function and C++ file,which i renamed .mm and connected to my project, i need to use Objective-C function in C++ file,but Xcode gives me:

Undefined symbols for architecture i386: "maxRadiusFinder(double*, int, int)", referenced from: Fun(int, double, double*, double*, int, double (*) [2], int, int) in RALG.o

My code Objective-C code:

 double maxRadiusFinder(double* algoPoints1,int count,int numberC)
{
NSMutableArray* algoPoints=[[NSMutableArray alloc] init];
for (int i=0; i<count; i++) {
    Points *temp=[[Points alloc] init];
    [temp setXCor:algoPoints1[i]];
    [temp setYCor:algoPoints1[i+count]];
    [algoPoints addObject:temp];

}
int howMany = (kDefaultGraphWidth - kOffsetX) / kStepX;
int howManyHorizontal = (kGraphBottom - kGraphTop - kOffsetY) / kStepY;
NSMutableArray *distancesKCover=[[NSMutableArray alloc]init];
for (NSUInteger j=0; j<=howManyHorizontal; j++) {
    for (NSUInteger i=0; i<=howMany; i++) {
        Points *temp=[[Points alloc] init];
        [temp setXCor:i*kStepX];
        [temp setYCor:j*kStepY];
        NSMutableArray *distances=[[NSMutableArray alloc]init];
        for(NSUInteger i1=0;i<[algoPoints count];i++)
        {
            float temp1=MAX(fabsf([[algoPoints objectAtIndex:i1] xCor]-[temp xCor]),fabsf([[algoPoints objectAtIndex:i1] yCor]-[temp yCor]));
            [distances addObject:[NSNumber numberWithFloat:temp1]];
        }
        NSArray *sortedDistances=[distances sortedArrayUsingSelector:@selector(compare:)];
        [distancesKCover addObject:[sortedDistances objectAtIndex:numberC]];
    }
}
double max = [[distancesKCover valueForKeyPath:@"@max.doubleValue"] doubleValue];
return max;
 }

And c++ call of this function in .mm file:

  rd += maxRadiusFinder(g, 10, 10);
user3579086
  • 123
  • 6

1 Answers1

5

objective-c has "c" linkage. Objective-c++ has c++ linkage. C and C++ are different languages that share some common syntax.

You need to declare the function as extern "C" in your .mm context

for example

declarations.h:

#ifdef __cplusplus
extern "C" {
#endif

// all function declarations here are common to C and C++. They will all have C linkage
// i.e. no name mangling and no overloading

double maxRadiusFinder(double* algoPoints1,int count,int numberC);

#ifdef __cplusplus
}
#endif
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Thank you. I used your advice wrap the declaretion and definition of function as extern "C" and issue has gone,but another issue appeared "use of undeclared identifier 'new' ". I figured out, that this problem appears only when my .mm file connected to my other m.file. – user3579086 Apr 27 '14 at 20:40
  • You can +1 my answer if you like ;-) – Richard Hodges Apr 27 '14 at 20:47
  • I need 15+ reputation to do +1 ( The problem hasn't gone. When i didn't import .mm file in .m file problem appears again, but when i did it, the "use of undeclared identifier 'new' " issue appears – user3579086 Apr 27 '14 at 20:56
  • can you link your files? Did you say you're importing a .m file? – Richard Hodges Apr 27 '14 at 21:24
  • You realise the operator new is a c++ language feature? You can only use it in a .mm file. Do not import a .mm into a .m (or vice versa). put the function declarations into a header file and import that from the .m and the .mm – Richard Hodges Apr 27 '14 at 21:27
  • I have 3 files .m file with maxRadiusFinder(double*, int, int), .mm file, which uses maxRadiusFinder and another .m file, which must use function from .mm file – user3579086 Apr 27 '14 at 21:28
  • the function exported from the .mm file will need to have extern "C" linkage also. – Richard Hodges Apr 27 '14 at 21:29
  • Undefined symbols for architecture i386: "_maxRadiusFinder", referenced from: _Fun in ViewController.o _Fun in RALG.o – user3579086 Apr 27 '14 at 21:37
  • maxRadiusFinder and Fun have extern "C" linckage – user3579086 Apr 27 '14 at 21:38