Linux C Programing

xiaoxiao2021-03-05  32

Related link http://www.chinalinuxuxpub.com/doc/pro/

1. Use the math library #include SIN (), COS (), ACOS (), etc. Compile with GCC, it is prompted that these mathematical functions are not defined. The reason for this problem is that the compilation method is not correct: the correct compilation statement is gcc mathsin.c -o mathsin -lm where mathsin.c is the original file MathSIN is the target file. The basic knowledge required to perform 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's GCC compiler. Below we explain how to use the GCC compiler with an example.

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 of the program. The gcc in the command line means that we are using GCC to compile our source programs, and the -o option indicates that we ask the compiler to the executable file named hello and hello.c is our source program.

There are many options in the GCC compiler. Generally speaking, we just know how many of them are enough. -o Option We already know, indicating that we ask the executable file name. The -c option means that we only require the compiler to output the target code without having to output an executable file. -g Option means that we ask the compiler to provide information on the program to debug 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 a file (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, it is possible to play for this program. But when we think more about things, if we have hundreds of source programs, do you want to compile a back to compile?

To this end, smart programmers came up with a good tool to do this, this is make. As long as we do the following Make, we can solve the above problem. Before we execute make, we must write a very important file first. --Makefile. For the program above, the possible Makefile 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 not comment. The most important thing in Makefile is the description of the dependencies 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 the dependents of Target Main is main.o mytool1.o mytool2.o When you rely on object modified after the target changes, you will go to the command specified by the rule. Just like our top Makefile's third line, you want to perform gcc -o main main.o mytool1.o mytool2.o pay attention to the tab in the rule, and it is a Tab button.

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.hgcc -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 default rule

.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, which makes that 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 occurs because the compiler cannot 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 the mathematical function, we must connect to the math library, to this, we have to join the -LM option. GCC -O TEMP TEMP.C -LM can be compiled correctly. Maybe someone wants to ask, how can we don't connect to the library in front of the printf function? Yes, for some common functions, the GCC compiler will automatically connect 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. At this time we have to use the -l option specified by the compiler. For example, we have a library under / home / hoyt / mylib so that we have to add -l / home / hoyt / mylib when we compile. For some standard libraries, we don't have to point out the path. As long as they can get in the path of the default library. The path / lib / usr / lib / usr / local / lib / usr / lib / usr / local / lib / usr / lib / usr / local / liba will not specify a 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 went to the standard library path to find a library that is related to the functions I use, I found the libpthread.a of the thread function. Of course, if you can't find it, there is only one stupid method. For example, I am looking for a library where this function is located. I have to use the nm -o /lib/*.so|grep sin> ~ / sin command, then look at the ~ / sin file, go to it there. In the SIN file, I will find such a line of LIBM-

2.1.2.so:00009fa0 W SIN This I will know that SiN is in the libm-2.1.2.so library, I can use the -lm option (remove the previous lib and the version flag, there is left So -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. Since I haven't used this software, I can't say how to use it. 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 choose 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 probably form of a function, don't remember the exact expression, or don't remember that the function has been 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 the function in the execution of the Man FREAD system. The header file in which this function is located. If we want to write this function, when we perform Man Write, the result of the output is not what we need. Because we want the description of the WRITE function, it is an instructions for Write this command. In order to get WRITE functions, we have to use Man 2 Write. 2 indicating that the WRITE that we use is a system call function, and there is a library function we often use 3 indicating that the function is C. function.

Remember when, MAN is our best assistant.

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

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

New Post(0)