0

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.

Farooq Arshed
  • 1,984
  • 2
  • 17
  • 26
  • There's rarely merit in including a .m or .mm file in another file. Same as including a .c in regular C -- it's done in very special cases, but if you're trying to do it in a "normal" situation you're doing something wrong. This is why there are .h files. (And if you don't know this you should not be programming in Objective-C -- learn plain old C first.) – Hot Licks Jul 25 '14 at 11:18
  • ... also Objective-C simply doesn't understand C++, so not only is an unusual technique, it's a waste of time. – trojanfoe Jul 25 '14 at 11:35
  • Your .m will not be able to include the header of the .mm if it includes c++. You will need to make an objc-only header to include, and keep the c++ portions in the .mm or another .h. – escrafford Jul 25 '14 at 17:18
  • Don't tell us you get errors without showing us the code and the errors. – Hot Licks Jul 27 '14 at 13:47
  • If you have a .h file that includes C++-only items, you need to use an #ifdef to skip those when it's included in a non-C++ module. There is a standard #define for C++ you can test. – Hot Licks Jul 27 '14 at 13:49

1 Answers1

1

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.

Community
  • 1
  • 1
uliwitness
  • 8,532
  • 36
  • 58