LINUX C language programming - basics

xiaoxiao2021-03-06  85

Home> Programming> C / C > text Linux C programming language - Basic knowledge http://linuxc.51.net Author: hoyt

(2001-05-08 11:31:29)

Preface: This article introduces the basics required to make C language programming under Linux. In this article, we will learn the following: Source program compiles the debug header file and system of the link program of Makefile Help ------------------------------------------------- ------------------------------ 1. Compile the source program under Linux, if you want to compile a C language source program, We want to use the GNU's GCC compiler. Below we explain how to use the GCC compiler with an instance. Suppose we have a very simple source program (Hello.c): int main (int Argc, char ** argv) { Printf ("Hello Linux / N");} To compile this program, we only need to execute them in the command: GCC -O Hello Hello.c GCC compiler will generate a Hello's executable file. Execution ./hello You can see the output result of the program. The gcc in the command line means that we use GCC to compile our source programs, and the -o option means that we ask the compiler to the executable file named hello, hello.c is us Source file file. The GCC compiler has many options. Generally, we just know how many of them. -O Option We already know, indicating that we ask the executable file name. -C option means us only The compiler is required to output the target code without having to output the executable file. The -g option indicates that we ask the compiler to provide us to debug the program in the compilation. I know these three options, we can compile us yourself. The simple source program, if you want to know more options, you can view the GCC's help document, there are many detailed descriptions for other options. 2. Makefile's writing assumption We have one of the following programs, The source code is as follows: / * main.c * / #include "mytool1.h" #include "mytool2.h" int main (int Argc, char ** argv) {mytool1_print ("hello"); MyTool2_print ("Hello") } / * Mytool1.h * / #ifndef _mytool_1_h #define _mytool_1_h void mytool 1_print (char * print_str); #ENDIF / * MyTool1.c * / #include "mytool1.h" void mytool1_print (char * print_str) {Printf ("this is mytool1 print% s / n", print_str);} / * mytool2.h * / #ifndef _MYTOOL_2_H #define _MYTOOL_2_H void mytool2_print (char * print_str); #endif / * mytool2.c * / #include "mytool2.h" void mytool2_print (char * print_str) {printf ( "This is mytool2 print % S / N ", Print_STR);

} Of course, because this program is very short, we can compile gcc -c main.c gcc -c mytool1.c gcc -c mytool2.c gcc -o main main.o mytool1.o mytool2.o, we can also Generate the main program, but also very troubles from time to time. But if we think that if one day we have modified one of them (for example, myTool1.c), then we need to re-enter the command? Maybe you will say, this is very Easy to solve, I wrote a shell script, let her help me do it. Yes, I can play a role. But when we think about things more complicated, if we When there are hundreds of source programs, do you want to build a compile to compile? To this end, smart programmers came to do this, this is make. We only need To do the following Make, you can solve the above problem. Before we execute make, we must write a very important file. - Makefile. For the above program, possible a Makefile file is: # This is the Makefile file main: main.o mytool1.o mytool2.o mytool1.h mytool2.h gcc -c main. c mytool1.o: mytool1.c mytool1.h gcc -c mytool1.c mytool2.o: mytool2.c mytool2.h gcc -c mytool2.c has this makefile file, but when will we modify what is in the source? Document, we only need to execute the make command, our compiler will only compile the files related to our modified files, and other files do not want to go. Let's learn how made makefile is written. In Makefile # Starting rows are notes. The most important thing in Makefile is a description of the dependencies of the description file. The general format is: target: Components Tab Rule The first line of dependencies is dependent. The second line is rule. The second line of the Makefile file above, main: main.o mytool1.o mytool2.o Represents our goal (Target), dependent object (Componen TS) is Main.o mytool1.o mytool2.o When the object rebellion is modified after the target changes, the command specified by the rules is to be executed. Just like the Makefile's third line of Makefile, it is necessary to execute. gcc -o main main.o mytool1.o mytool2.o Note that Tab in the rules is a very useful variable that is a Tab key makefile. Size $ @, $ ^, $

Ok, our makefile is almost the same. If you want to know more about the Makefile rule, you can view the corresponding document. 3. Link of the library tries to compile the following program / * Temp.c * / #includeint main (int Argc) Char ** argv)

