Common gcc/makefile/gdb commands

Source: Internet
Author: User

Common commands

Indent automatically adjusts the indent format of C code. By default, the GNU style is used.

GCC compiler: source file (c) ----------- preprocessing ------> preprocessing output file (. i) ------- compile -----> compile a file (. s) --------- compile ------> target file (. o) ---------- connect --------> executable file (. out)

Gcc-s hello. c output hello. s

Gcc-C hello. c B. C output hello. o B. O

GCC hello. c Outputs A. Out by default

Gcc-O hello. c output hello. Out

"Database file" during connection"

Standard library file Linux system location:/lib // usr/lib/

Standard header file location:/usr/inclue/

However, by default, GCC only searches for the Standard C language library/Standard C header file, and only the library files are placed in the standard directory in different rows. -L dir must be added to the header file, and-l dir must be added to the library file (if-L. Search is used to add the current directory)

Static and shared function libraries:

Call a function in the static function library, first find the header file that defines the function, and then find the corresponding function from the library file search path, only add the used function to the generated executable file.

The sharing method only contains one function reference. The advantage is that a copy is saved throughout the memory and the file/usr/lib/libm. so is the revision of the actual library file (/usr/lib/libm. so. n, N is the version number), so that its updates are independent of the application that uses it. In addition, when Linux starts an application, it will consider the version of the function library required by the application to prevent the new main version of the function library from making the old application unavailable.

For Linux systems, the program (Dynamic loader) responsible for loading the shared library and parsing the function application of the customer program is lD. so, may also be a ld-linux.so.2/ld-linux.so.1. you can search other shared library locations in the file/etc/lD. so. conf. If this file is modified, use the ldconfig command. You can run the LDD tool to view the shared libraries required by the program.

The shared library is similar to the dynamic link library used in Windows in many aspects .. The so library corresponds to the. dll file and is loaded at runtime, while the. A library is similar to the. Lib file and is included in the executable file.

The following is an example of generating your own library file.:

Pro1.c contains function pro1

Pro2.c contains the function pro2

Lib. h header file, including the declaration of function pro1 and function pro2

Use ar CRV libfoo. A pro1.o pro2.o can generate the static library file libfoo. a. if the library file is placed in the current directory, when another program includes the header file Lib. h, the connection command is

Gcc-O pro. O-L. libfoo. A (-L indicates that the path of the Search Library file is added to the current path)

NM commandIt can be used to wipe the sweat and view the functions contained in the target file/function library/executable file.

Make tool a MAKEFILE file has a series of make rules, the format of each make rule is: Target: dependency file list

<Tab> command list

Target: The final file to be created by make.

Dependency: other files required for compiling the target file, such as. C. H. O.

Command: The action executed by make. Each Command occupies one line. You can use "\" to wrap the line.

A makefile file:

test: prog.o code.o       gcc -o test prog.o code.oprog.o: prog.c prog.h code.h        gcc -c prog.c -o prog.ocode.o: code.c code.h        gcc -c code.c -o code.oclean:         rm -f *.o

The timestamp check process executed by make:

Make determines whether to execute a Target Based on the timestamp of the file that a target depends on.

If you enter make in the command line, the first target test is executed by default. Test depends on Prog. O Code. o, make will check whether there is any Prog. O target with the same name. If yes, check the target Prog. O dependency timestamp to determine whether to execute target Prog. o; if not, check the timestamp of these dependent files directly. Such a recursive process.

Variables in make

  • Custom variable: Define OBJ = PRG. O Code. O to use $ {OBJ}
  • Environment variable: used directly
  • Predefined Variables
  • Automatic Variable


Predefined Variables Description
$ @ The name of the current target file. For example, when used to create a library function, its value is the library file name.
$? List of new dependent files (that is, those files on which the current target file depends) compared to the current target file
$ < The first dependent file newer than the current file
$ ^ All dependent files separated by spaces (there is only one repeated file name, if any)

GNU debugging tool GDB

Set the breakpoint break <row number> Break <Function Name> multi-file break <file name: row number> Break <Function Name> condition break <row number/Function Name> If <condition expression>

STEP (entering the function)/next (not entering the function) in one step)

View Source Code: by default, 10 lines near the current row are displayed in list [M, N].

View help document: Help

Variable in the monitor: Print whatis

Set the variable value: Set

Two common C errors GDB debugging practice: http://www.diybl.com/course/6_system/linux/Linuxjs/200875/130421.html

Http://blog.chinaunix.net/space.php? Uid = 317451 & Do = Blog & id = 92412

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.