Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C with Objective-C and duplicate symbol linker error (iPhone related)

I have the following file testf.h:

#ifndef TESTF_H_
#define TESTF_H_
int test(int what){
    return what;
}
#endif

I included/imported it in TestAppDelegate.h (which is used for other .m files in my xcode project). I get a duplicate symbol error. If I included/imported testf.h in a .m file that is never included/imported in other files then it works fine. So it seems like the #ifndef/#define/#endif has no effect. Is there anyway to go around this?

Thanks

like image 644
cisgreat Avatar asked Jan 20 '23 08:01

cisgreat


1 Answers

This is a function definition, it belongs in a c, cpp or m file.

This popular trick with #defines will protect you from compiler errors (typically to survive circular #include dependencies. ) It will not protect against a linker error. This is exactly why people put declarations in h files and definitions in c (or m) files.

like image 164
Kris Van Bael Avatar answered Jan 30 '23 21:01

Kris Van Bael