Unix/linux the most basic knowledge of C + + programming _ block Chain

Source: Internet
Author: User
Tags memory usage

1. Compile. for C + +, usually file independent compilation--generates a standalone intermediate target file. This is mainly a semantic grammar check, the correctness of the Declaration of functions and variables. This requires the introduction of the appropriate header file for the compiler to see the definition. The compiled generated files are. obj files under Windows, and. o Files under UNIX.

2. Connection. when connecting, it is mainly connected functions and global variables, address replacement and relocation. Only focus on the target file when connecting, ignoring the source file. If the function is not declared, it is usually just a warning and can generate the target file. However, an error occurs if the type is undefined (the compiler cannot determine the type size).

3. library files. Each text file gets an. o file. We must connect all. o files when we use them. This is clearly not a clever approach. What to do. We can pack a lot of. o files into a library file. For example, standard C library, standard C + + library and so on. The packaged library file is a. lib file under Windows, and a. a file under UNIX.

//-4,5,6,7,8 is an overview of makefile:http://www.linuxsir.org/main/doc/gnumake/GNUmake_v3.80-zh_CN_html/ Index.html#content

4.makefile of writing
Makefile is performed by the Make interpreter, the primary rule of makefile is exceptionally simple, the target and the prerequisites are distinguished by a colon, and the next line is the command to be executed when the prerequisites are met:
Target:condition1 ... conditionn
CommandLine
An instance:
MAIN.O:MAIN.C Main.h
Gcc-g-O main main.c
If any of the files in MAIN.C or main.h are newer than MAIN.O, perform the recompilation of the following command.
Another example:
Clean: (No conditions, clean is not a file, just an action name, no prerequisite means unconditional execution)
Rm-f MAIN.O
If we enter make clean then execute rm-f MAIN.O

Working process of 5.make
--Look for Makefile and makefile files in the current directory.
--Explain the execution of makefile files, and makefile will recursively rely on their dependencies (like a stack process) for the object files on which they are dependent.
--The first target file, when it is the ultimate goal, automatically executes, it is the root of the tree. If some of the rules are not in this tree, the rule

Will not be performed automatically. such as the clean action of the example above (these actions can show the call)

6. Write a good makefile file
Writing makefile files doesn't mean you can write a beautiful makefile file.
Makefile can declare variables (equivalent to macros): Name= ... Reference $ (NAME)
The purpose of using variables is to make makefile clear and easy to maintain.

7.make Automatic derivation function
For example, see MAIN.O automatically add main.c to its dependencies list. and Gcc-o MAIN.O Main.c was deduced.
This is the "obscure rule", where vague rules and pseudo object files are not discussed in detail (. Phony said).

8. About "Programming accomplishment": write a clean pseudo target for each makefile file. The general style is:
Clean
RM Object1. $ (objects)
A more robust approach would be to:
. Phony:clean
Clean:
-RM Object1 $ (objects)//front Plus-the reason is: this way if an error occurs during execution, ignore the error and continue to follow.

//-9,10 is an overview of gcc/g++

9.gcc/g++ Introduction : GCC and g++ are GNU C & C + + compilers, respectively
gcc/g++ It takes 4 steps to perform the compile work
1. Preprocessing, generating. i file--. I file
2. The preprocessed file will not be converted into assembly language, generating files. s--. s file
3. Files that have been compiled into object code (machine code) generate. o File--. o
4. Connect target code, generate executable program

10.[Parameters Detailed]
-C
Only activate preprocessing, compilation, and assembly, that is, he only makes the program into obj files
-S
Activating preprocessing and compiling only means compiling the file into assembly code.
-E
Activate preprocessing only, this does not generate a file, you need to redirect it to an output file
--You can try gcc-e hello.c >hello.txt or gcc-e hello.c | More
-O
Set the target name, by default, GCC compiled the file is a.out, it is difficult to listen, if you and I feel the same, get rid of it, haha
-ansi
Turn off the features in GNU C that are incompatible with ANSI C and activate the proprietary features of ANSI C (including the prohibition of some ASM inline typeof keywords to

and Unix,vax and other preprocessing macros,
-wall
Display warning messages
-fno-asm
This option implements part of the functionality of the ANSI option, which prohibits asm,inline and typeof as keywords.
-o0
-o1
-o2
-o3
4 levels of the compiler's tuning options,-o0 means no optimizations,-o1 defaults,-o3 highest level of optimization
-G
Just the compiler, when compiling, produces debugging information.
-ggdb
This option will generate the debug information that GDB can use as much as possible.
-ldir
Specifies the path to the library when compiling. such as your own library, you can use it to create a directory, otherwise the compiler will only be in the standard library directory

Find. This dir is the name of the directory.

-llibrary
Specify the libraries to use when compiling

//--11,12 is a general overview of GDB

11.gdb Introduction : Linux contains a GNU debugger called GDB. GDB is a powerful debugger for debugging C and C + + programs. It allows you to observe the internal structure and memory usage of the program while the program is running. Here are some of the features GDB provides:
It allows you to monitor the value of variables in your program. --monitor memory and registers
It allows you to set breakpoints so that the program stops executing on the specified line of code. --Breakpoint debugging
It allows you to execute your code in a row. --Single Step debugging

12.gdb Basic Command :
File loads the executable file you want to debug.
Kill terminates the program that is being debugged.
List lists part of the source code that generated the execution file. You can add n to the back position
Next executes a line of source code but does not go inside the function.
Step executes a line of source code and goes inside the function.
Run executes the program that is currently being debugged
Quit Terminate GDB
Watch allows you to monitor the value of a variable regardless of when it is changed. You must first allow the breakpoint to enter
The break sets a breakpoint in the code, which causes the program to be suspended when it is executed here.
Make enables you to recreate the executable file without exiting GdB.
The shell allows you to execute UNIX shell commands without leaving GDB. Exit out of Shell mode


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.