Go Research on C + + development under Linux

Source: Internet
Author: User
Tags gcov thread stop perl script ultraedit

1. Development tools

Under Windows, development tools are presented to end-users in the form of an integrated development environment IDE. For example, VS2008 integrates editor, macro Assembler ml,c/c++ compiler cl, resource Compiler RC, debugger, document Generation tool, NMAKE. They are provided to end users in an integrated manner and are very convenient for beginners. However, this business model directly leads to poor user customization, is not conducive to automation, and the ability to integrate third-party tools is weak. For example, you cannot customize some macros to handle repetitive operations, and you do not realize the makefile one step pleasure of automation; You cannot remotely log on to the server for development, and you cannot use some kind of "binder" to effectively invoke third-party tools (for example, text Tools, string tools). It can be said that good business support and fool-type development are their main advantages.
Under Linux, development tools are cut into separate gadgets. Each deal with a different problem. For example, the editor (Emacs, VIM) is used for editing programs, the debugger (GDB) is used to debug programs, the compiler (GCC) is used to compile and link programs, the Performance analysis tool (Gcov, gprof) is used to optimize the program, the document generator (Doxygen) is used to generate documents. At the same time, there are some system tools and system knowledge, we are very necessary to understand: Program Automation mechanism makefile, System binder shell, System Finder Tool grep, locate, find. Other tools (such as Ctags, OCI's MPC, etc.), once mastered, will be the weapon in your hands.

This is mostly a experience of the use of development tools for Linux. Because of the variety of tools, we do not have the ability or need to introduce each. For Linux under IDE tools, such as Eclipse, Anjuta, and so on, although they are also very useful, but it is relatively simple to use, and currently is not mainstream. Therefore, they will not be highlighted. At the same time, this article does not intend to write the operating manual of each tool, only focus on the various tools to solve the problem, operating mechanism and main characteristics.

1.1 Editor

To develop, the first thing to do is to choose an appropriate editor. There are several features of the editor selection:
1) Reduce the editing time by reducing unnecessary editing actions.
All the editing tasks that can be described without ambiguity could and should be automated. For example, every C + + program will have a main function, and when we define the. h file, we want to include some preprocessing directive # define to help us solve the problem of repeatedly referencing the same header file. Mouse operation is always slower than keyboard operation. This aspect of Emacs can be considered to be the ultimate. So, Emacs users often boast that they are editing at the same speed as they think.
2) High scalability.
Programmers expected editors should be able to provide some programmatic help, such as syntax highlighting, auto-completion, automatic typesetting, grammar checking, and so on. Take a look at Gedit, Vim, Emacs, and UltraEdit, and you'll find that they're far more primitive than Windows Notepad, WordPad provides. For a new language, the new syntax, they should be able to easily provide support, rather than stay in one or several fixed language.
3) High user-customizable nature.
If you want to work long-term research and development, especially under the Linux/unix, then you need to learn a functional enough editor. There is a sentence: Linux programmers are divided into three kinds, using Emacs, using VI, and others. Emacs is a GPL editor written by Stallman in the Lisp language. The Emacs we're talking about here refers to GNU Emacs, not xemacs. Because of its openness, we can make it a powerful IDE. After we have installed the Cgywin, we can also use EMACS under the Windows system. Cgywin and MinGW are third-party tools that simulate POSIX systems on Windows systems.
Emacs is more of an operating system than an editor. We can use it to write programming, to write wikis, to send and receive emails, and so on. Emacs is mainly extended in two ways: El script (Elisp is a dialect of Lisp) and third-party expansion packs. Emacs's entry costs are high. Because it is a purely keyboard operation, you need to remember a large number of shortcut keys, powerful is through the user to add some extension packages, Lisp script to achieve. How to properly configure and modify is very patient and skillful.

1.2 Translator

