LINUX C language programming

zhaozj2021-02-16  134

?

Foreword Linux contains a lot of software development tools. Many of them are developed for? C? And? C applications.? This article introduced? Linux? Can I use to use? C? Application development And debugging tools. What is the main purpose of this article? How to use? Linux? C? C? Compiler and other? C? Programming tool,? Not? C? Language programming tutorial.? In this article, you will learn The following knowledge:

What is the "CGNU? C? Compiler? GDB? To debug GCC applications, you can also see other useful? C? Programming tools for Linux? Issue. These tools include source program beautifying programs (PRETTY? Print? Programs), the additional debugging tool, the function prototype automatic generation tool (AutomaticFunction? prototypers).

-------------------------------------------------- ------------------------------ Note:? Source Program (PRETTY? Programs) Automatically helps you format the source The code is consistent with a constant indentation format. ----------------------------------------- ---------------------------------------

What is it? C? C? Is a uniX? The earliest programming language is widely used in the early stage of the operating system. Is it the first to be a Bell Lab? Dennis? Ritchie? For? Unix? Auxiliary development ,? Start? Unix? Is it written in assembly language and a call? B? 'S language.? From then on,? C? It becomes the most widely used computer language in the world.

C? There are some reasons why you can get such a wide support in the programming field: it is a very common language. Almost you can think of at least one of the computers available? C compiler And its grammar and function library are unified on different platforms. This feature is very attractive to developers. Use the program written by? C? The execution speed is very fast .c? Is all versions System language on UNIX. C? I have developed a lot in the past 20s. The American National Standards Association (Americannational? Standards? Institute) was released in the late 1980s. ANSI? C? ? C? Language standard. This makes it more guaranteed the consistency of C? C? In different platforms.? In the 1980s, a C-object-oriented expansion called? C .? C ? Another article? "C ? Programming" describes. Linux? Is it available on? C? Compiler is the "GNU? C? Compiler, which is based on the programming license of the Free Software Foundation? Can be released freely. Can you find it on the release of the linux?

?

GNU? C? Compiler? Slackware? Linux? Is the GNU? C? Compiler (GCC) is a full-featured? ANSI? C? Compatible compiler.? If you are familiar with other operating systems or hardware platforms One? C? Compiler, you will be able to master GCC quickly. This section describes how to use? GCC? And some? GCC? Compiler The most common option.

Use? GCC usually uses some options and file names? GCC? Compiler. • The basic usage of the GCC? Command is as follows: GCC? [Options]? [Filenames] command line option specified the action will be given on the command line Execute on the file.? Next Section will narrate some options you will use.

GCC? Options GCC? There are more than 100 compile options available. Many of these options you may never use, but some major options will be used frequently.? Many? GCC? Options include one The above characters. So you must specify your respective hyphens for each option, just like most? Linux? Commands, you can't follow a set of options after a separate hyppen.?,,, Below? The command is different: GCC? -P? -G? Test.c

GCC? -pg? Test.c The first command tells the GCC? Compile? Test.c? When the PROF? command establishes a profile information and adds the debug information to the executable file.? 2 Command only tells the GCC? GPROF? Command to establish a profiling information. When you don't have any options to compile a program, GCC? Will be built (assuming compile success) A executable file named? A.out.?, For example, , The following command will generate a file in the current directory: GCC? Test.c You can use the "-o? Compilation option to replace a file name to specify a file name to generate the executable? A.out., for example, compile a call? count.c? C? program compiled into the executable of the called count ?. You will enter the following command: GCC? -o? count? count. c

-------------------------------------------------- ------------------------------ Note:? When you use? -O? Option,? -O? Be followed A file name. ---------------------------------------------- ----------------------------------

