I have a project in xcode where I use a c++ library, it use to work and compile correctly, but whithout any change now it won't compile telling some errors in the .h files libraries and in my view controller where I use some c++ variables, I have my file with the .mm extension and all the errors I'm having are about the c++ syntax like in the word namespace it tells it's Unknown type name or in every other line where the syntax is c++ type Xcode can't recognize it.
Found out by my self on this page http://answers.oreilly.com/topic/631-how-to-get-c-and-objective-c-to-play-nicely-in-xcode/
The problem was that the compiler was making a mess because only the files where I used c++ code I put them the .mm extension and there was some files with just .m
By changing the extension of every file in my project to .mm even if it hasn't any c++ code solved the problem.
Related
In Xcode, I've created a "Cocoa application" project. One of its dependencies is a framework containing C++ code. I renamed AppDelegate.m to AppDelegate.mm and included the framework.
The project fails to compile. The problem is that the C++ header files in the framework are using some symbols that conflict with Objective-C or Cocoa.
The C++ header files are defining functions called verify() and check(), which conflict with /usr/include/AssertMacros.h in the MacOSX10.8 SDK.
The C++ header files contain a variable called NO, which conflicts with the Objective-C macro NO.
A workaround would be to modify the C++ code in the framework to avoid these conflicts. But since it's a large C++ project maintained by another organization, this would take time and would possibly break in future updates of the C++ project.
Is there some way just to tell Clang/Xcode to treat those C++ header files as C++ instead of Objective-C++?
Reading through the /usr/include/AssertMacros.h that comes with Mac OS 10.8, it looks like you could do:
#define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
before including AssertMacros.h, which will prevent it from defining macros called verify() and check().
Regarding NO: you could use the preprocessor to rename that variable for you. For example:
#define NO NO_libraryname_renamed
#include <libraryname.hh>
#undef NO
Depending on how the NO variable is used by the library, this might cause problems — if the header is declaring it as extern, then your Cocoa app will refer to it by the wrong name, and you'll get an undefined symbol error. But as long as you're not using that variable, and the library isn't depending on your app to define that variable, then you should be fine.
(And please file a bug report with the offending library, requesting that they rename their variable.)
Mixing several languages is calling for grief. Even more so mixing Objective C++ (itself a strange hybrid) with C++. Don't do it.
Can a NSIS plugin I made contain C++ classes or can it only be C code?
I am developing a NSIS plugin as a Win32 DLL but I am getting a compile error in my header file at the line where my class is declared.
The compile error is:
error C2061: syntax error : identifier 'MyClass'
The line that causes the compile error is:
class MyClass
Whats going wrong? Am I not allowed classes in a Win32 DLL? I am compiling in MS Visual C++ 2010.
I made this mistake many years ago and spent an hour scratching my head. I was looking round for a compiler option that would select whether code is compiled as C or C++, but there isn't one that you can select in the IDE (though you can control this from the cl command-line using /Tc and /Tp). However, the answer is simple.
If a source file has a .c extensions it's compiled as C. If it has a .cpp extension it's compiled as C++.
Additionally, if you put C++ constructs (such as classes) in a header file and #include them in a C file you'll get the same problem. You can hide the C++ constructs from C using #ifdef __cplusplus.
I'm trying to use code that already works in another standalone project without problems.
When I bring this code into my other final project, it indicate that 'cmath' file not found.
The file with the #include is in an .hpp file (it just defines some structs for opengl stuff), so there is not a corresponding .mm file. (This is C++ code and I'm mainly an objective-c user so not sure of all the c++ stuff)
But this is for opengl stuff and works fine we not in this project.
I've tried everything to make this work.
The final project with the problem has other code that uses #include without problems.
It is almost like something is causing xcode not to recognize the path the header anymore.
I've checked its file type it is "Default C++ header"
In the final project I'm using Zxing and also using CorePlots. Not sure if they are causing any problems. Also some file are using #include not sure if that could conflict with the #incude or not. (But again the other files with #include are working fine.
Any help will be greatly appreciated...
Alternately to Jon Reid's good advice you can pick "Compile as Objective-C++" in the Build Settings. (Search for "Compile sources" in the Build Settings window.) This is a little bit wild-west but is a quick way to see if the problem is in fact your C++ source compiling as C or Objective-C
Your header file doesn't exist by itself; something must import it. Rename the importing files, changing their file types from .m to .mm.
For example, let's say your header that includes <cmath> is named foo.h. And that this in turn is used by a bar module, composed of bar.h and bar.m. So foo.h is imported in either bar.h or bar.m.
Rename bar.m to bar.mm so that it uses C++. Do the same for all .m files that depend on foo.h.
I need to add a .mm file which contains both ObjectC and C++, I am wondering how to do it using XCode 4?
There are absolutely no issues in doing that, just add the .mm fle to the project and XCode will compile it as Objective-C++.
The only caveat is that the .h associated with that ObjC++ code must not contain any C++ specific code or every file in which that header is included must be .mm too.
This because XCode will use different compiler according to the single file, so if a .m is found it will try to compile it as plain ObjC and not ObjC++. You can force to compile it with che ObjC++ compiler but I suggest you to follow the principle described or rename other files to .mm just to avoid getting things complicated.
In the zxing iphone project the readme states:
It can happen that when trying to build your own project with
ZXingWidgetController you get linker
errors like "undefined reference to".
If this error looks like a c++
undefined reference, then renaming
main.m into main.mm (Objective-C++
source suffix) may fix the problem
It did indeed. But I'm wondering why?
.mm extension stands for Objective-C++, when compiler can process C++ classes.
But when using .m extension it will be able to compile only C code, without C++ classes.
Both .m and .mm are class file extensions of source code for Mac-based applications. .m files can contain both Objective-C and Objective-C++ classes. To avoid conflicts between the two in mixed-use scenarios there's the convention to rename all Objective-C++ class files to .mm. This helps compilers to distinguish.
So in a project which uses both Objective-C and Objective-C++ you will see:
.m files containing Objective-C
.mm files containing Objective-C++