C + + development environment under MAC

Source: Internet
Author: User

1. Xcode Create C + + Project

Xcode (version 4.6.3) supports the creation of C + + projects by default, with a simple procedure:
Open Xcode and create a new project;
In OS X, select Command line Tool in application.

Then, in the next project settings, set the Type to C + +;

If you don't need to use ARC, uncheck it (not available in C + +);
In this way, you create a basic C + + project in Xcode, Run:

It is important to note that the default C + + compiler for Xcode is Apple LLVM Compiler (version 4.2 of 4.6.3) and Clang, the default C + + standard library is libc++ (standard library provided by Clang) and currently supports C + + 11 ( Previous c++0x) standard. Xcode also provides the LLVM GCC compiler, which comes with the C + + standard library libstdc++ (GNU C + + standard library). When introducing some third-party libraries, it is important to note that these choices, such as OpenCV, may require the standard library to be set to libstdc++.
IDE is easy to use for project development because it integrates such features as document, auto-complete, and visual environment configuration, but how to compile C + + projects directly using the command line without the IDE? Very simple, use the compiler directly.

2. C + + compilation process

Before introducing the compiler, let's simply say the C + + compilation process to understand the compiler's work.
Compiling (compiling) does not mean that only one executable file is created. Creating an executable file is a multistage process, the most important of which is preprocessing (preprocessing), compiling (compliation), and linking (linking). From the source code file to an executable file throughout the process, the best word is build (Chinese translation, there is called generation, there is called compile link, also called build). Compiling is only part of the build process, but you often encounter many people who refer to compile as the whole process. Typically, you do not need to run separate commands for these processes, which the compiler will call itself, such as a preprocessor.

2.1 Preprocessing

