GCC compilation, gdb debug, makefile notation

Source: Internet
Author: User

TEST.C:

#include <stdio.h>

int main (void)

{

printf ("Hello world!");

return 0;

}

======================================

One

1. Compilation process: preprocessing (processing)-"compiling (Compilation)-" compilation (assembly)->linking

2. Pretreatment:

GCC-E Test.c-o test.i/gcc-e test.c

The result of preprocessing is inserting the contents of the stdio.h file into the test.c

3. Compile as assembly code:

Gcc-s Test.i-o Test.s

The-S option indicates that the assembly code is generated and-o represents the output assembly code file.

4. Compilation:

Gcc-c Test.s-o TEST.O

Assembler compiles assembly code into a target file

5. Links:

GCC Test.o-o Test

Two

1. Compile Multiple files:

GCC test1.c test2.c-o Test

2. Error detection

Gcc-pedantic Illcode.c-o Illcode

-pedantic helps programmers find code that does not conform to ANSI/ISO standards.

-wall enables GCC to generate as many warning signals as possible

-werror will stop compiling at the place of warning, forcing the programmer to modify the code

Third, the library file links:

A function library is a collection of header files (. h) and library files (. So,. lib,. dll). Linux defaults to the header file in the/usr/include/, the library file is placed in the/usr/lib/, if we want to use the library is not in these directories, so when the GCC compilation must use its own method to find the required header files and library files.

Example: test.c link MySQL, we want to download MySQL library--mysql connectors, down later by an include folder, which contains the header file, there is a Lib folder, It contains the binary so file libmysqlclient.so, where the path to include is/usr/dev/mysql/include, and the Lib folder is/usr/dev/mysql/lib

1. Compile into the target file:

Gcc-c-i/usr/dev/mysql/include Test.c-o TEST.O

2. Link: Finally, we link all the target files to executable files.

Gcc-l/usr/dev/mysql/lib-lmysqlclient test.o-o Test

The dynamic link library under Linux ends with so, and the static link library is terminated by a.

3. Use static link libraries when linking:

Add-static

Gcc-l/usr/dev/mysql/lib-static-lmysqlclient test.o-o Test

Static Library Search Order

(1). LD is going to find the GCC command, parameter-L.

(2) Re-find GCC environment variable Library_path

(3) Find the default directory/lib,/usr/lib,/usr/local/lib

Dynamic Link Search Order:

(1) The search path specified when compiling the target code

(2) environment variable Ld_library_path

(3) configuration file/etc/ld.so.con

(4)/lib,/usr/lib

GDB Debugging:

1. Gcc-g Main.c-o Main

When using GCC, plus-G indicates that source code information is added to the generated target file for debugging

2. L, List source code starting from the first line

3. Start, execute the program, break the first line

4. N, Next line

5. S, Step entry function

6. BT, backtrace view function stack

7. I locals, use the info command to view local variables

8. F 1, to stack frame 1

9. P Sum, print the value of sum

Finish, run to the return point--if it's a function coming in from S

Set Var sum=0, assigning a value to the variable sum during debugging

P result[2]=33, using p to assign values

Display sum, which displays the value of sum every time the program stops

B. The current loop of break

b 9, set breakpoints on line 9th

C. Continuous operation of continue

Breakpoints. I, see all breakpoints

Delete Breakpoint 2, remove breakpoint 2

Disable breakpoint 2, disabling breakpoint 2

Enable breakpoint 2, enabling

Break 9 If sum!=0, breakpoint activated when condition is met

R, run from the beginning

Makefile Basics:

1. Makefile is made up of a set of rule information, each of which is as follows:

Target ...: prerequistites ...

<tab>command1

<tab>command2

Cases:

MAIN:MAIN.O STACK.O MAZE.O

GCC main.o stack.o Maze.o-o Main

As soon as there is a prerequisities update, the target will be updated, that is, execute command

2. Clean rules

Used to clear the binaries generated by the compilation, preserving the source files:

Clean

@echo "Cleaning Project"

-RM Main *.O

@echo "Clean completed"

If the command does not display the command itself, only the result is displayed. If plus-indicates that the command will not stop even if it is faulted. Usually RM or mkdir is preceded by a-, because there may not be this file, or you already have the file.

If there is a file name called clean, then an error occurs, then add a row to declare the Clean keyword as a pseudo target

. Phony:clean

3.4 Rule keywords:

Install: Copy executable files, configuration files, docs to different installation directories

All: Performs major compilation work, typically used as the default target

Clean: Delete compiled generated binaries

Distclean: Not only delete the binaries, but also delete the other, leaving only the source files.

4. Implied rules and pattern rules:

One of them has a

%.O:%.c

$ (compile.c) $ (output_option) $<

[Email protected] is the target in the rule, $< is the first condition in the rule, the above sentence is equivalent to cc-c [email protected] $<

The equivalent of all such dependencies:

Main.o:main.h statk.h maze.h

Can suppress

Main.o:main.c

Cc-c o main.o main.c

5. Variables:

Main.o:main.c

$ (CC) $ (CFLAGS) $ (cppflags)-C $<

CC = gcc

CFLAGS =-o-g

Cppflags =-iinclude

: =, immediately assign value

? =, if no value is assigned

+ =, append assignment

$^, which represents a list of all the conditions

$?, which represents all the lists that are made up of new criteria than the target

6. Automatically handle dependencies:

Cases:

All:main

MAIN:MAIN.O STACK.O MAZE.O

GCC $^-o [email protected]

Clean

-RM Main *.O

. Phony:clean

Sources = Main.c stack.c maze.c

Include $ (souces:. C =. D)

%.D:%.c

SET-E; rm-f [Email protected];\

$ (CC)-mm $ (cppflags) $< > [Email protected]$$$$;\ # $ $ $ equals two $, which indicates a process

.... Slightly...

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.