Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include C++ headers in an Objective C++ header?

I have a .cpp/.hpp file combo -> the .hpp file has #include ..

I also have a .mm/.h file combo -> if I include the .hpp file within my .mm Objective C++ file, there are no issues. However, if I try to include the .hpp file within my .h (Objective C header) file, I get a preprocessor issue 'iostream not found'.

Is there any way around this other than doing funky stuff like having a void* in my Objective C .h file and then casting it as the type that is included in the .mm or wrapping every C++ type within an Objective C++ type?

My question is basically the same as Tony's question (but nobody answered his):

https://stackoverflow.com/questions/10163322/how-to-include-c-header-file-in-objective-c-header-file

like image 250
2 revs Avatar asked Dec 03 '12 20:12

2 revs


People also ask

Do you include C file in header file?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .

Can you include header files in header files C?

Note: We can't include the same header file twice in any program. Create your own Header File: Instead of writing a large and complex code, we can create your own header files and include them in our program to use it whenever we want.

Can I include CPP file in header?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.

Can C++ include C headers?

If you are including a C header file that isn't provided by the system, you may need to wrap the #include line in an extern "C" { /*... */ } construct. This tells the C++ compiler that the functions declared in the header file are C functions.


2 Answers

The problem is that you have to avoid all C++ semantics in the header to allow normal Objective-C classes to include it. This can be accomplished using opaque pointers.

CPPClass.h

class CPPClass
{
public:
    int getNumber()
    {
        return 10;
    }
};

ObjCPP.h

//Forward declare struct
struct CPPMembers;

@interface ObjCPP : NSObject
{
    //opaque pointer to store cpp members
    struct CPPMembers *_cppMembers;
}

@end

ObjCPP.mm

#import "ObjCPP.h"
#import "CPPClass.h"

struct CPPMembers {
    CPPClass member1;
};

@implementation ObjCPP

- (id)init
{
    self = [super init];
    if (self) {
        //Allocate storage for members
        _cppMembers = new CPPMembers;

        //usage
        NSLog(@"%d", _cppMembers->member1.getNumber());
    }

    return self;
}

- (void)dealloc
{
    //Free members even if ARC.
    delete _cppMembers;

    //If not ARC uncomment the following line
    //[super dealloc];
}

@end
like image 99
Joe Avatar answered Sep 25 '22 03:09

Joe


To use C++ in an Objective-C++ header file make sure to:

  1. Have a .h/.mm couple (Objective-C++)
  2. In the identity of your file, have the type set to Objective-C++ Source (it should be automatic)
  3. Include the considered file only from Objective-C++ source files. This point is obvious but can be very quickly forgotten and hard to track down
like image 37
MartinMoizard Avatar answered Sep 25 '22 03:09

MartinMoizard