LINUX C language programming - basics

xiaoxiao2021-03-06  69

Author: hoyt

Preface:

This article introduces the basics required for C language programming under Linux. In this article, we will learn the following:

Source program compilation

Makefile writing

Link of library

Program debugging

Head file and system help

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

1. Compilation of the source program

In Linux, if you want to compile a C language source, we should use the GNU GCC compiler. Below we will 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 under the command:

GCC -O Hello Hello.c

The GCC compiler will generate a Hello executable file. Execute ./hello 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 us Requires the compiler to the executable file of our output is Hello and Hello.c is our source program file.

There are many options in the GCC compiler. Generally, we just know how many of them are enough. The -o option We already know, indicating that we ask the executable file name. -c option means that we only require the compiler to output the target Code, without having to output executable files. The -g option means that we ask the compiler to provide us for debugging the program when compiling.

I know these three options, we can compile the simple source of our yourself, if you want to know more options, you can view the GCC's help document, there are many detailed instructions for other options.

2. Makefile

Suppose we have a program below, 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 mytool1_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 this way.

gcc -c main.c

gcc -c mytool1.c

gcc -c mytool2.c

GCC -O main main.o mytool1.o mytool2.o

In this way, we can also generate the main program, and it is very troublesome from time to time. But if we think that if one day we have modified one of them (such as MyTool1.c), then we need to re-enter the command? Maybe you Will say, this is easy to solve, I wrote a shell script, let her help me do it. Yes, for this program, it can play a role. But when we think about things more complicated One thing, if our program has hundreds of source programs, do you want to compile a one to compile? To this end, smart programmers came to do this, this It is Make. We can solve the above problem. We must write a very important file before we perform Make. - Makefile. For the program above, a Makefile The file is:

# This is the Makefile file above the program.

Main: main.o mytool1.o mytool2.o

GCC -O main main.o mytool1.o mytool2.o

Main.o: main.c 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

With this makefile file, when will we modify what files in the source program, 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 connect. Deal with.

Let's learn how made makefile is written.

In the Makefile, the rows that start are notes. The most important thing in Makefile is a description of the dependency of the description file. The general format is:

Target: Components

Tab Rule

The first line represents the dependency. The second line is the rule.

For example, the second line of the Makefile file above

Main: main.o mytool1.o mytool2.o

Indicates that our target (Components) is Main.o mytool1.o mytool2.o When you rely on object modified after the target modification, you will go to the command specified by the rules. Just like our The Makefile's third line is said to execute gcc -o main main.o mytool1.o mytool2.o pay attention to the Tab in the rules, and it is a Tab key.

Makefile has three very useful variables. The meaning of $ @, $ ^, $

$ @ - Target file, $ ^ - All dependencies, $ <- the first dependency file.

If we use the above three variables, then we can simplify our Makefile file as:

# This is a simplified Makefile

Main: main.o mytool1.o mytool2.o

GCC -O $ @ $ ^

Main.o: main.c mytool1.h mytool2.h

GCC-C $

Mytool1.o: mytool1.c mytool1.h

GCC-C $

Mytool2.o: mytool2.c mytool2.h

GCC-C $

After a simplification, our makefile is simple, but people sometimes think simple. Here we learn a Makefile's default rules.

.c.o:

GCC-C $

This rule indicates that all .o files are dependent on the corresponding .C file. For example, MyTool.o depends on MyTool.c, Makefile can also become:

# This is another simplified Makefile

Main: main.o mytool1.o mytool2.o

GCC -O $ @ $ ^

.c.o:

GCC-C $

Ok, our makefile is almost the same, if you want to know more about the Makefile rule, you can view the appropriate documentation.

3. Link of library library

Try to compile the following procedure

/ * TEMP.C * /

#include

INT 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 export a detailed explanation of a function. The header file in which the function is located. If we want to write this function, when we execute When Man Write, the result of the output is not what we need. Because we want the Write this function, it is a description of the command of Write. In order to get the Write function, we have to use Man 2 Write. 2 It means that the WRITE function we use is the system call function, and there is a library function we use 3 indicating that the function is C.

Remember when, MAN is our best assistant.

-------------------------------------------------- ------------------------------ Well, this chapter is so much, there are these knowledge we can enter Exhaust the C program adventure in Linux.

Not accumulating, there is no thousand mileage!

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

New Post(0)