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.
Related
I included a custom header file, with a "test" function defined in it, as I am learning to include custom header files.
I was able to add the directory of the header file to the compiler "include" list, and Eclipse built the project fine.
However, when I try to run it, I get an error saying that it can't find the header file anymore.
use include like this:
#include "../HeaderFolder/header.h"
I have a source code containing .c files which is built using ndk build in eclipse Ide. I want to add a .cpp file , in which i need to import my .c files. I get compile time issues when I do the same. Most of the issues are due to type casting to user defined data types. The compilation runs fine, and ndk -build is successful with only .c files
jni/folder1\folder2\folder3\folder4\src\abc.c:963:29: error: invalid conversion from 'int' to 'MY_STATUS_CODE' [-fpermissive]
How can I solve this?!
You can't include C code in a C++ source file unless the C code is valid C++. The compilation error shows that your C code is not valid C++.
You could try to work around the problem by changing your C code to be also valid C++ but that's a waste of time in my opinion. The proper way to solve the error is to not include the C source file but compile it separately. Including a source file in another is bad design anyway so you'll get rid of that smell for free.
I am trying to figure out why XCode cannot resolve a C++ file I imported.
I have changed the file extension to .mm
I have added the .h file contents to the top of my file
I have command line tools installed
The code complete detects the algorithm library:
But when I try compile I get this:
Ok I found the issue.
As it turns out you need to not only use the .mm extension to invoke the objective-c++ compiler, but also anything that REFERENCES the .mm too. So I had a view controller that referenced the c++ header.
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.
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.