Compile C ++ code using command lines in Mac OS and reference it in xcode

Source: Internet
Author: User

1. Write c ++ classes:

C. h

# Include <stdio. h>

Class AA {

Public:

Void go ();

};

 

C. Mm

# Include "C. H"

AA: Go (){

Printf ("AA: Go ");

}

2. Compile the class just written. It can be divided into two runtime environments. One is to run the class in the simulator and the compilation target.CodeThe architecture is i386, one is to run in the real iPhone iPad, and the target code architecture is armv7. First, compile the code that can be run in the simulator. Use the GCC compiler of the simulator to open the command line, type the following command:

CD/Program/code/

/Developer/platforms/iphonesimulator. Platform/developer/usr/bin/llvm-G ++-4.2-arch i386-c-I././C. Mm

In this waySource codeDirectory will generate a c. o file, which is the target file we need. To compile the ARM architecture

/Developer/platforms/IOS. Platform/developer/usr/bin/llvm-G ++-4.2-arch i386-c-I././C. Mm

3. Open xcode and create an iOS app.ProgramClick the project name, select build phases, select link binary with librarys, expand the project, click the plus sign, and select the generated C. o. Compile and run the program. The calling code of objective-C is as follows. Note that the extension of the source code file of objective-C should be called. change m. mm to support the combination of objective-C and C ++:

-(Void) viewdidload {

[Super viewdidload];

AA * A = new AA ();

A-> go ();

Delete;

}

Note:

I tried again. Changing llvm-G ++-4.2 to G ++-4.2 can also be compiled successfully and run well, it can be referenced by the llvm-GCC (used by xcode) compiler after compilation by the GCC compiler.

/Developer/platforms/IOS. Platform/developer/usr/bin/g ++-4.2-arch i386-c-I././C. Mm

 

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.