Farooq Arshed
Farooq Arshed

Reputation: 1963

Include mm file in m file

I have a .mm file with c++ code and class declaration. I want to include it in another .m file but the compiler starts giving error in another class Unknown type name 'class'; did you mean 'Class'?

Please note that the .m file is already included in the .mm file.

e.g

A.h

A.mm -> this class has #include "B.h"

B.h -> #include "A.h" when I try importing A.h gives error.

B.m -> #include "A.h" when I try importing A.h gives error.

I have tried using struct in the .m file but the method does not get called in the instance.

Any help would be appreciated.

Upvotes: 0

Views: 1837

Answers (1)

uliwitness
uliwitness

Reputation: 8783

What are you really trying to do? Trying to include source files from within source files is a very rare thing to do, and usually a smell of something else being wrong in your program's architecture.

Usually, you should have a header and an implementation file for the .m and .mm file each. If you do not want the ObjC class declarations to be in the main header (e.g. because you're implementing a framework and this header is the framework's public API), you can also have several headers. E.g. you could have a MyClass.h and a MyClassPrivate.h, and only the second would be included.

You can't really use C++ from plain ObjC (that's what ObjC++ is for), so to use the header of the .mm in the ObjC file isn't possible. Unless you split it up into the ObjC parts (which you can use) and the C++ parts, e.g. in several headers.

For more info on mixing straight ObjC and ObjC++, look at my answer in this thread: Can I separate C++ main function and classes from Objective-C and/or C routines at compile and link?

I also did a podcast episode where I go into much more detail on the vagaries of using C++ and ObjC together: http://nsbrief.com/113-uli-kusterer/ In particular, make sure you're 100% firm on what a compilation unit is or you'll always have problems including ObjC headers from C++ or (Obj)C++ headers from ObjC.

Upvotes: 1

Related Questions