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
Related
I have been thinking this problem for a while but still no idea about it, if my project is mainly cpp file, should a c file name as .c, or should be named as .cpp to consistent with other .cpp file?
I just list some advantage and disadvantage (in my current knowledge) of using .c (I don't know if the following idea is correct):
advantage of .c:
fast to know it does not contain c++ content (e.g.:class,std::string)
easy to separate from .cpp file by searching name
disadvantage of .c:
not consistent with other files (because other files mostly .cpp)
may need to rename it as .cpp if I want to change the function as using oop or want to add some oop features into it
some scripts or files may need to add *.c as file input if the original version only handles *.cpp, (e.g.: need to add *.c in Android.mk in android jni)
Also I don't know if compiler handles .c and .cpp in different way,also don't know if it affects other behaviour (e.g.:performance,platform or compiler specific issues...), is anyone have idea about it?
Depends what you mean by "C" code.
Are you going to compile it with a C compiler?
Call it file.c
Or do you just mean "C-like" C++ code? C++ code that, at time of writing, happens to also be valid C?
Call it file.cpp
Rule of thumb - name it according to which compiler you intend to use for it. This keeps your makefiles nice and simple.
So if your "C code" is C++ code that could be compiled as valid C but that's not what you are doing, then name it *.cpp and let your makefile invoke the C++ compiler on it.
If your code is actual C, to be compiled with a C compiler, then name it *.c - and remember the (appropriately-#ifdefed) extern "C" in the header file so that C++ built against it can link successfully.
C++ fully supports c code. So the compiler would be just fine with c code in a .cpp file.
And like Quentin mentioned above. If your c code is never used in a c only project I would leave it in an cpp file.
Posting a basic question about using C style .c and .h class in a C++ application.
I have a library which is meant for C but based on the documentation i can also use for C++.
Should i need to rename the two files as .cpp and .hpp before i start including them in my project ?
I tried to refer existing thread but it talks about other way crom cpp to c.
How to convert C++ Code to C
No you don't. The .h extension is shared.
The implementation file extension depends on the compiler/IDE. For example, MSVS will compile .c files as C source code, and .cpp files as C++. That means you'll have to use
extern "C"
in the header if you use the C functionality in the C++ part of the project.
AFAIK you can compile .c files with g++ as C++, so the extension change is not necessary. Or you can compile them with gcc and use extern "C" again.
I need to use C++ file in my project, it's called CAXException.hpp, and row in project targets "Compile Sources As" - "According to file type" not Objective-C++. But when it's compiling it always displays me error error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CAXException'
in code:
class CAXException //<-------error here
{}
Please help me to fix it..
If I understand you correctly, you have a C++ header file (.hpp) that you want to include from Objective-C file. Unfortunately, you can't do that directly. You'll have to use a workaround.
The easiest is to change the compilation option of each and every Objective-C file (.m) that include this C++ header file (either directly or indirectly) to be compiled as an Objective-C++ file. This can be done either by renaming the files to .mm extension or by changing the option for the compiler for the file.
If this work for you, this will be the easiest, however Objective-C++ is not a complete superset of Objective-C (as C++ is not a superset of C), and some valid Objective-C is invalid Objective-C++ (if C++ keywords are used as variables names).
If this happens, you'll have to create an Objective-C wrapper to the class, with an implementation in Objective-C++ that simply delegate to the C++ class. That is create an CAXExceptionWrapper.h Objective-C file, containing something like:
#interface CAXExceptionWrapper {
#private
void* _CAXExceptionImpl;
}
- (id)init;
// ...
#end
And an `CAXExceptionWrapper.mm' Objective-C++ file containing:
#import "CAXException.hpp"
#implementation CAXExceptionWrapper
- (id)init {
if ((self = [super init])) {
_CAXException = new CAXExceptionWrapper;
}
return self;
}
// ...
#end
And then in your Objective-C files, include the wrapper Objective-C header instead of the C++ header.
class CAXException //<-------error here
{};
^^^
you are missing the ;
And Your compiler seems not to recognize the C++ keyword class, which is strange. Most likely, you are missing some ; before this class definition or some other syntax error but before this.
It is possible that that header is being included from an objective-c (.m extension) source code file. The source code file that includes that header must be an objective c++ one, i.e. ends with .mm or you can force objective c++ by explicitly changing the "Compile sources as..." setting.
I am extending a current c++ project with objective c code. For this purpose I compile the code as objective-c++ code. But since i changed the ending of a .cpp file into .mm, the compiler can't find two includes anymore.
One is a framework (#include <QTime>) and the other a simple header (#include "timecoder.h"). I changed the path of timecoder.h to its relative path and the compiler didn't complain anymore. But the problem with QTime still persists.
Do you have any idea what i could do?
Try using #import instead of #include.
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.