GCC? Also specifies how much compiler processing.? -C? Option tells? GCC? Judging the source code as a target code to skip the assembly and connection steps. • This option is very frequent because it makes compilation Multiple? The speed is faster and easier to manage.? Is the default? GCC? The target code file created is a? .O? The extension.-S? Compile option tells? GCC? C? C? The code has generated the assembly language file and stops compiling. The default extension of the assembly language file generated by the GCC? Is the default extension of the language files? Ss?.? -E? Option indicates that the compiler is only preproces the input file.? When this option is When using, the output of the pre-regulator is sent to the standard output instead of being stored in the file.

Optimization? Choose? When you use? GCC? Compile? C? Code, it will try to complete compilation with the least amount of time and make the compiled code easy to debug.? Easy debugging means compiling code and source The code has the same order,? Compiled code has not been optimized.? There are many options to tell? GCC? GCC? GCC? A more faster executable can be generated on the basis of more compile time and sacrificial procurement. • The most typical -o? And? -O2? Option. -O? Option tells? The GCC? The source code is basically optimized. These optimizations will make the program faster in most cases.? -O2? Option tells? GCC? Generate as small and as fast as possible.? -O2? Option will make compiled speed than use? -O? Is slow. But usually generated code execution speed will be faster.

In addition to? -O? And? -O2? Optimization options? There are some low-level options to generate faster code. These options are very special, • It is best to compile these options when you fully understand these options What effect is produced when the code is generated. • Detailed description of these options, please refer to the "GCC?" Guide page, type MANGCC?

Debug and profiling option GCC? Support several debugging and profiling options. What is most commonly used in these options? -G? And? -Pg? Option. -G? Option tells? GCC? Produce Can Be? GNU • Debugging information used by the debugger to debug your program.? GCC provides a lot of other? C? There is no feature in the compiler ,? In GCC? You can make? -G? And? -O? (Generated Optimize code) Use. This is very useful because you can debug your code as possible with the final product as possible. When you use these two options simultaneously, you must know that some code you wrote is already When optimized? GCC? Changes. • For more information on debug? C? Program, please see the next section "Use? GDB? Debug? C? Program"?-Pg? Option tells? GCC? In you Add additional code in the program, when executed,? GPROF-use profiling information to display your program's time consumption.? About? GPROF? For more information, please refer to? "GPROF"? Section.

Use? GDB? Debug? GCC? Program Linux? Contains a "GDB? GNU? Debugger.? GDB? Is a powerful debugger for debugging? C? And? C ? Program. It makes you Can observe the internal structure and memory usage of the program at runtime.? The following is some of the features provided: it allows you to monitor the value of the variables in your program. It makes you set a breakpoint to make the program Stop execution on the specified code line. It allows you to perform your code in a line. Type it on the command line? GDB? And press Enter keys to run? GDB?,? If everything is normal,? GDB? Will be started and you will see similar content on the screen: GDB? Is? Free? Software? And? You? Are? Welcome? To? Distribute? Copies? Of? IT

Under? Certain? conditions ;? Type? "show? copying"? to? see? the? conditions.

There? Is? Absolutely? No? Warranty? For? GDB ;? Type? "Show? Warranty"? Details.

GDB? 4.14? (I486-slakware-linux),? Copyright? 1995? Free? Software? Foundation, Inc.

(GDB) When you start? GDB? After you can specify a lot of options on the command line. You can also run in the following ways? GDB?: GDB?

When you run in this way? GDB?, Can you specify the program you want to debug.? This will tell GDB? Load name

For? FName?'S executable.? You can also use? GDB? Go check a result of an abnormal termination of the program? Core

Document, or connected to a running program. You can refer to the "GDB? Guide page or type on the command line

GDB? -H? Get a simple list of instructions on these options.

To debug compilation code (Compiling? Code? For? Debugging) To make the GDB? Normal work, you must make your program contain debugging information when compiling.? Debug information contains the type of each variable in your program and The address mapping in the executable file and the line number of the source code.? GDB? Use this information to associate the source code and the machine code. Use the "-g? Option to open the debug option when compiling.

