Basic knowledge of C/C ++ programming in Unix/Linux

Source: Internet
Author: User

1. compile.For C ++, a file is usually compiled independently to generate an independent intermediate target file. At this time, it mainly involves semantic syntax check, and the correctness of the declaration of functions and variables. This requires introducing a suitable header file for the compiler to see the definition. The compiled file is a. OBJ file in windows and a. o file in UNIX.

2. Connection.During connection, connection functions and global variables are used to replace and relocate addresses. Only the target file is concerned during connection, and the source file is ignored. If the function is not declared, it is usually a warning to generate the target file. However, if the type is undefined (the compiler cannot determine the type size), an error occurs.

3. database file.Each text file generates a. o file. All. O files used must be connected during use. This is obviously not a smart approach. What should I do? We can package many. O files into library files. For example, the standard C library and the Standard C ++ library. The packaged library file is a. Lib file in windows and A. A file in 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. Write makefile
Makefile is executed by the make interpreter. The main rules of makefile are very simple. Objective and prerequisite are distinguished by colons. The next line is the command to be executed when the prerequisite is met, as shown below:
Target: condition1... conditionn
CommandLine
One instance:
Main. O: Main. C main. h
Gcc-g-o main. c
If any file in Main. C or main. H is newer than main. O, execute the following command to re-compile.
Another example:
Clean: (no conditions, clean is not a file, but an action name. No prerequisite means unconditional execution)
Rm-F Main. o
If we enter make clean, execute Rm-F Main. O.

5. Make Process
-- Find the makefile and makefile files in the current directory.
-- Explain the execution of makefile. For the target files on which makefile depends, makefile recursively recursion their dependencies (like a stack process ).
-- The first target file is the ultimate goal. When it is automatically executed, it is equivalent to the root of the tree. If some rules are not in this tree

Not automatically executed. For example, the Clean Action in the example above (these actions can be called)

6. Write outstanding makefile files
Writing makefile does not mean writing a beautiful makefile.
Variables (equivalent to macros) in makefile: Name =... reference $ (name)
The purpose of using variables makes makefile clear and easy to maintain.

7. Make's automatic derivation Function
For example, Main. O automatically adds main. C to its dependency list. Gcc-O main. O main. C is derived.
This is the "Hidden Rules". Here we will not discuss the Hidden Rules and pseudo-target files in detail (. Phony ).

8. About "programming Cultivation ":Write a clean pseudo target for each makefile. The general style is:
Clean:
Rm object1... $ (objects)
A more robust approach is:
. Phony: clean
Clean:
-RM object1... $ (objects) // The reason for adding-above is: in this case, if an error occurs during execution, ignore the error and continue the execution.

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

9. Introduction to GCC/g ++: GCC and G ++ are respectively gnu c & C ++ Compilers
Gcc/g ++ requires a total of four steps for compiling.
1. pre-process and generate the. I file --. I file
2. Convert the pre-processed files into assembly languages to generate the. s --. s file.
3. A file named. O is generated by means of compilation into the target code (machine code ).
4. Connect the target code to generate an executable program

10. [Detailed description of parameters]
-C
Only activate preprocessing, compilation, and assembly, that is, he only makes the program into an OBJ file.
-S
Only activating preprocessing and compilation means compiling a file into assembly code.
-E
Only activate preprocessing. 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, the GCC compiled file is a. Out, which is hard to hear. If you feel the same way with me, change it. Haha
-ANSI
Disable the feature incompatible with ansi c in gnu c and activate the proprietary feature of ansi c (including disabling some ASM inline typeof keywords

And UNIX, VAX, and other preprocessing macros,
-Wall
Display warning information
-Fno-ASM
This option is part of the feature that implements the ANSI option. It prohibits the use of ASM, inline, and typeof as keywords.
-O0
-O1
-O2
-O3
The compiler has four levels of optimization options.-O0 indicates no optimization,-O1 indicates the default value, and-O3 indicates the highest optimization level.
-G
Only the compiler generates debugging information during compilation.
-Ggdb
This option will generate debugging information that can be used by GDB as much as possible.
-Ldir
Specifies the path of the database to be searched during compilation. For example, you can use your own library to create a directory. Otherwise, the compiler will only

Find. This DIR is the directory name.

-Llibrary
Specifies the library used for compilation.

// -- 11,12 is an overview of GDB

11. Introduction to GDB: Linux contains a GNU debugging program called GDB. GDB is a powerful debugger used to debug C and C ++ programs. it enables you to observe the internal structure and memory usage of the program while the program is running. the following are some functions provided by GDB:
It enables you to monitor the value of variables in your program. -- monitor memory and registers.
It enables you to set breakpoints so that the program stops running on the specified code line. -- breakpoint debugging
It enables you to execute your code in one row. -- single-step debugging

12. GDB basic commands:
File to load the executable file to be debugged.
Kill to terminate the program being debugged.
List lists a part of the source code that generates the execution file. You can add n to locate it.
Next, execute a line of source code but do not enter the function.
Step: execute a line of source code and enter the function.
Run to execute the program currently being debugged
Quit terminate GDB
Watch enables you to monitor the value of a variable, no matter when it is changed. You must first set the breakpoint to enter
Break sets breakpoints in the code, which will cause the program to be suspended when it is executed here.
Make enables you to re-generate executable files without exiting GDB.
Shell enables you to run the Unix shell command. Exit to exit the shell mode without leaving GDB.

 

 

 

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.