Do not adapt to the diet of the United States, when a salted fish in the apartment coding a wave, learn how to implement C + + programming in Ubuntu
The text reads as follows:
(Preliminary knowledge)Learning vim:http://www.cnblogs.com/starspace/archive/2009/05/15/1458044.htmlConfigure vim:http://haohetao.javaeye.com/blog/690715 Summary : Installing VIM--and configuring the/ETC/VIM/VIMRC file allows vim to support syntax highlighting, auto indent, and more. The contents of the VIMRC configuration file currently used by Wood are as follows, and it is sufficient to have basic C + + programming. ++++++++++++++++++++++++++++++++++ The syntax highlightingsyntax EnableSyntax onSet Background=darkSet NocompatibleSet number "Type of Detection filefiletype on "Record the number of rows in historySet history=1000Set Cursorline the Set autoindentSet Cindent "Sets the specific indentation for C + + languagesset Cinoptions={0,1s,t0,n-2,p2s, (03s,=.5s,>1s,=1s,:1sSet Smartindentset Expandtab "Use a space instead of tab."set ts=4 "Number of spacesset shiftwidth=4 the width of the auto indentSet ShowmatchSet CursorlineSet Nobackup "Just for encodeSet fileencodings=utf-8,gb2312,gbk,gb18030Set Termencoding=utf-8Set Fileformats=unixSet ENCODING=PRCset Hlsearch "Highlight all matches the Set Foldmethodset fdm=indent "code folding if &term== "Xterm"Set T_co=8Set T_SB=^[[4%DMSet T_SF=^[[3%DMendif +++++++++++++++++++++++++++++++++++++++ vim function is quite powerful, each person can according to their own needs DIY this VIMRC file, can create an extraordinary effect, can see the following content: http://blog.csdn.NET/wooin/archive/2007/10/31/1858917.aspx (i) Code writing 1. Start the terminal; 2. Input Vim test.cpp, a new file is called "Test.cpp", if this file has been established before, it is the file that opened the name. 3. The default entry is command mode, input I, a, O, etc. into the edit insert mode, corresponding to the current position before, after inserting and inserting a row. If you want to delete the contents of the line you are currently editing (delete a character, delete a word, delete a row), you must press ESC to return to command mode, using X (delete one character), DW (delete one word), DD (delete a line) to delete. if you want to undo this operation, go back to command mode, and then enter Command U (undo) To undo the operation, or, if you want to restore the undone content, in command mode, enter the command ": Redo" or ": Control key +r", without setting the compatible, Can be revoked and resumed several times; 4. Edit the end, enter ": w" to indicate the disk, and then enter ": Q" To exit the Vim editor, or directly enter ": Wq" to complete these two steps directly. (ii) code compilation run The content of the "test.cpp" file is:#include <iostream>int main (int argc,char *argv[]) {std::cout << "Hello, world/n";return 0; } 1. For this. cpp file, compile the file with the following command:Gcc-wall test.cpp-o test-lstdc++ (the-lstdc++ option must be used to notify the linker to link to the static library libstdc++.a, otherwise it will fail because the library function cannot be found)or use the command g++, which is dedicated to compiling C + + (. cpp) files, as follows:g++-wall test.cpp-o test ==============This command compiles the code in the file ' Test.cpp ' into machine code and stores it in the executable ' test '. The file name of the machine code is specified by the-o option, which is typically the last parameter in the command line. If omitted, the output file defaults to ' A.out '. Note that if a file with the same name as the executable file already exists in the current directory, it will be covered. option-wall turn on almost all common warnings from the compiler-it is strongly recommended that you always use this option. The compiler has a number of other warning options, but-wall is the most common. By default, gcc/g++ does not produce any warning messages. Compiler warnings are very helpful when writing C or C + + programs to detect problems with your program. in this example, the compiler uses the-wall option without generating any warnings, because the sample program is completely legal. In addition: if a "test.c" C language type of file is established at the outset, the content is as follows;#include <stdio.h>int main (void) {printf ("Hello, world!/n");return 0; } compile with the "Gcc-wall test.c-o testc" command. It is also important to note that C + + libraries can not appear in the file, such as # include <iostream.h> will be error, but the CPP file contains the C language of the library is possible. ============== 2. Run:$./test==============This command loads the executable file into memory and causes the CPU to start executing the instructions it contains. The path "./" refers to the current directory, so "./test" loads and executes the executable file ' test ' in the current directory. ==============The result is: Hello, World (c) compiling multiple source files 1. Examples of multi-source file filesThe above "Test.cpp" is decomposed into three different files ' main.cpp ', ' test_fn.cpp ' and header file ' Test.h '. (1) The main program ' Main.cpp ' is as follows:#include "test.h"int main () {Test ("Hello world!");return 0; } (2) The function declaration file "Test.h" is as follows:void Test (const char* name); (3) The contents of the function implementation file "Test_fn.cpp" are as follows:#include <iostream>using namespace std;#include "test.h" void Test (const char* name) {cout<< "Hello world!" <<endl; } 2. Multi-file compilationg++-wall main.cpp test_fn.cpp-o newtest =====================In this example, we use the option-O to specify a different name newtest for the executable file. Note that the header file ' test.h ' is not specified on the command line because it is already contained in the main.cpp. ===================== (d) Simple makefile fileThe make command reads the description of the item from makefile (default is the file named ' Makefile ' in the current directory). MAKEFILE Specifies the compilation rules for a series of targets (such as executables) and dependencies (such as object files and source files) in the following format:Goal: RelianceCommandfor each target, make checks its corresponding dependent file modification time to determine if the target needs to be re-established with the corresponding command. Note that the command line in makefile must be indented with a single tab character, not a space (correcting: a space instead of a tab). GNU make contains a number of default rules (see implied rules) to simplify the construction of makefile. For example, they specify that the '. O ' file can be obtained by compiling the '. C ' file, and the executable can be obtained by linking '. O ' to it. The implied rules are specified by things called make variables, such as CC (c language compiler) and CFLAGS (compiler option for C programs), and in the makefile file they are set in the form of a variable = value of the exclusive row. For C + +, the equivalent variable is cxx and cxxflags, while the variable cppflags is the compile preprocessing option. 1. Now create a simple makefile file for the "compiling multiple source Files" example above, with the following content:cxxc=g++Cxxflags=-wallNEWTEST:MAIN.O TEST_FN.O (using the implied rules Cpp-->o)clean:rm-f newtest main.o test_fn.o ===================The makefile file can be read like this: Use the C + + language compiler g++, and compile the option '-wall ', from the object file ' test.o ' and ' test_fn.o ' to generate the target executable newtest (file ' test.o ' and ' test_ ' FN.O ' is generated by ' test.c ' and ' test_fn.c ' by implied rules, respectively. The target clean is not dependent on the file, it simply removes all compiled files from the build. The RM command option '-F ' (force) suppresses the error message that is generated when the file does not exist. =================== 2. To use the Makefile file, enter the Make command. When make is called without arguments, the first target in the makefile file is built to generate the executable ' newtest ', and the terminal will have the following output: $ Makeg++-wall-c-o main.o main.cppg++-wall-c-o test_fn.o test_fn.cppg++ main.o test_fn.o-o newtestRun the executable file:$./newtestHello, world!. 3. A source file is modified to regenerate the executable file, simply re-enter make. By checking the timestamp of the target file and the dependent file, program make can identify which files have been modified and update their corresponding target files according to the corresponding rules: $ vim Test.cpp (if you open the editor to modify the Test.cpp file)$ make (re-make)g++-wall-c-o test.o test.c (at this time only make the modified file test.cpp)g++ test.o test_fn.o-o newtest Run:$./newtestHello, world!. 4. Finally, remove the make-generated file and enter "Make clean" to display the following:$ Make cleanrm-f Hello hello.o hello_fn.o Summary: The makefile file is similar to a batch file that compiles a connector, and all commands are written into a file called Makefile, and the Make command allows you to execute this file makefile like a batch file. A professional makefile file typically contains additional targets for installation (make install) and test (make check). The examples involved in this example are simple enough to be completely makefile, but it is necessary to use makefile files for any larger program. (v) Linking external libraries a library is a collection of precompiled object files, which can be linked into a program. The static library is prefixed with '. A ' for Special archive Files (archive file) storage. standard system libraries can be found in the directory/usr/lib and/lib. For example, in Unix-like systems, C-language math libraries are generally stored as file/USR/LIB/LIBM.A. The prototype of the function in the library is declared in the header file/USR/INCLUDE/MATH.H. The C standard library itself is stored as/USR/LIB/LIBC.A, which contains functions specified by the Ansi/iso C standard, such as ' printf '. For each C program, LIBC.A is linked by default. The following is an example of a sin function called in the math library LIBM.A, creating a file calc.c: #include <math.h>#include <stdio.h> intmain (void){Double x = sin (2.0);printf ("The Value of Sin (2.0) is%f/n", x);return 0;} trying to generate an executable file from the file alone will cause an error in the link stage: $ gcc-wall Calc.c-o Calc/tmp/ccbr6ojm.o:in function ' main ':/tmp/ccbr6ojm.o (. text+0x19): Undefined reference to ' sin ' The function sin, not defined in this program, is not in the default library ' LIBC.A '; unless specified, the compiler will not link ' libm.a '. 1. Law IIn order for the compiler to link sin into the main program ' CALC.C ', we need to provide the math library ' LIBM.A '. An easy-to-think but troublesome practice is to explicitly specify it on the command line: $ gcc-wall Calc.c/usr/lib/libm.a-o Calc the function library ' LIBM.A ' contains the target files for all mathematical functions, such as Sin,cos,exp,log and sqrt. The linker will search all files to find the target file that contains the sin. Once the target file containing the sin is found, the main program can be linked and a complete executable file can be generated: $./calcThe value of sin (2.0) is 0.909297 The executable file contains the machine code of the main program and the machine code corresponding to sin in the function library ' LIBM.A '. 2. Law twoto avoid specifying a long path on the command line, the compiler provides a quick option '-l ' for the link function library. For example, the following command $ gcc-wall Calc.c-lm-o Calc equivalent to the command above that specifies the full path '/USR/LIB/LIBM.A ' of the library. In general, the option-lname causes the linker to attempt to link the library file Libname.a in the System library directory. A large program typically uses a lot of-l options to specify the math libraries, graphics libraries, network libraries, etc. to link to. Source: http://blog.csdn.net/deutschester/article/details/5690111
Linux: How to do C + + programming