GDB? Basic command GDB? Support a lot of commands to achieve different features. These commands are loaded from simple files to the complex command that allows you to check the content called the stack content, list 27.1 List you are using? GDB? Some commands that will be used when debugging.? Want to know? GDB? For details, please refer to the guide page.

Table? 27.1.? Basic? GDB? Command.

Life order? Let's describe the executable .Kill? Terminate the program that is debugging. List? List part of the source code generated by the execution file. NexT? Perform a row source code but do not enter the function Internal. Step? Perform a row of source code and enter the function inside. Run? Execute the current debugging program quit? Terminal? GDBWATCH? Make you monitor the value of a variable, regardless of whether it is changed. BREAK? Set break in the code Point, this will hang it when the program is executed here. Make? Make you not to withdraw? GDB? Can you react to execute file. SHELL? Make you not leave? GDB? Do you? Unix? Shell? command.

?

GDB? Support a lot of command editing features as the "UNIX? shell? program. Can you press? Bash? or? TCSH? Tab? key to let? GDB? Help you fill a unique command, if Unique words? GDB? Will all match the command. • You can also ride the history command with the cursor keys.

GDB? Application Example This section teaches you step by step? GDB? debugger. • The debugged program is quite simple, but it shows the typical application of GDB?.

The program that will be debugged will be listed below. This program is called? Greeting?, It shows a simple greeting, then use it to list it again. # Include?

?

Main? () {

CHAR? my_string []? =? "Hello? there";

?

MY_PRINT? (my_string);

MY_PRINT2? (my_string);

}

?

Void? my_print? (char? * string)

