Unix programming tools
Author: Parlante, zelenski, etc.
Copyright 1998-2001, stanfo University
Introduction
This article describes the entire process of UNIX-based programming, including editing, compiling, linking, and debugging. There are few Common Unix programming tools-GCC, make, GDB, emacs, and Unix shell. The goal of this article is to introduce the main features and typical usage of these tools and to demonstrate in detail how to use them together to complete simple projects. We have used a version of this article in stanfo to help students start Unix programming.
Main Content
Introduction -- Compilation-link Process
GCC compiler/connector
Make Functions
Gdb debugger
Emacs editor
Unix shell commands
Http://cslibrary.stanford.edu/
(This is Document #107 in the stanfo Computer Science Teaching database, called Unix programming tools. There are other free teaching materials available at http://cslibrary.stanford.edu. This document can be used for free, copied, or sold as long as it is complete and has not been tampered .)
Other resources
This document is just an introduction to the tool. To obtain more detailed information about a tool, go to the man page of the tool and the entry description in xinfo. In addition, o'reilly & Associates published a series of books on Unix-related tools (there will be an animal on the cover of those books ). If you want to learn the basic C language, please view the computer education library #101 documentation, (http://cslibrary.stanford.edu/101/
).
Compilation process
Before you understand the details of a tool, it is very useful to review the entire process of compiling an executable file. After the source file is edited, build consists of two steps: Compilation and link. Each source file (FOO. c) is compiled into a target file (FOO. O ). Each target file contains a system dependency and is compiled into a program representative, as described in the source file. The file name of a target file is the same as that of the source file, but the extension is changed to ". O "--" Main. C "is compiled into" Main. O ". this. O files will contain references, symbols, methods, variables, and so on required by the program.
Each separate target file can be linked together to generate an executable file that can run on the system. This step also links some library files that contain library methods (such as printf () and malloc. The entire process is as follows:
Part 1 -- gcc
Next we will discuss the GCC compiler, an open-source product of the GNU (www.gnu.org) project. Using GCC has the following benefits: it will always be updated and very reliable. It can run on multiple platforms. Of course, it is free and open-source. GCC can compile C, C ++, and objective-C. GCC is actually a compiler and a linker. For simple problems, a simple call can complete the entire compilation and link operations. For example, for a simple project, you may use the following command to compile three. c files and generate an executable file called "program.
GCC main. c module1.c module2.c-O program
The name of the row above can be equivalent to three separate commands to create a project.
Gcc-C main. C # each of these compiles a. c
Gcc-C module1.c
Gcc-C module2.c
GCC main. O module1.o module2.o-O Program # This line links the. O's to build the program
The General Command to call GCC is:
GCC options files
Option A series of options used to control the compiler, while files is a list of GCC for reading or writing
Command Options
Like most UNIX programs, GCC supports a large number of command options to control its running. They are introduced on the GCC man page. We can safely ignore most of the command options, but we should note the following:-C,-o,-G,-wall,-I, -l and-l.
-C files
Directly use GCC to compile the source file to the target file and ignore the link stage. Makefiles (described below) use this option to compile multiple files at a time.
-O file
Set the GCC output file name to file. If this option is not specified, the default name depends on the situation. If a. c source file is compiled, the output target file will have the same name as the source file, but the extension is. O. If the event link generates an executable file, the output file will be named A. Out. Most of the time, the-O option is only used to set the file name of the output file when the link is used to generate the executable file. during compilation, people only use the default settings.
-G
Indicates that the compiler contains additional debugging information in the output. We recommend that you add this option when compiling your source file, because we encourage you to become more proficient by using a debugger like GDB (as described below.
-Wall
Provides possible errors in the source code. The prompt obtained by using-wall is not a real error. They are only errors that the compiler deems possible. We strongly recommend that you add the-wall command when compiling your code. Errors found during compilation are 10 thousand times simpler than errors found during running. -The wall command is like a nagging aunt, but she deserves to be with you. If a student asks me for a code that cannot be run and the code generates some warnings during compilation using the-wall command, 30% of these warnings are the cause of the problem, 30% may not be, but you should be very happy that it gives you free bug tips.
In some cases, the warning obtained by the-wall command is not a real problem, the code is correct, and the compiler has also passed the compilation. In this case, do not ignore the warnings. Resolve all the warnings generated by the source code. Warning is a bad habit.
The following is a small code snippet that assigns values to variables in a code sentence and determines whether the variables are true or not.
Int flag;
If (flag = isprime (13 )){
...
}
The compiler will give a warning that the assignment may be inadvertently assigned. Although the assignment is correct in this case, this warning will capture a general bug, that is, when you want to input =, you enter =. To solve this warning, rewrite the test code...
Int flag;
If (flag = isprime (13 ))! = 0 ){
...
}
In this way, there will be no warning, and the generated code is the same as the previous one. Alternatively, you can put the entire test code in a braces to demonstrate your intention. This is the value of using-wall to help you find bugs.
-Idir
Add the Dir directory to the search Command of the # include command. The compiler automatically searches for several Standard Directories. You can use this option to add the compiler's search directory. There is no space between the "-I" command and the directory name. If the compilation fails because the # include command cannot find the file, you need to use-I to solve it.
The following describes how to use the "find" command in UNIX to find your # include file. In this example, find all header files containing the "Inet" field in the/user/include directory...
Nick % find/usr/include-name '* Inet *'
/Usr/include/ARPA/inet. h
/Usr/include/netinet
/Usr/include/netinet6
-Lmylib
(L is in lower case) Search for the library named mylib for the unimplemented sign (method or global variable) during the link. The real name of the library should be libmylib. a, and the Library can be found either in the default path of the system, or in the directory added by using the-l command (as mentioned below.
The position of the-l flag in the command list is very important when linking, because the linker does not search for undefined flag in the previously checked library. For example, if you call a library that depends on the math library, it must be linked before it is linked to the math library. Otherwise, a link error is reported. Like the command above, there is no space between the command character and the database name, and this is a lowercase "L", not a number 1, if the link fails because a flag is not found, you need a-l command to add the appropriate library, or you can write the wrong function name or variable name during compilation.
-Ldir
Add dir to the search directory of the library specified by the-l command. Like the command above, there is no space between the command character and the directory name. If a library file is not found during the link, you need to use the-l command to solve the problem, or your library name is wrong.