The compiler prefers GCC (GNU COMPILER COLLECTION). There are two reasons for this, which is GNU open source, and it supports up to 96.15% of standard C + +. And vc++6.0 's support is only 83.43%. GCC is not only the usual C or C + + compiler, it can also compile other languages such as Java. GCC is the GNU C compiler, g++ is the GNU C + + compiler, and Egcs (enhanced GNU Compiler Suite) can be considered an improved version of GCC.
The compiler language from the source program to the target code will go through the following stages: the source program-> Assembler-> compiled into an obj program, linked to the final executable program. We can do all the steps with a compile instruction. You can also step into the execution. GCC has three important options-E (preprocessing only),-s (generate assembly code),-g (Generate executable with the original code debug symbols, if you want to debug with GDB, you should open this option at compile time).
GCC can be seen as a package, in addition to the compiler tool, it also integrates debugger GDB, profiling Tools Gcov, Gprof. As long as we have GCC installed, these powerful tools can be used directly.
With Gcov, we can view a program that runs each line of code in the source code. When we optimize the code that runs the most, we can greatly optimize the program. When using Gcov, you need to open GCC's Fprofile-arcs and ftest-coverage two options. The options commonly used in Gcov are the-B branch statistics.
With the Gprof tool, we can see the order of calls between functions and the time each function runs. We can understand gprof as a linux/unix with the tool time. When using gprof, you need to turn on the PG option for GCC.
The common denominator of Gcov and gprof is that when compiling a program, it is necessary to add some of its own auxiliary information, which can be diagnosed by the program. In addition to these optimizations, we can also use some memory leak tools to reduce the amount of memory space that is not released by the wild Pointer.
Debugger
GDB is the GNU debugger, which is a performance-quality debugger that comes with GCC. By combining GDB and scripting, we can implement regression testing well.
GDB can run on both CLI and GUI two modes. The default gdb is CLI mode, we can go to download and install GUI mode gdb, such as XXGDB, DDD, etc. A better way is to use GDB in Emacs. GDB includes all the debugging features of the Visual Studio Tools, and includes features that it does not have. In addition to support, we generally set breakpoints, single-step tracking, step in, step out, step over, and some other powerful features. In gdb, we can pause in the following ways: Breakpoints (breakpoint), Observation points (watchpoint), Snap points (catchpoint), signal (signals), thread stop (threads Stops).
Here are a few of the features that impressed me. 1) The Watch command allows the program to pause when the value of a variable is changed. 2) with the print instruction, set the value of the variable when the program runs, and run a method supported by the program itself. 3) with the until instruction, we can let the program pause when it runs to a program. 4) Pass the break: if directive, which causes the program to pause when it satisfies a bool expression.

1.3 Adhesives

I want to use the word "adhesive" to express the "glue" that binds multiple tools together. For example, through shell scripting, we can concatenate OS commands, sed instructions, awk commands, and other script files to play their combined forces. In Linux C + + programming, we will inevitably use the makefile file. Through, it can be compiled instructions, generate document operations, cleanup operations and so on series together. In a sense, it is also equivalent to an adhesive.
The starting point of makefile is to maintain the dependencies of many files in a project, thus obtaining a topology diagram of the source program. When we modify only one of the nodes in the diagram, we just need to compile the associated link in the topology diagram. As a result, the compilation time is greatly shortened. Make has two major concepts: dependencies and rules. Rules rule defines an action rule for each dependency dependency. This fine-grained separation allows us to customize the behavior of software builds. For example, modify the compiler used, modify the Includepath, modify the LIBPATH, modify the compilation options, and so on. NMAKE in our common VC, features and make are similar.
The focus and difficulty with make is to write makefile files. Makefile's syntax is quite different from other languages, so we should pay special attention to the difference between tab and SPACEBAR. There are many tools available to help us generate makefile. The most famous is the GNU autoconf. The preparation of a GNU program requires AutoScan, aclocal, autoconf, automake four tools.
We know that the three steps of the GNU Software Installation are:./configure, make, make install. One of the./configure is to generate a makefile file based on autoconf, alocal and other tools. The make command calls the make command to compile the source program according to the rules of the makefile file. The make install is the action (typically a copy operation) indicated by the install rule in makefile. Make Clean is the action (typically an RM operation) that is indicated by the clean rule in makefile. When we developed managed C + + project with ECLISPE+CDT, it generated makefile by objects.mk,subdir.mk,sources.mk three files. We look at the output information at compile time, and we can see the contents of the makefile file displayed.
It can be said that if you want to compile a cross-platform C + + program, then makefile is the most convenient mechanism. OCI has written a great Perl script--MPC for the Ace,tao Open source community in Douglas C.schmidt. It consists of platform information, a rule file, source code, generate the project files that the user wants, such as make, Nmake, Visual C + + 6, Visual C + + 7, and so on. Google Web Tookit, Celtix do things similar, but they are for Java, and the MPC is for C + +.

2. Basic development ProcessWriting of 2.1 code

For the transplant project, the basic code is done under Windows, just need to code to the Linux , and then under Linux to organize the source directory. For uploading to Linux, you can use the VI tool under Linux, or you can open the file under Linux by UltraEdit to edit it. VI is one of the most commonly used text editors for Linux, and some basic uses of VI are described later.

