Compiler error when including C++ code in the iPhone project - c++

I'm trying to include some C++ code into my iPhone project and I'm getting the following compiler error:
"error:expected initializer before '<' token"
at this code construct:
template<typename T, P_UINT_32 BEG, bool OQ, bool OVR, bool DBG>
P_UINT_32 EKType<T, BEG, OQ, OVR, DBG>::getSizeX() const {
return n;
}
It looks like the XCode compiler is not recognizing this as a valid C++ syntax. I have named my C++ files with .h and .mm, and I've set the types of the files to sourcecode.cpp.h and sourcecode.cpp.cpp
Anyone has an idea as to why I'm getting this error?

You probably have the header being included by a .M file somewhere. It's amazing how these things can get pulled in, so make sure all of your .M files are renamed .MM.

You only need to name a file .mm if the file contains both Objective-C and C++.
If the file only contains C++, it should have the extension .cpp
If the file is a mix of ObjC and C++, then it should have the extension .mm and have its type set as sourcecode.cpp.objcpp

Are you sure that the source file you are trying to compile includes the declaration of the EKType class (or struct) and the declaration P_UINT_32?
I would think you'd get a similar error if the compiler wasn't aware of EKType or P_UINT_32.

Related

'NSString' was not declared in this scope

I'm building a mobile (IPhone) application, I'm new to use the c++ in xcode, this is my code
NSInteger myInteger = 42;
int myInt = (int) myInteger;
It should work, because I can compile both objective-c++ and c++ in the xcode, but I get this error
'NSString' was not declared in this scope
Any suggestions?
Actually, yes you can write both Objective-C, Objective-C++, and C++ using xcode, but you need to make sure what's the file extension you are using.
If it's .m this mean you can compile only the objective-c code, and you can't write a c++ code.
If it's .mm this mean you can use Objective-C++ so you may compile a c++ code in this case.
If it's .cpp this mean you can only compile the c++ code.
Your question not so clear, but I think this problem happen may because the file extension is .cpp.
If you are using a .cpp file, so you need to change it to .mm file, or not use NSInteger and use int instead of.
Edit:
NSString is an Objective-C type and cannot be used in C++ code, therefore try to compile as Objective-C++, by change the file extension from .cpp to .mm

How can I use my custom types (C++) in an objective-c project with xcode?

I have a few classes I wrote to deal with numbers having units transparently as if they were just ints or floats. In a nutshell:
SI_Term speed(4, "mph");
SI_Term distance(10000, "feet");
SI_Term time = distance / speed;
std::cout<<time.string();
and all the major operators are overloaded to work this way. Took a quite a bit of work so I would like to use the C++ classes in my ObjC iPhone app. I tried adding them with .h and .mm extensions to my xcode project, but it doesn't seem to be treating them as C++ files. (Won't find any of the STL headers, syntax errors anywhere I declare a class or anything).
New Info:
Tried .mm and .h extensions, errors:
in a .h file:
namespace DA and just about everything else involving c++ code (about 40 errors all this message) gives me expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
I also get *: No such file or directory for all instances of #include <*> throughout the c++ files.
I'm sure its some kind of simple fix but .mm and .h wasn't enough.
Update:
Found that copying and pasting source code into new files seems to work. The mm file (or the cpp file) compiles. The h file doesn't work though, it throws the above mentioned error at any occurrence of c++ specific code.
Thanks!
You are probably including (or importing) your h file in one of your .m files. Those need to be renamed to .mm too.
In the end you'll probably name all .m fikes to .mm. And by the way, the cpp files, can simply remain cpp. No reason to rename them.
Not knowing exactly what the errors you're getting are, rather than adding your files with a .hpp and .cpp extension to your project, I would change the extension of the header files to just .h, and the extension to your .cpp files to either .mm or .M so that Xcode recognizes them as Objective-C++ files, and compiles them appropriately.

Mixing Objective C ,(*.m , *.mm & .c /.cpp ) files

