Learn about Linux
Write
User-Layer Program
I found that some students often spend a long time learning how to use a compilation tool when learning a language.
Real
It's quite confusing.
. There are not only many compilation tools, but also continuous elimination. We know that the C compilation tools are (turboc, Vc, Vim, ADS .......) if we spend a lot of time mastering them, it would be a great tragedy. It is vital that we spend a lot of time learning a language. I hope you only have a rough understanding of the tool. You can check the information if you have any questions when using these tools.
I. How to write programs in Linux:
1. Generally, VIM and VI are the most commonly used programming in Linux. So how to write C Programs in Vim.
Let's take the simplest example and look at the process of compiling C Programs in vim-print Hello World on the terminal using C Programs.
Step 1: Enter the command: Vim file name. C on the terminal
Example: Vim hello. c
(Hello. C is a source program name for programming)
Step 2: After Entering Vim hello. C, we enter the programming interface. Now we can write our own C program. First, press "I" or "A" on the keyboard to enter characters.
# Include <stdio. h>
Int main ()
{
Printf ("Hello World/N ");
Return 0;
}
Step 3: The program can be saved after it is written. How can we save the program in Vim?
First press the <ese> key to enter the naming mode, then press ":", and then use the following command
Generally, there are three commands: WQ, Q, and Q! (Note that all lowercase letters are used here)
A. WQ: Save and exit.
B Q: Do not save and exit
C Q! : Force. Exit.
So we use the command: WQ
To save the program and exit
Step 4: Compile and link the program after everything is done.
In Linux, we usually use GCC to compile the program (Note: GCC can only compile user-layer programs, not kernel-state programs, links)
GCC file name. C-o file name 1 (Note: File 1 indicates the name of the compiled executable file. However, the name of the source file is usually the same)
Function example:
GCC hello. C-O hello
After using this command, the system will compile the hello. C link into the executable file hello.
Step 5: How can I execute an executable file?
You can run the command:./to execute the file name (Note:./indicates the current directory)
Function instance:./Hello
After executing this command, you can see the execution result of the program.
Ii. Some common GCC commands during programming
1> objdump-D TIgE. O reverse Merge (objdump indicates disassembly)
2> gcc-C tiger. c generates the tiger. o file
3> gcc-s tiger. c generates the tiger. s file
4> GCC tiger. O-o tiger generates the execution File
Tiger-John note:
A. Here: Tiger is the name of my own program.
B. What do common file suffixes mean;
. C is the original C program.
. I is preprocessed.
. S is the original program of assembly language.
. O is the target file
. A. archive file consisting of the target file
Tiger-John added:
1> during programming, we may sometimes forget the number, order, and header file of the parameters. What should we do?
Someone said, buy Ben L.Books such as inux are available at any time.
In fact, brother tells you that you only need to enter the command on the terminal: Man + the function you want to query. We can see the prototype and header file of this function.
For example, if you enter man read, you can see the prototype and header file of the READ function and detailed information about how to use a function. If you do not believe this, try again.
2> some functions, such as mkdir, are Linux commands and are also called by the system. In this case, we can enter man 2 mkdir to obtain the prototype of the function.
3> for database functions, you can enter man 3 <Database Function Name> to view all its information.