{

PRINTF? ("TH? STRING? IS?% S / N", "String);

}

?

Void? my_print2? (char? * string)

{

CHAR? * string2;

INT? size ,? i;

?

Size? =? strlen? (string);

String2? =? (char? *)? Malloc? (size? ? 1);

For? (i? =? 0 ;? i?

String2 [size? -? I]? =? string [i];

String2 [SIZE 1]? =? `/ 0 ';

PRINTF? ("T? String? Printed? Backward? IS?% s / n", string2);

} Compile it with the following command:

GCC? -o? test? test.c This program shows the following results: the? String? Is? Hello? There

The first line of the "String? Printed? Backward? IS output is correct, but the second line prints are not what we expect. • The output we envisage should be: The? String ?printed? Backward ? Is? estillo? illeh? My_print2? The function is not working properly. Let us use? GDB? Look at what is the problem, type the following command:

GDB? Greeting

-------------------------------------------------- ------------------------------ Note:? Remember to turn the debug options when compiling? Greeting? Program. ------ -------------------------------------------------- ----------------------------

If you forget the program you want to debug as a parameter when you enter the command, you can load it with the "GDB? Prompt to load it:

(GDB)? File? Greeting This command will load? Greeting? The executable is like you are in the GDB? The command line is loaded. At this time, you can use the GDB? Run? command? Greeting ?? When it is? The result is about the result after the GDB? Is like this:

(GDB)? RUN

Starting? Program :? / root / greeting

THE? STRING? IS? HELLO? There

The? String ?printed? Backward? IS

PROGRAM? EXITED? WITH? CODE? 041 This output is the same as the result of the "GDB? The result is, why is it not working in the reverse order ?? In order to find the crux, we can be? MY_PRINT2? Function • After the FOR? statement, set a breakpoint. The specific practice is in the "GDB? prompt, Type List? command three times, list the source code: (GDB)? List

(GDB)? List

(GDB)? List

-------------------------------------------------- ------------------------------ Skill:? At the GDB? Press to press Enter to return to the car. -------------------------------------------------- -----------------------------

The output of the first typing List? Command is as follows: 1? #Include?

2

3? Main? ()

4? {

5? Char? My_string []? =? "Hello? There";

6

7? My_print? (My_string);

8? My_print2? (My_string);

9? }

10 If you press the Enter, GDB? Will then execry again? List? Command, give the following output:

11? My_print? (Char? * String)

12? {

13? Printf? ("THE? STRING? IS?% S / N", "String);

14?}

15

16? My_print2? (Char? * String)

17? {

18? Char? * String2;

19? Int? Size ,? i;

20 Press a cycle to list? The remainder of the Greeting? Program: 21? Size? =? Strlen? (String);

22? String2? =? (Char? *)? Malloc? (Size? ? 1);

23? For? (I? =? 0 ;? I?

24? String2 [size? -? I]? =? String [i];

25? String2 [size 1]? =? `/ 0 ';

26? Printf? ("T? String? Printed? Backward? Is?% S / n", "String2);

27?} According to the source program listed? You can see where you want to break points on the 24th line, on the? GDB? Command line prompt to set the following command to set the breakpoint: (GDB)? Break? 24GDB • The following response will be made: BreakPoint? 1? AT? 0x139 :? File? Greeting.c ,? line? 24

(GDB)

Now type the RUN? Command, will generate the following output:

Starting? Program :? / root / greeting

THE? STRING? IS? HELLO? There

?

BreakPoint? 1,? My_print2? (String? =? 0xBfffDC4? "Hello? There")? At? Greeting.c?: 24

24? String2 [size-i] = String [i] Can you see how the error is generated by setting an observation point of the string2 [size? -? I]? The value of the value is generated. What is the practice to type:

(GDB)? Watch? string2 [size? -? i] GDB? Will respond: WatchPoint? 2 :? string2 [size? -? i] Can you use it now? Next? command to perform a step-by-step execution? For? loop a:

(GDB)? Next after the first loop,? GDB? Tell us? String2 [size? -? i]? The value is? `h`? GDB? Use the following display to tell you this information:

WatchPoint? 2 ,? string2 [size? -? I]

Old? Value? =? 0? `/ 000 '

NEW? Value? =? 104? `H '

MY_PRINT2 (String? =? 0xBfffdc4? "Hello?")? at? Greeting.c: 23

23? For? (I = 0 ;? I

This value is expecting. The result of the subsequent cycle is correct.? When? I = 10? When?

String2 [size? -? i]? The value is equal to? `E`, size? -? i? The value is equal to? 1, the last character has been copied to the new

Skewer.

If you do your loop again, you will see that there is no value assignment? String2 [0]?,? And it is the first character of the new stroke, because the malloc? Function puts them when allocating internal Initialization is empty (NULL) character.?

The first character of? String2? Is an empty character. This explains why there is no output when printing? String2?

.

Now I have found the problem, what is it easy to fix this error.? You have to write the code in the code? String2's first character is changed to? Size? -? 1? Instead? Size. This is because the String2? The size is 12, but the starting offset is? 0, the characters in the string are from the offset? 0? arrival? 10 ,? Offset? 11? Reserved for empty characters.

In order to make the code work properly, there are many modifications. Is a variable that sets another actual size than a string.? This is the code of this solution:

#include?

?

Main? ()

{

CHAR? my_string []? =? "Hello? there";

?

MY_PRINT? (my_string);

MY_PRINT2? (my_string);

}

?

MY_PRINT? (char? * String)

{

PRINTF? ("TH? STRING? IS?% S / N", "String);

}

?

MY_PRINT2? (char? * String)

{

CHAR? * string2;

INT? size ,? size2 ,? i;

?

Size? =? strlen? (string);

Size2? =? size? -1;

String2? =? (char? *)? Malloc? (size? ? 1);

For? (i? =? 0 ;? i?

String2 [size2? -? i]? =? String [i];

String2 [SIZE]? =? `/ 0 ';

PRINTF? ("T? String? Printed? Backward? IS?% s / n", string2);

} Additional? C? Programming tool Slackware? Linux? The release includes some of us that we have not mentioned? C? Development tools.? This section describes these tools and their typical usage .xxgdbxxGDB? Yes? GDB? A graphical interface based on the x? Window? System includes all the features on the gdb? On the command line version.? Xxgdb? Make you can perform a common command by pressing the button.? Set the breakpoint Use graphs to display.

Can you type the following command in one? Xterm? Window: xxgdb You can use any valid command line options in GDB? What is the XXGDB?? What? XXGDB? There are also some unique command line options, Table? 27.2? These options are listed.

Table? 27.2.? Xxgdb? Command line option.

Select? Item Description DB_NAME? Specify the name of the debugger used, is the default? GDB.DB_PROMPT? Specify the debugger prompt,? Default? Gdb.gdbinit? Specify the file file of the initialization? GDB? Name, default? Pinit.nx? Telling the xxgdb? Do not execute? .Gdbinit? File. BIGICON? Use a large icon.

?

Calls You can use the following path: /pub/linux/devel/lang/c/calls.tar.z u 站??

Come? Calls?,? Some older versions? Linux? CD-ROM? The release is also included in the release.? Because it is a useful tool, we also introduce here.? If you think useful,? From? BBS,? Ftp ,? or another CD-ROM? Latch a copy.? Calls? Call the GCC? Preprocessor to process the given source file, then output the function in these files Call the tree map.

?

-------------------------------------------------- ------------------------------ Note: • Install? Calls on your system?, After you log in as superuser Execute the following steps:? 1. Unzip and Untar? Files.? 2.? CD? Enter? Calls? Untar? After the subdirectory is established. / bin? directory.? 4. Put the name of the name? Calls.1? to the directory? / usr / man / man1?? 5. • Delete? / tmp / calls? directory.? These steps will these steps? Calls? program and its guide page installed on your system. ----------------------------------- --------------------------------------------- 当? Calls? Print When the tracking result is called, it gives the file name of the file in the function behind the function: main? [Test.c] If the function is not to? Calls? What is given in the file? Calls? I know where the function is called, only the name of the function is displayed: PrintFCalls? Do not output the recursive and static function. • The recursive function is displayed as below: Fact? <<

>> Static function is like this: total? [Static? In? Calculate.c] as an example, how is it to use? Calls? Process the following:

#include?

?

Main? ()

{

CHAR? my_string []? =? "Hello? there";

MY_PRINT? (my_string);

MY_PRINT2 (my_string);

}

?

MY_PRINT? (char? * String)

{

PRINTF? ("TH? STRING? IS?% S / N", "String);

}

?

MY_PRINT2? (char? * String)

{

CHAR? * string2;

INT? size ,? size2 ,? i;

?

Size? =? strlen? (string);

Size2? =? size? -1;

String2? =? (char? *)? Malloc? (size? ? 1);

For? (i? =? 0 ;? i?

String2 [size2? -? i]? =? String [i];

String2 [SIZE]? =? `/ 0 ';

PRINTF? ("T? String? Printed? Backward? IS?% s / n", string2);

} The following output will be produced: 1? Main? [Test.c]

2? My_print? [Test.c]

3? Printf

4? My_print2? [Test.c]

5? Strlen

6? Malloc

7? PrintFCalls? There are many command line options to set different output formats. For more information on these options, please refer to the calls? Calls? -H?

CPROTOCPROTO? Read? C - source program files and automatically generate prototypes for each function. Code: #include?

?

Main? ()

{

CHAR? my_string []? =? "Hello? there";

MY_PRINT? (my_string);

MY_PRINT2 (my_string);

}

?

MY_PRINT? (char? * String)

{

PRINTF? ("THE? STRING? IS?% S / N" ,? * string);

?

MY_PRINT2? (char? * String)

{

CHAR? * string2;

INT? size ,? size2 ,? i;

?

Size? =? strlen? (string);

Size2? =? size? -1;

String2? =? (char? *)? Malloc? (size? ? 1);

For? (i? =? 0 ;? i?

String2 [size2? -? i]? =? String [i];

String2 [SIZE]? =? `/ 0 ';

PRINTF? ("T? String? Printed? Backward? IS?% s / n", string2);

} You will get the following output: / *? Test.c? * /

Int? main (void);

INT? my_print (char? * string);

INT? my_print2; This output can be redirected to a defined function prototype containing file .indentIndent? Utility is another programming utility included in Linux?. This tool is simple to say Your code produces a beautiful indentation format.? Indent? There are many options to specify how to format your source code. For more information on these options, please see the Indent? InDent? -H?.

The following example is the default output of Indent?

Run? Indent? Previous? C? Code:

#include?

?

MAIN? ()? {

CHAR? my_string []? =? "Hello? there";

MY_PRINT? (my_string);

MY_PRINT2 (my_string) ;?}

?

MY_PRINT? (char? * String)

{

Printf? ("TH? STRING? IS?% s / n" ,? * string);

}

?

MY_PRINT2? (char? * string)? {

CHAR? * string2;

INT? size ,? size2 ,? i;

?

Size? =? strlen? (string);

Size2? =? size? -1;

String2? =? (char? *)? Malloc? (size? ? 1);

For? (i? =? 0 ;? i?

String2 [size2? -? i]? =? String [i];

String2 [SIZE]? =? `/ 0 ';

PRINTF? ("T? String? Printed? Backward? IS?% s / n", string2);

} Run? Indent? After? C? Code: #include?

?

Main? ()

{

CHAR? my_string []? =? "Hello? there";

MY_PRINT? (my_string);

MY_PRINT2? (my_string);

}

?

MY_PRINT? (char? * String)

{

Printf? ("TH? STRING? IS?% s / n" ,? * string);

}

?

MY_PRINT2? (char? * String)

{

CHAR? * string2;

INT? size ,? size2 ,? i;

?

Size? =? strlen? (string);

Size2? =? size? -1;

String2? =? (char? *)? Malloc? (size? ? 1); for? (i? =? 0 ;? i?

String2 [size2? -? i]? =? String [i];

String2 [SIZE]? =? `/ 0 ';

PRINTF? ("T? String? Printed? Backward? IS?% s / n", string2);

} indent? does not change the substance of the code, but just changing the appearance of the code. What makes it more readable? This is always a good thing .GProfgprof? Is it installed in your? Linux? system? / A program in the usr / bin? it enables you to analyze your programs to know which part of the program is performed .GProf? Tell your program to each function to call each function and each The percentage of the time when the function is executed. • You are useful if you want to improve your program performance.

In order to use it on your program? GPROF, you must add the "-pg? Option when compiling the program.? This will make the program generate a file when executed each time it is executed.? GPROF? This file generates a profiling information.

After you run your program and generate? GMON.Out? You can use the following command to obtain an analysis:

GPROF?

Parameters? Program_name? Is it the name of the program that generates the "GMON.out? File.

-------------------------------------------------- ------------------------------ Skills:? Gprof? The analysis of the analysis is large, if you want to check these data It is best to redirect the output to a file. ----------------------------------------- -----------------------------------------

F2C? and? p2cf2c? and? p2c? is two source code conversion programs. Installation? GCC? When these two programs will be installed. If you have some use? Fortran? Or? PASCAL? Write code to use? C? Rewritten,? F2C? And? P2c? It is very useful to you. ? These two programs are generated? C? Code can generally be used directly? GCC? Compilation.

If you want to convert? Fortran? Or? PASCAL? The program is more useful? F2c? Or? P2c? Don't add any options. • If you want to convert the program, you can use a lot of files, you may have to use some Command line option.

Use the "Fortran® program to use? F2C?, Enter the following command:

F2C? my_fortranprog.f

-------------------------------------------------- ------------------------------ Note:? F2C? Request the extension of the program converted? .F? Or? A? .f?. -------------------------------------------- ------------------------------------

To replace a PASCAL? Program to? C? Program, enter the following command: P2C? My_pascalprogram.pas This two programs generated? C? The file name of the source code is the same as the original file name, but expand Name? .F? Or .pas? Changed to? .C.?

?

转载请注明原文地址:https://www.9cbs.com/read-7998.html

New Post(0)