The first step in the build process is for the compiler to run the C preprocessor, which is intended to handle text on the code file. It handles header files containing directives (#include), conditional compilation directives (#ifdef ... #endif) and macros (#define), which are called preprocessing directives, all starting with the well character #. The compiler itself is absolutely invisible to these preprocessor directives.
Like what:

    1. #include <iostream>

This code will tell the preprocessing instructions, to take the contents of the iostream file to the current file, each containing a header file, it will paste the contents of the header file into this file, and then the #include instructions removed.

    1. #define MY_NAME "Alex"

A macro is a string content that is replaced by something else (possibly more complex), at which point the preprocessor puts the following code:

    1. cout << "Hello" << my_name << Endl;

Expand into:

    1. cout << "Hello" << "Alex" << Endl;

Because the preprocessor handles the code before the compiler, it can also be used to remove code--sometimes you want to execute some test code in the code. You can tell the preprocessor that if a macro is defined, it contains some code. Then, if you want to execute this code, define the macro, or remove the macro definition.

#include <iostream>  #define DEBUG  using namespace std;  int main ()  {  int x;  int y;  cout << "Enter value for x:";  CIN >> X;  cout << "Enter value for y:";  Cin >> y;  x *= y;  #ifdef DEBUG  cout << "x:" << x << ' \ n ' << "y:" << y;  #endif   }  

If you do not want to perform the printing of variables, simply comment out #define DEBUG on the line.
In the same way, you can change the condition with #ifndef-if not defined ... This method is often used when introducing multiple headers.

2.2 Compiling

Compiling means turning a source file (. cpp) into an object file (OBJECT,.O or. obj).
An object file encapsulates every function in your program into a form that a computer processor can understand-machine instructions (machines language instructions). Each source file is compiled separately, that is, the machine code that the object file contains is compiled source code. For example, you have three source files, compiled, generated three object files, each object file contains the corresponding machine code.
But you can't run them yet, and that's when you need the linker.

2.3 Links

Linking (linking) is the creation of a single executable file (such as an. exe or. dll) by putting a bunch of object files and libraries (and sometimes just an object file, but also needing links).
The linker creates an executable file in an appropriate format and passes the contents of each individual object file to an executable result. The linker also handles references to functions other than the source code of the object file, such as functions in the C + + standard library. When you call a C + + standard library function, such as cout << "Hi", you are using a function that is not defined in your own code, it is defined in a related object file, but it is provided by the compiler and does not belong to you. At compile time, the compiler knows that this function is valid because you draw out the iostream header file, but since this function is not part of the CPP file, the compiler leaves a stub in the call tree, and the linker traverses the object file for each stub, It will find the correct function address and then replace the corresponding stub with the correct address from the other object files that have been linked.
This process is sometimes called correction (fixup). When you separate your program into multiple source files, you will use the linker to fix all functions that have been called in the source file. If the linker cannot find the location of the function, it generates a undefined function error, which does not mean that the code is correct, even if the code is passed by the compiler. The linker is the first to detect this error from a global perspective.

3. GCC and LLVM

MAC, if you have Xcode installed, you haveLLVMAndGCCTwo large compilation tools.
3.1 LLVM
LLVM, originally called "low level Virtual machine", the project leader and the original author was Chris Lattner and Vikram Adve, beginning in 2000 at the University of Illinois at Urbana-Champaign (University of Illin Ois at Urbana-champaign), Apple hired Chris Lattner in 2005 to set up a team to focus on the various uses of the LLVM system on the Apple development system. LLVM is now an important part of Apple's development tools for Mac OSX and IOS.
The LLVM project is a comprehensive project (umbrella project) that includes a range of techniques related to development tools, such as:
compiler, CLANG,LLVM native C/c++/objective-c compiler, designed to provide a fast compiler
debugger, Lldb, a debugger built on the basis of LLVM and Clang
LLVM technology implementation of JIT system, Vmkit, Java and. NET Virtual machines
Optimizer, Dragonegg, integrated LLVM optimizer and code generator with GCC parsing tools
......
3.2 GCC
GCC, all called the GNU complier Collection,GCCis a key component of GNU Project, initiated by the father of free software, Richard Stallman, on September 27, 1983 at MIT to provide a free and controllable computing environment for all computer users, who are free to run, share, learn, and modify software, That is free software.
GCCOriginally called GNU C Compiler, only supported C language compilation, after 1.0 began to support C + +, and then supported the Objective-c,objective-c++,fortran,java,ada,go and other languages.
As for the choice of which as the first choice, see the specific situation.
GCCIt has a long history and is widely supported, and many open source projects use GCC directly as a compiler. Of course, Clang is an open source project that can be used across platforms, and Clang has the advantage over GCC: Faster compilation: On some platforms, the Clang compiles significantly faster than GCC. Small footprint: Clang generates an AST that consumes about one-fifth of GCC's memory. Modular design: The Clang uses a library-based modular design that is easy to reuse for IDE integration and other purposes. Diagnostic information is readable: During compilation, Clang creates and retains a large amount of detailed metadata (metadata), which facilitates debugging and error reporting. The design is clear, simple, easy to understand and easy to expand and enhance. The learning curve is gentle compared to the old code-based GCC.

Compile a C + + code under Clang:

GCC under Compile:

They are similar in usage. GCC and clang have similar compile-options commands, and we can do a lot of things with these commands. Like what:
GCC and Clang with the appropriate compilation options, you can see some steps in the build process:
-E, you can only perform the preprocessing phase, such as: clang++-e HelloWorld.cpp results will print out the preprocessed code, HelloWorld only 7 rows, after preprocessing there are tens of thousands of rows.
-S, execute to compile phase: clang++-S HelloWorld.cpp this stage, will generate a corresponding name of the. S file, which contains the so-called machine instructions-assembly code, HelloWorld.cpp generated 86 lines of assembly code
-C, executes to the post-compilation stage, generating the. O Object file for the corresponding name
For options on both tools, you can see more detailed introductions using the man gcc and man clang.

C + + development environment under MAC

Related Article

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.