Linux programming

zhaozj2021-02-16  51

Linux Programming GNU is a world of dreams. Linux is a very magical operating system. The system should be open, the software should be free to use. The freedom of beliefs stimulates the passion of countless people, struggles for free software. However, due to the rule of Windows Too long, we are still very strange to free software. Lack of application software support, lack of users. But all this can't stop the development of the free software business. Let us join hands, for free software alliance Do your best!

The program is a programmer communication. You can write a program in Linux, an effort for free software is a wish for every Linux user. The purpose of this tutorial is also here. Here is some of my assumption: 1. Readers are familiar with C language; 2. The reader has installed a Linux operating system and is already running; 3. All the development environment of this tutorial is completed in the character interface, and it is very simple, just a gate tutorial; let us let's start!

The use of VI is one of the most classic text editors under UNIX / Linux. LINUX users must be familiar with it. Vi filename // Open / create a file (if the file does not exist), enter Vi The interface is the interface. The key to understanding the VI is to understand two modes of VI: edit mode and command mode. At the beginning, it is command mode. In command mode, you can use some commands, such as saving, exit, etc. In editing mode You can edit, modify the text ... At any time, press the ESC button to enter the command mode. In command mode, press A (Append), i (insert) to edit mode. General use i. In command mode, press ':', then enter WQ (write and quit), you can save the disk exit. If you don't want to save the disk, then enter Q! (Forcrate exit). In command mode, direct Shift zz can also be exited directly, and save the incoming trouble. Below is a complete example of a complete C language code. Vi hello.c // Command mode, press the I key to enter the edit mode // Enter a test code // Press ESC to enter the two ways of the command mode // store: A: Enter: WQ B: SHIFT ZZ // Way (this editing modification content invalid) Enter: Q!

GCC use (?) GCC is a GNU's fist product. The powerful C language compiler in the world. If you are familiar with the command line compilation, this tool is ideal for you. One version is also available under Windows. In the introduction GCC Before using, let's review the compilation process of C language: Hello.c -> Hello.o (connection) -> Hello // Linux Hello.c (compilation) -> Hello.obj (connection) -> Hello.exe // Windows In the previous example, I already had a C language source program, Hello.c, let's compile it into executable GCC -O Hello.c // Result: We get one The executable file name named by the default name A.out GCC Hello.c -c Hello.o // -c option represents compilation, the result is the target code hello.o gcc hello.o -o hello // -o option, The result is an executable file Hello (unreasonable) GCC hello.c -o hello // General Usage, one step, directly get the executable file Linux, the executable file and system command in the current directory are strictly distinguished. You want to execute a system command, such as copying command CP, entering a CP bus directly; if you want to run an executable file hello in the current directory, you need to enter ./hello, talented. This is also Linux security measures One.

The use of GDB (Debug) GDB is a commonly used commissioning tool next to Linux. To use it to debug the program, pay attention to the option -g, gcc -g hello.c -o hello when compiling The debugging information is added in the executable, and the usual file will become great. Below is its usage: gdb hello // debug Hello GDB BREAK Main // Break, set breakpoints, this breakpoint is set in the program At the beginning, the entrance to the main function starts running the program, stopping at the breakpoint, the start of the program starts GDB S // Step, starting a single step debugger, and printing the source code for this program GDB print var // The current value of the variable, the program does not execute forward, var is the variable name GDB R or GDB Q // error excluded, allowing the program to run directly or exit directly. (GDB R will not quit GDB, please enter a q) Makefile (?) Makefile is used for multi-source code file project management. For example, an executable file ABC is connected by AO, BO, CO; and A1 is made from A1.c. Compilation of A2.c, A3.c, BO is compiled by b1.c, b2.c, b3.c, Co is compiled by C1.c, C2.c, C3.c. Sufficient complicated. We need GCC A1.C A2.c A3.c -c AO GCC B1.C b2.c b3.c -c bo gcc c1.c c2.c c3.c -c Co GCC AO BO CO -O ABC If only A source file A1.C changes, we must recompile the other programs that have not changed, do not work, and the example is still small, if it is a project with hundreds of source code, simplifies the compilation process is necessary. Makefile is born. makefile consists of dependencies and compile commands. The written method of dependencies is the target: dependent file 1, dependent file 2, dependent file 3 ... Compile command directly written in the dependencies, Note, starting with a Tab backup button, four spaces do not write a makefile file with the example above. (Makefile's default file name is makefile, no hardened name) Vi Makefile ABC: AO Bo Co GCC AO BO CO -O ABC AO: A1.C A2.C A3.C GCC A1.C A2.C A3.C -C AO bo: b1.c b2.c b3.c gcc b1.c b2.c b3.c -c B.Oc.O: C1.C C2.C C3.C GCC C1.C C2.C C3.c - C CO performs Makefile, the system will determine the final modification time of the source file, and which do not need to be recompiled.

转载请注明原文地址:https://www.9cbs.com/read-22045.html

New Post(0)