From windows to * nix (6) Program Development

Source: Internet
Author: User

 

Outline

 

Integrated Environment

Compiler (gcc)

Debugger (gdb, printf)

Dependency Management (make)

Search code (grep)

Code Editor (vim)

Code redirection (ctag, CSAG)

Taglist)

Auto-completion (^ N, ^ x ^ o)

Compilation error/code correspondence (make + vim)

Programmer Manual (man)

 

 

Starting from this chapter, we will begin to talk about the issues that everyone is most concerned about, that is, how to write programs under * nix?

The following uses C/C ++, which is recognized as the most difficult to develop, as an example.

 

In windows, a preset integration environment is usually used. For example, in Visual Studio and * nix, you also need an integrated environment. The difference is that when you use this integrated environment for the first time, you need to do more work. The root cause is that each component in the integration environment is developed by a different person or organization.

 

Compiler

 

Whether it is windows or * nix, the compiler is basically a collection of command line tools, which is very uniform. If you have modified the compilation options through the GUI in windows, you can easily understand this point, although the command line options of each tool are different. In general, the most commonly used compiler in * nix is gcc.

 

Dependency Management

 

Programmers in Windows may have never touched this concept, but you are using it every day. Think about it. Every time you press compile, only the modified source files will be re-compiled. In addition, you press compile, but execute several compilation commands, including preprocessing, compilation, and linking.

 

In * nix, make is responsible for this process. Make parses a file named makefile, which describes the target, dependency, and action. Whenever make finds that the dependency is new to the target, it calls an action to generate the target.

 

Early versions of Visual Studio, such as vc6, also support the make method to generate programs. You can export makefile from the project file, but I don't know if it has been changed.

 

Makefile has a benefit. Compared with a GUI program to manage a project file, makefile has a complete syntax that can be manually modified by programmers to Expand functions.

 

The basic structure of makefile is as follows:

# Variable <br/> var1 = "value" <br/> var2 = $ (var1) <br/> ...... <br/> # You can specify a target and generate the dependency of the target. The following is the executed action <br/> target1: dep. c dep. h <br/> $ (CC) dep. c-o target1 </p> <p> # more targets and dependencies <br/> target2 :......

 

When the make command is used, a target name is added after the make command. If the target name is omitted, the first target is used by default. In makefile, in addition to variables, you can also use predefined variables. For example, $ (CC) is a predefined variable whose value is the default C compiler name in the system. In addition, you can specify the variable value in the make command line.

The following are some examples:

$ Make # Build the default target, that is, the first target <br/> $ make target2 # Build target2 <br/> $ make CC = gcc # specify the variable CC value as gcc for building <br/> $ make-f linux. mk # use linux. mk as Makefile

 

Makefile can use any name as the target, but there are some fixed target names in the Conventions:

All performs the default action. If the target name is omitted

Install

Clean

 

For more details, see the GNU make manual. If you just look for a few open-source makefiles, you will know how tedious it is to write a Makefile. To avoid meaningless repetitive work, or increase the memory burden. You can find some general Makefile templates to deal with some simple projects. I recommend that you give up handwriting Makefile and use the program to generate the Makefile, that is, to learn how to use autoconf and automake, next chapter.

 

Encoding Process 

 

Vim still needs to be learned. The following describes some common skills in the form of FAQs:

 

1. My vim does not have the function you mentioned.
Please compile a full-featured high version vim from the source code. Some systems provide a castrated version of vim. Enter vim -- version in the command line to view the current version.

 

2. vimrc configuration is troublesome
In ~ /. Write a row in the vimrc File
So $ VIMRUNTIME/vimrc_example.vim
This is a preset configuration file that generally meets the requirements of most people.

 

3. How to find in the file
Method 1:/foo <press enter>. If there is a result, n is next to N.
Method 2: If the cursor is on top of foo, press * to directly search For foo.

 

4. How to find multiple files
Method 1: grep foo *-R
Method 2: vim foo **/*. [ch]

 

5. How to jump to a function or structure definition
Install ctags and bind the command to generate the tag file to a hotkey in vim. Next, you need to press the hot key for the first time, then move the cursor to foo, press Ctrl +] to jump to the past, then you can try Ctrl + I and Ctrl + o, one forward and one backward

 

6. I want to see a list of functions and class members.
Install taglist. This is a vim plug-in.

 

7. When the above taglist is used, there is an extra window. I will not switch between windows.
Ctrl + ww switch window
ZZ close current window

 

8. How to edit multiple files
: E. Then you will know what's going on.
: Bn when you have edited more than one file, the next file
: Bp File
When too many files are edited at the same time, install Buffer Explorer, which is a vim plug-in.

 

9. How to easily compile and find errors
Input: make and
: Next cn Error
: Cp previous error
You can also use Ctrl + ww to switch to the error window for free browsing.

 

10. I want to temporarily leave vim to do something.
Please use Ctrl + z, you will return to shell
Fg, you will return to vim again
Don't shut down vim, which wastes a lot of time.

 

11. I know windows is used up, but I still want to ask how to automatically complete it.
If you want to try it out, press Ctrl + n when entering one half of the word. You can also check vim + OMNI online for details.
But I have used vim for such a long period of time, and I have never used it to complete it.

 

12. I want to see the prototype of the library function.
If you know what the function is, you can use man. For example, in vim, You can position the cursor on printf, press K, or return to shell and input man printf. In particular, for functions like stat, the stat command is run by man. You need to enter man 2 stat.
If you don't know what the function is, google
Compared with google, man has two advantages: man is faster when you know what it is. Some library functions have many versions, and man's information is closer to your current system.

 

 

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.