Transferred from: http://hi.baidu.com/onejw/item/f34390c997cdc226a1b50aehttp://www.cocoachina.com/ask/questions/show/57217
Http://www.cnblogs.com/uyoug321/archive/2010/12/11/1903499.html
Support for C, C + +, OBJECT-C3 languages in Xcode
Xcode supports C, C + +, OBJECT-C3 languages, then if you want to make the compiler mixed, only need to change the implementation class ". M" format to ". MM", so that the compiler can compile to allow C, C + +, OC code;
But the mixed code, the most prone and most error in compiling is as follows:
View Plain
- command/developer/platforms/iphonesimulator.platform/developer/usr/bin/g++-4.2 failed with exit
Some of the analysis is summarized as follows:
1. (global variable) static method name repetition-because there is no concept of object in C, its method can be used as long as the corresponding. h file is added, so if the static method with C + + or objective-c is the same, the environment cannot be distinguished, it is obvious that This error is a compile time error, and for static variables and methods are added at compile time, so it is not recognized.
2. (namespace) The name of the global variable is duplicated, the reason is the same as above, of course, if it is a global variable, it is recommended that the objective-c. m file (which is now changed to. mm) in the fame, otherwise, the declaration in. h alone is no problem if you directly reference C or C + +. H, Then even if the global variables are not duplicated at this time, the above problem will occur.
3. (main function) contains more than one main method, the main method is the entry of the program, if there are multiple files containing the main method, the above problem will also occur.
In fact, for the "failed with exit" problem, in general, this problem is because of the method or global variable duplication caused by the compilation environment is not recognized and compiled! So children's shoes when using a mixed program to pay special attention, the best solution is that if you have a 1/100000 chance of the project will be used in a mixed (for example, to join the box2d development package), then you should start from the project to modify the implementation class to ". MM" format to compile code, To prevent complex mixed problems at a later stage;
Note the following two points:
Automatic Reference mode to turn it off.
Compile Source as---> Objective C + +
Transferred from: http://www.cocoachina.com/ask/questions/show/57217
You can write an OC class, the header file is declared in OC method, in the original file directly call C + + on the line. However, the original file suffix name should be changed to. mm
As an example:
@interface Myoc:
NSObject
-(void) importrootcertificate;
@end
@implementation Myoc
-(void) importrootcertificate{
A *a = new A ();
A-> importrootcertificate (Prame,....); /your C + + call.
}
@end
Use
Myoc *oc = [Myoc alloc] init];
[OC importrootcertificate];//This indirectly calls your C + +.
Transferred from: http://www.cnblogs.com/uyoug321/archive/2010/12/11/1903499.html
Mix C or C + + code into OBJECT-C code in Xcode
Object-c is actually a language in C or C + + code that embeds the call of a run-level object!
What is a run-level object call, that is, the object call is not at the syntax level, but in the compiled run-level, similar technology also has Microsoft's COM and open object organization CORBA, but the latter two do not add the contents of the run-level object calls to the implementation of the language, but the use of separate IDL syntax and files!
Understand the above thing, you can understand, is to let the compiler to the source file as C or C + + to compile!
Method One: Directly change the extension
The. m file compiles the source file as a C file written with Object-c
The. mm file compiles the source file as a C + + file written with Object-c
Method Two: Change the file properties through Xcode
Select File to open filey->get Info for menu
To change the drop-down selection for file type, I believe the programmer can see what it means at a glance:)
C, C + +, OBJECT-C3 languages in Xcode