Search code examples
c++objective-cobjective-c++

How to Call a C++ Method in Objective-C


I have C++ files: C.cpp and C.h

//C.h
#ifndef __ThreeMoreOpenCV__SourceC__
#define __ThreeMoreOpenCV__SourceC__

#include <iostream>

class C
{
public:
    static void Cmethod ();
};
#endif

//C.cpp
#include "SourceC.h"

using namespace std;

void Cmethod()
{
    printf("ff");

}

Also I have Wrapper.h/.mm

//Wrapper.h
#import <Foundation/Foundation.h>

@interface CVWrapper : NSObject
+(void)returnCmethod;
@end

//Wrapper.mm
#import "Wrapper.h"
#import "C.h"

@implementation CVWrapper

+(void)returnCmethod
{
    C::Cmethod();
}

@end

I have no idea but i'm getting an error like:

"C::Cmethod()", referenced from: +[Wrapper returnCmethod] in Wrapper.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation).

Can anybody tell me what is correct way to call function?


Solution

  • You haven't actually implemented the Cmethod function in your C class. You need to implement it with C:: in the function header, like:

    void C::Cmethod()
    {
        printf("ff");
    }