C + + programming in the "linux&c++" Linux Environment

Source: Internet
Author: User

In the process of reading any questions, welcome to communicate together

Email:[email protected]


Under Linux, development tools are cut into separate gadgets. Each deal with a different problem. For example:

editor (Emacs, vim)
used to debug programs
compiler (GCC) used to compile and link the program's
performance analysis tool (Gcov, gprof)
document Builder (Doxygen)

used to generate the document

At the same time, there are some system tools and system knowledge, we are very necessary to understand:
Makefile Program Automation mechanism
Shell System Adhesives
grep, locate, find System Finder Tool
Other tools (e.g. Ctags, MPC of OCI, etc.)

Take a C + + source file as an example:


#include <stdio.h>

int main (int args,char **argv)


printf ("Hello world!");

return 0;




1. Compile process:. C --preprocessing (processing)---I --compile (compilation) ----Assembly (assembly)->o< /c3>--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, and-O represents the output assembly code file.

3. Compile as assembly code:

Gcc-s Test.i-o Test.s

The-S option indicates that the assembly code is generated

4. Compilation:

Gcc-c Test.s-o TEST.O

Assembler compiles assembly code into a target file

5. Links:

GCC Test.o-o Test


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:


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


GDB Debugging:

1. Gcc-g Main.c-o Main

When using GCC, the plus-G means that the source code information is added to the generated target file for debugging. Without-G, you will not see the program's function name, variable name, instead of the memory address of the runtime. When you use
-G After the debugging information is added and the target code is successfully compiled, GDB can be used to debug it.

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


Compile Build Execution file: (under Linux)
hchen/test> cc-g Tst.c-o TST
Debugging with GDB:
hchen/test> gdb TST <----------start GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU general public License, and you are
Welcome to change it and/or distribute copies of it under certain.
Type "Show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "Show warranty" for details.
This GDB is configured as "I386-suse-linux" ...
(GDB) L <--------------------l command is equivalent to list, starting from the first line of the original code.
1 #include
3 int func (int n)
4 {
5 int sum=0,i;
6 for (i=0; I 7 {
8 sum+=i;
Sum of ten return;
(GDB) <--------------------direct return, repeat the last command

Main ()
15 {
int i;
A long result = 0;
for (I=1; i<=100; i++)
19 {
result + = i;
(GDB) Break <--------------------set breakpoints at line 16th of the source program.
Breakpoint 1 at 0x8048496:file tst.c, line 16.
(GDB) Break Func <--------------------set breakpoints at the entrance of function func ().
Breakpoint 2 at 0x8048456:file tst.c, line 5.
(GDB) Info Break <--------------------view breakpoint information.
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x08048496 in main at tst.c:16
2 Breakpoint Keep Y 0x08048456 in func at Tst.c:5
(GDB) R <---------------------running program, shorthand for Run command
Starting program:/HOME/HCHEN/TEST/TST
Breakpoint 1, Main () at Tst.c:17 <----------stop at the breakpoint.
A long result = 0;
(GDB) n <---------------------single statement execution, next command shorthand.
for (I=1; i<=100; i++)
(GDB) n
result + = i;
(GDB) n
for (I=1; i<=100; i++)
(GDB) n
result + = i;
(GDB) C <---------------------continue to run the program, continue command shorthand.
RESULT[1-100] = 5050 <----------program output.
Breakpoint 2, func (n=250) at Tst.c:5
5 int sum=0,i;
(GDB) n
6 for (i=1; i<=n; i++)
(GDB) P i <---------------------print the value of the variable i, the Print command shorthand.
$ = 134513808
(GDB) n
8 sum+=i;
(GDB) n
6 for (i=1; i<=n; i++)

(GDB) P sum
$ = 1
(GDB) n
8 sum+=i;
(GDB) n
6 for (i=1; i<=n; i++)
(GDB) P sum
$4 = 3
(GDB) bt <---------------------view the function stack.
#0 func (n=250) at Tst.c:5
#1 0x080484e4 in Main () at tst.c:24
#2 0x400409ed in __libc_start_main () from/lib/libc.so.6
(GDB) Finish <---------------------Exit function.
Run till exit from #0 func (n=250) at Tst.c:5
0x080484e4 in Main () at tst.c:24
printf ("result[1-250] =%d/n", func (250));
Value returned is $6 = 31375
(GDB) C <---------------------continue to run.
RESULT[1-250] = 31375 <----------program output.
Program exited with code 027. <--------program exit, Debug end.
(GDB) Q <---------------------exit gdb.

Makefile Basics:

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

Target ...: prerequistites ...





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:


@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


$ (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


Cc-c o main.o main.c

5. Variables:


$ (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:




GCC $^-o [email protected]


-RM Main *.O

. Phony:clean

Sources = Main.c stack.c maze.c

Include $ (souces:. C =. D)


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

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

.... Slightly...

C + + programming in the "linux&c++" Linux Environment

Related Article

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.