{

Double Value;

Printf ("Value:% F / N", Value);

}

This program is quite simple, but when we compile with gcc -o temp temp.c, the following is shown.

/TMP/cc33kydu.o: in function `main ':

/ TMP/cc33kydu.o(.Text 0xe): undefined Reference to `log '

Collect2: ld returned 1 exit status

This error is because the compiler can not find the specific implementation of the log. Although we include the correct header file, we still have to connect the determined library when compiling. Under Linux, in order to use mathematical functions, we must use mathematics Library connection, to this, we have to join the -LM option. Gcc -o temp temp.c -lm can be able to compile correctly. Maybe someone wants to ask, how can we connect to the library when we use the printf function? Yes, this is like this, For some common functions, the GCC compiler will automatically connect to some common libraries so that we don't have to specify themselves. Sometimes we want to specify the path of the library when compiling the program, this time we want to use The compiler's -l option specifies the path. For example, we have a library under / home / hoyt / mylib, so we have to add -l / home / hoyt / mylib when we compile. For some standard libraries, we There is no need to point out the path. As long as they can get a default library, it is possible. The path of the system's default library / lib / usr / lib / usr / local / lib is in these three paths, we can not Specify the path.

There is also a problem, sometimes we use a function, but we don't know the name of the library. What should I do this time? I am sorry, I don't know the answer for this question. I only have a stupid approach. First, I am The standard library path is going to find a library that is related to the function I use, I found the libpthread.a of the thread function (libpthread.a). Of course, if you can't find it, there is only one stupid method. For example, I have to find the library of SIN. I have to use nm -o /lib/*.so|grep sin> ~ / sin command, then look at ~ / sin file, go to it, in the SIN file I will find such a line of rings, linem-2.1.2.so:00009fa0 W Sin, I will know that SiN is in the libm-2.1.2.so library, I use the -LM option (remove the front LIB and behind) The version flag is left, so it is -LM). If you know how to find it, please tell me quickly, I am very grateful. Thank you!

4. Debugging the program

The procedures we have written are unlikely to succeed. In our program, there will be many mistakes we can't think of, this time we have to debug our procedures.

The most commonly used debugging software is GDB. If you want to debug programs in the graphical interface, you can now choose XXGDB. Remember to join the -g option when compiling. About GDB's use can look at GDB help files. Because I don't have I used this software, so I can't say how to use. But I don't like to use GDB. Tracking a program is very annoying, I usually use the value of the intermediate variable in the program to debug the program. Of course you can Choosing your own way, there is no need to learn others. Now there is a lot of IDE environments, which have been bringing a debugger yourself. You can choose a few tricks to find out what you like.

5. Head file and system for help

Sometimes we only know the general form of a function, don't remember the exact expression, or don't remember that the function is described in that header file. At this time we can help the system.

For example, we want to know the exact form of the FREAD function, and we will output a detailed explanation of a function in the Man FREAD system. The header file where this function is located.

Description. If we want to write this function, when we execute Man Write, the result of the output is not what we need. Because we want the description of the WRITE function, it is a description of the write of the write. To get the Write function, we have to use Man 2 Write. 2 indicating that the WRITE function we use is a system call function, and one we often use 3 indicates that the function is a library function of C. Remember, MAN It is our best assistant.

-------------------------------------------------- ------------------------------

Ok, this chapter says so much. With these knowledge, we can enter the C program adventure in exciting Linux.

Not accumulating, there is no thousand mileage!

(http://www.fanqiang.com) Enter [UNIX Forum]

Related Articles LINUX C language programming - Thread operation (2001-05-08 11:43:15) Linux C language programming - Process communication, message management (2001-05-08 11:38:03) Linux under C Language Programming - Signal Processing Function (2001-05-08 11:35:28) Linux C Language Programming - Time Concept (2001-05-08 11:34:12) LINUX C Language Programming - File Operation (2001-05-08 11:33:15) LINUX C language programming - Creation of the process (2001-05-08 11:32:30) Linux C language programming - basics (2001-05-08 11 : 31: 29) ★ Fan Qiang made welcome sharing ★

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

New Post(0)