C, C ++, Object-C3 language mixing in xcode

Source: Internet
Author: User
Transferred from: Http://hi.baidu.com/onejw/item/f34390c997cdc226a1b50ae Http://www.cocoachina.com/ask/questions/show/57217

Http://www.cnblogs.com/uyoug321/archive/2010/12/11/1903499.html

Xcode supports mixing of C, C ++ and Object-C3 languages

Xcode supports C, C ++, Object-C3 languages, so if you want to make the compiler mixed, you only need to implement the class ". M "format changed ". mm ", so that the compiler can compileCode;

However, the most common errors in compilation are as follows:

View plain

    1. Command/developer/platforms/iphonesimulator. Platform/developer/usr/bin/g ++-4.2 failed with exit

Some analyses are summarized as follows:

1. (global variable) the static method name is repeated-because there is no object concept in the C language, the method only needs to be added to the corresponding. the H file can be used. Therefore, if it is the same as the static method of C ++ or objective-C, the environment cannot be distinguished. Obviously, this error is a compilation error, static variables and methods are added during compilation, so they cannot be identified.

2. (namespace) The global variable name is repeated for the same reason as above. Of course, if it is a global variable, we recommend that you use objective-C. M file (of course changed. mm). Otherwise, it is declared in. if C or C ++ is referenced directly. h, the above problem will occur even if the global variables are not repeated at this time.

3. The (main function) contains multiple main methods. The main method isProgramIf multiple files contain the main method, the above problem also occurs.

In fact, for the "failed with exit" problem, in general, this problem is caused by repeated methods or global variables that make the compilation environment unable to recognize and compile! Therefore, when using the mixed editing program, you should pay special attention to it. The best solution is that if your project has a 1/100000 chance of using the mixed editing program (for example, adding to the box2d development kit ), the implementation class should be modified from the beginning of the project ". MM "format to compile the code to prevent complicated mixing problems in the future;

Note the following:

Disable the automatic reference mode.

Compile source as ---> Objective C ++

From: http://www.cocoachina.com/ask/questions/show/57217

You can write an OC class. The header file is declared using the OC method. You can directly call C ++ in the original file. However, the suffix of the original file should be changed to. Mm.

For example:

@ InterfaceMYOC:
Nsobject

-(Void) Importrootcertificate;

@ End

@ ImplementationMYOC

-(Void) Importrootcertificate {

A * A = new ();

A->Importrootcertificate (prame,...); // your c ++ call.

}

@ End

// Use

MYOC * OC = [MYOC alloc] init];

[OC importrootcertificate]; // This indirectly calls your c ++.

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 for embedding runtime-level object calls in C or C ++ code!

 

What is a running-level object call, that is, an object call is not at the syntax level, but at the runtime level after compilation. Similar technologies include Microsoft's com and open Object Organization's CORBA, however, the last two do not add the calling content of the running-level object to the implementation language, but use separate IDL syntax and files!

 

After understanding the above, we can understand that the compiler should regard the source file as C or C ++ for compilation!

Method 1: directly change the extension

The. M file is to compile the source file as a c file written with object-C.

The. MM file is to compile the source file as a C ++ file written with object-C.

 

Method 2: Use xcode to change the file attributes

Select a file and open Filey-> get info of menu.

Change the drop-down selection of file type. I believe the programmer can see at a glance what it means :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.