In my project Core libraries are part of C/C++ files, while UI needs to be developed in Objective C,
I am able to access/Call C++ functions from Objective C/.mm files
but reverse no luck so far, i.e. i am not able to call Objective C functions from C++ Files,
when i tried to include Objective C header even system header
#import <foundation/foundation.h>
getting around 1000+ compilation error,
something like this
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:180:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:180: error: expected unqualified-id before '#' token
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:182:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:182: error: expected initializer before '*' token
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183: error: 'NSString' was not declared in this scope
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183: error: 'aSelectorName' was not declared in this scope
Am i missing some pre-compile flag etc..
can anyone suggest me, the best possible way to call/access objective C class which is inherited from NSObject, without modifying much C++ code, i just need to call one function
Code structure / Order to include header files are
Some system header file
Some Core Class Header file
#import <foundation/foundation.h>
If you have a .cpp file with C++ code that needs to use Objective-C as well, either rename that .cpp file to .mm or pass -x objective-c++ to the compiler.
I found it imposible to use any Objective-c in the C++ header files.
However, you can include Objective-c in the implementation files.
(.mm or you can set how to interpret .cpp files in the info of the file. Choose Info->General:FileType:Sourcecode.cpp.objcpp )
Use
cppClass.h:
class objcClass;
objcClass* mMemberVariable;
cppClass.mm:
#import "objcClass.h";
void cppFunction(){
[objcClass message];
}
in the cpp header file.
Then include the header that defines the class in the .cpp or .mm file.
If you use Qt, there is Q_FORWARD_DECLARE_OBJC_CLASS macro which can be used to forward declare an Objective-C class.
As a reference, here is the implementation:
#ifndef Q_FORWARD_DECLARE_OBJC_CLASS
# ifdef __OBJC__
# define Q_FORWARD_DECLARE_OBJC_CLASS(classname) #class classname
# else
# define Q_FORWARD_DECLARE_OBJC_CLASS(classname) typedef struct objc_object classname
# endif
#endif
I used a technique of the name expand files depending on a programming language. Also, I added "-x objective-c ++" to the compiler, but the problem remained. I was helped by council for the link, pay attention to the "file type" parameter.
In XCode 11, it can be done by changing the build setting Compile Source as (GCC_INPUT_FILETYPE) to Objective-C++ (sourcecode.cpp.objcpp). By doing this, there is no need to change the file extension type to mm.

Problem when #import C++ Header File in iPhone/iPad Project

I have a C++ class I would like to use in an iPhone/iPad project.
I created this file in different ways (like with "New File" => C++) and the error is always the same.
When I compile the project without having any #import (of the .h C++ class), it's ok.
But as soon as I #import the header file in one of my header objective-c file, I get error like :
error: vector: No such file or directory
or
error: expected '=', ',', ';', 'asm' or 'attribute' before ':' token"
I tried setting different values to the file type (of C++ class) in File Info, renaming my objc class in .mm etc, but it doesn't seem to work.
So I must have missed something about importing the .h c++ class in the objc header file, but what :p ^^
SOLUTION thanks to Vlad
1°) To include the header c++ file :
#ifdef __cplusplus
#include "Triangulate.h"
#endif
2°) Renaming the objc file in .mm AND in his File Info (right clic) setting file type as sourcecode.cpp.objcpp
Thanks for helping !
Vincent
Note: Xcode requires that file names
have a “.mm” extension for the
Objective-C++ extensions to be enabled
by the compiler.
Trying to use C++ in Objective-C code residing in a file with .m extension is the most probable cause of the problem because compiler just does not recognize C++ constructs according to the error message. Renaming your .m file to .mm should help.
For more details, see Using C++ with Objective-C.
Assuming you want to use an Objective-C class in an Objective-C++ source file, there's no problem at all. The only restriction is that your .h file must be Objective-C clean. This means that you can't use any C++-isms in it, or that if you do you must wrap them all in #ifdef __cplusplus. The header will be compiled in ObjC mode when it's #included by a plain Objective-C file, so it has to eliminate any C++isms for that situation (1). So your Objective-C header file should include C++ header like this:
#ifdef __cplusplus
# include MyCPPHeader.h
#endif

Objective C syntax inside .cpp file?

I'm trying to copy over some example code into my own project. The example project is iPhoneExtAudioFileConvertTest from the sdk. The example project contains a file called ExtAudioFileConvert.cpp. This file contains what looks like Objective-C code:
assert([NSThread isMainThread]);
The example project runs fine, but the compiler complains about the code above when I build my own project: error: expected primary-expression before '[' token
What's going on here? Obviously there's some way to use objective c bracket syntax in a .cpp file. What do I need to do to make it work?
Change the file extension to .mm for Objective-C++ instead of just .cpp for C++.
The default build settings of iPhoneExtAudioFileConvertTest is Objective-C++.
If you change the settings to According to File Type, you will get the same error message.
So, change the file extension to .mm or change the build settings of your project.