It is worth noting that the carriage return character in thetext under Windows contains two characters '\ n ' and '\ R ', and the carriage return in the text under Linux contains only a single character '\ n '. This way, if you do not choose the correct way to upload the file, you should use the text to upload the use of binary mode, or should use the binary method of uploading the use of text, then there will be problems in Linux, the opening of the text in each line of the end of the line will appear a '^ M ' character. It can be solved by the matching and replacing function of VI (introduced later) or in the correct way.

2.1.1 vi Introduction

VI is the most commonly used under Linux a text editor, small and powerful, once we take it out alone to do the introduction. For more detailed information, perform man vi to view its online Help documentation.

2.1.2 Command mode and edit mode

VI's working mode includes both command mode and edit mode. Command mode can execute some of the commands defined in VI, which correspond to specific keys, and all keyboards in command mode will be interpreted as commands. Edit mode is the mode of editing the document, in the editing mode, all the keyboard corresponding to the content entered as a document. The ESC key allows you to switch from edit mode to command mode. With some edit naming, you can enter edit mode from the command mode.

2.1.3 basic Commands

The basic command refers to commands that are executed by keystrokes in command mode, where we introduce several commonly used commands:

I insert at current start, enter edit mode

I begin inserting at the beginning of the line, entering edit mode

A is appended to the current character and enters edit mode

A is appended at the end of the line and enters edit mode

X Delete the character at the current cursor

X Delete the characters in front of the current cursor

D Delete all characters from the cursor position to the end of the line

DD Delete When moving forward

DW Delete the current word

U Cancel the operation just now

G jump to end of file

In addition, in command mode, the input '/' can be queried by entering ': ' to enter some other commands. Input ': ' commands that can be entered include:

Q exit

W Save

Wq Save exit

Q! force quit, do not save

W! force write on protected files, including read-only files

Set Number Displays line numbers

Number jumps to a row

2.1.4 find and Match

VI's search function is also very powerful, command mode through the input '/' can enter the search mode, you can enter the keyword to find. You can then use 'n ' to find the next place.

After entering ': ', you can also enter some command that matches the substitution--'%s '. The format of the command is ":%s/str1/str2", which will replace all str1 in the current file with str2. To give a typical example, as we said earlier,if the text file under Windows is passed to Linux in binary way , then VI opens with a "^m" at the end of each line, we can open this file with VI, these "^m" are then removed by the match-and-replace function of VI. The command format is as follows:

:%s/^m//

where "^m" is entered by Ctrl + V and ctrl+m.

2.2 compiling2.2.1 Simple compilation

For simple programs, such as only a few source files, you can directly use the compiler to compile, or to write a few compile commands in a script file, by executing a script file to implement the project compilation and connection. For example, a project with only one hello.cpp file can be compiled with the following command:

Cc–o Hello Hello.cpp

Where cc is the compiler, there may be different compilers under different systems. Generally speaking, the C compiler under most Linux systems is called cc, while the C + + compiler is called cc. the C compiler under Linux is GCC, and the C+ + compiler is g++. The- o parameter is used to specify the name of the target of the output, which is the name of the compiled execution program. In this case, the compilation and the connection are completed in one step.

For a slightly responsible program that contains multiple source files, you can write a compilation script that is equivalent to batch processing under Windows. as follows:

Project contains hello.cpp,func.cpp,other.cpp, we can use the following script to achieve the project compile.

Cc–c Hello.cpp

Cc–c Func.cpp

Cc–c Other.cpp

Cc–o Hello hello.o func.o other.o

In the case of multiple files, the compilation and the connection are executed separately, the source files are compiled one after the other, and then linked to form the final executable program. the-c parameter declares that only the compilation operation is done.

2.2.2 Using Makefile

When the project reaches a certain scale, thepractice in 2.2.1 is clearly not sufficient, if not to do so, will bring a lot of work, but also very prone to error. This is where we are going to use makefile to help us with the work of compiling the project.

The makefile file is equivalent to a project file that describes the source code in the project, the additional required library files and their paths, the additional header file paths that are required by the compiler type, compilation parameters, and so on. Use the make command to transfer into makefile for project compilation. When the make command is executed, a file named "Makefile" or "Makefile" is searched under the current directory, eitheras the currently compiled project file or as a specified other project file, such as make–f mymakefile.

Go Research on C + + development under Linux

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.