Question

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);
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top