Question

i am trying to integrate my own small cpp project into a big objective-c project. I have compiled my own project into a .dylib file and added into the big project, and .h file is included. But in order to follow the legacy i have to call my function from a .m file in the big project.

i know that .m file can not call cpp functions so what i have tried is to include my cpp .h file in another .mm file, then in my .m file i include the header file to the .mm file. here is what i did:

A.mm (i created this file together with the header)

#import "A.h"
#include "mycppfunc.h"
int ParseFile(char * filename)
{
    ....(using cpp functions)
    return 0;
}

A.h

int ParseFile(char* filename);

B.mm (originally in the big project)

#import "B.h"
#import "A.h"
.... (original codes in B.mm)

B.h

#import "C.h"
....

C.m (originally in the big project, i have to call my own functions from here due to legacy)

#import "C.h"
#import "B.h"
....
- (IBAction) call_my_proj: (id) sender
{
    ....
    ParseFile("myfile.txt");
}

C.h

.... (this file is un-changed)

When i compile, i got this error:

Implicit declaration of function 'ParseFile' is invalid in C99

Can anyone help me explain where it went wrong? Can it be due to the circular reference to different header file (but i can not change it since it affects other peoples code). how can i get rid of this error? any help is highly appreciated! thanks.

Was it helpful?

Solution 2

First, you need to include A.h from C.h or C.m.

But, because the functions defined in A.mm are being compiled with a (Obj)C++ compiler, you might need to make them callable from C. You do this using C linkage. Then ParseFile() should be callable from the C.m file. For instance, in the header file:

extern "C"
{
   int ParseFile(const char *);
}

OTHER TIPS

You need to #include "A.h" in C.m. You are calling ParseFile() in C.m.

Rename extension of all your .m files to .mm. This way you can directly call cpp functions in your objective-C classes without any wrappers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top