I’m trying to create a library in C++ in XCode, and I’m trying to use it inside an iPhone project. I’m using XCode 4.3.2. Since there’s no ready template available in iOS development to create a C++ library, I’m using the Mac OSX – Framework & Library -> C/C++ Library option.
In this project, I’m having .h and .cpp files. I purposely want to keep my C++ code in .cpp files since it may later be used on Android or Windows 8. In order for it to link against a sample iPhone project, I’ve modified the project’s target to point to Latest SDK iOS 5.1, and use the architecture armv6 and armv7.
Following is my .h file
#ifndef Test_Test_h
#define Test_Test_h
class Test { --> I get the error here saying unknown type 'class'; did you mean 'Class'?
public:
Test();
~Test();
int addTwoNums(int a, int b);
};
#endif
Following is my .cpp file in my C++ library
#include <iostream>
#include "Test.h"
Test::Test(){}
Test::~Test(){}
int Test::addTwoNums(int a, int b)
{
return (a + b);
}
Now in order to enable function calls from within my iPhone project, I’ve created a wrapper layer embedding C++ objects inside a .mm class courtesy this article:
http://www.philjordan.eu/article/mixing-objective-c-c++-and-objective-c++
Following is the implementation of my wrapper header file which is part of my separate iPhone project
#import <Foundation/Foundation.h>
@interface TestWrapper : NSObject
-(id)init;
-(int)returnSumFromCpluspus:(int) a b:(int) b;
@end
Following is the implementation of my wrapper .mm file part of my separate iPhone
#import "TestWrapper.h"
#import "Test.h"
@implementation testWrapper
{
Test *tst;
}
-(id) init
{
self = [super init];
if (self) {
tst = new tst();
if(!tst)
self = nil;
}
return self;
}
-(int)returnSumFromCpluspus:(int) a b:(int)b
{
int result = 0;
if(tst)
result = tst->addTwoNums(a,b);
return result;
}
- (void)dealloc
{
if(tst)
delete tst;
}
@end
Now in my iPhone project, I’ve linked it against this static library. I’m able to compile and debug just fine only when I select a “compile sources as: Objective-C++” in my iPhone project’s target, and not my C++ static library project. If I change the compile sources as: According to the File Type”, I get compilation errors:
unknown type name ‘class’;did you mean ‘Class’?
Expected ‘;” after top level decorator.
My question is it compulsory to change my iPhone project property target compilation type as Objective-C++, and not my static library C++ project or is there another way, which I might be missing and not aware of?
Any help in this regard will be greatly appreciated!
Thanks,
Asheesh
It would help us understand your problem if you edit your question to include some of the actual source code that the compiler doesn’t like.
That said, I think you are trying to
#include(or#import) a.hfile containing C++ class definitions in a.mfile.A
.mfile is (by default) an Objective-C file, not an Objective-C++ file, so the compiler will reject C++ constructs in a.mfile. If a.mfile includes/imports a.hfile, that.hfile must also contain only Objective-C code – not C++ or Objective-C++ code.You say that you have written a wrapper layer using Objective-C++. So you probably have a file named something like
Wrapper.hthat declares the@interfaceof your wrapper class, and you have aWrapper.mmcontaining the@implementation(in Objective-C++) of the wrapper class.You need to make sure that
Wrapper.hcontains no C++ code. It can only contain Objective-C, because you are going to include/importWrapper.hin your.mfiles.If your
Wrapper.hincludes/imports a header file from your C++ library, you need to take that out. You can only include your C++ library header files directly in your.mmfiles, or in other.hfiles that you only include from.mmfiles.