Previous article Linux C + + programming--Introduction we have introduced the gun, GCC, g++ and other important concepts, should now start hands-on practice!
Installation of development tools
Environment
Distributions version: CentOS 6.7
Linux kernel slices: 2.6.32-573.3.1.el6.i686
After installing the General Linux installation by default (GNU Compiler Collection), you can check the GCC and g++ version numbers to see if GCC and g++ are installed.
[luowf @luoweifu ~]$ GCC-VGCC version 4.4 . 7 20120313 (Red hat 4.4 . 7 -16 ) (GCC ) [Luowf @luoweifu ~]$ g++-VGCC version 4.4 . 7 20120313 (Red hat 4.4 . 7 -16 ) (GCC )
If it is not installed, you can install the development toolset with the following command when the Linux system is linked to the network:
"Development Tools"
First HelloWorld Program
When it comes to programs, the first one is definitely helloworld, so let's start with HelloWorld, write a simple Hello program, and compile and run.
Write the first program
[LUOWF@luoweifucplusplus]$pwd/home/luowf/Workspace/cplusplus//write the first HelloWorld program in Vim[LUOWF@luoweifucplusplus]$ Vim HelloWorld.cpp1#include <iostream>2 3 intMain ()4{5Std::cout <<"Hello wolrd!"<< Std::endl;6 return 0;7}//Compile program[LUOWF@luoweifucplusplus]$ g++ HelloWorld.cpp [LUOWF@luoweifucplusplus]$lsA.out HelloWorld.cpp test1 TEST2[LUOWF@luoweifucplusplus]$./a.out Hello wolrd! [LUOWF@luoweifucplusplus]$
The difference between g++ and GCC
In the previous article, we have actually introduced GCC and g++, saying that GCC is a C compiler, and g++ is a C + + compiler. Does that mean that GCC compiles only C, and g++ only compiles C + +? In fact, GCC can also compile C + + programs, and C + + is a C based on the development, so g++ can not compile C language. The differences between them are as follows:
1. The suffix is. C, GCC treats it as a C program, and g++ as a C + + program; the suffix is. cpp, both of which are considered C + + programs. Note that while C + + is a superset of C, there are differences in the requirements for syntax, and the syntax rules for C + + are more rigorous.
2. During the compile phase, g++ calls GCC, which is equivalent to C + + code, but because the GCC command is not automatically linked to the libraries used by C + + programs, it is usually done with g++ to complete the link, for the sake of unification, simply compile/link all with g++, which gives an illusion, As if the CPP program can only be used g++. Compile with GCC, add the libraries you want to link to in the options, or compile C + +. As the HelloWorld program above, we can also compile with the following command:
[luowf@luoweifuCplusplus]$ HelloWorld.cpp[luowf@luoweifuCplusplus]$ lsa.out HelloWorld.cpp test1 test2[luowf@luoweifuCplusplus]$ HelloWolrd![luowf@luoweifuCplusplus]$
- GCC does not define __cplusplus macros, and g++ will
This macro simply indicates that the compiler will interpret the code in C or C + + syntax, as described above, if the suffix is. c, and the GCC compiler is used, the macro is undefined, otherwise it is defined.
Reference article: http://www.linuxsky.org/doc/dev/200804/298.html
Compilation of programs and common commands
Compile the source code into a target file without linking
gcc -c HelloWorld.cpp
Optimized execution speed According to the operating system at compile time
-O HelloWorld.-c
Link the target file into two (executable) files
-o HelloWorld HelloWorld.-lstdc++
HelloWorld is the executable file name, HELLOWORLD.O is the target file name, and if the C source file of the. c suffix is not added-lstdc++, if it is a C + + source file with a. cpp suffix, add-lstdc++.
When making a binary file, fill in the linked function library with the associated path
(.cpp文件)gcc -lstdc++ HelloWorld.cpp
Or
(.c文件)gcc HelloWorld.-lm-L-I /usr/include
Description
-LM refers to the libm.so or LIBM.A function library file;
The path followed by-L is the search directory for the function library just above;
-I is the directory where the include file (which contains the MATH.H header file) is located in the source code.
The HelloWorld.cpp code is as follows:
1 #include <iostream> 2 #include <math.h> 3 4 intMain ()5{6 STD::cout<<"Hello wolrd!"<<STD:: Endl;7 floatValue =Sin(3.14/2);8 STD::cout<<"Value:"<< value <<STD:: Endl;9 return 0;Ten}
Outputs the results of a compiled link to a specific file name
-lstdc++-o HelloWorld HelloWorld.cpphelloWorld为输出的文件名
At compile time, output more information to explain
-lstdc++-o HelloWorld HelloWorld.-Wall
C + + programming under Linux--development environment construction and first program