$ @ $ ^ % In makefile <use

Source: Internet
Author: User
Tags fread mathematical functions

This article introduces the basic knowledge required for C Programming in Linux. In this article, we will learn the following:

Source program Compilation

Write makefile

Library Link

Program debugging

Header files and system help



1. source program Compilation

In Linux, to compile a C language source program, we need to use the GNU gcc compiler. The following example shows how to use the GCC compiler.

Suppose we have the following simple source code (hello. C ):

Int main (INT argc, char ** argv)

{

Printf ("Hello Linux \ n ");

}

To compile this program, we only need to execute it in the command line:

Gcc-O hello. c

The GCC compiler generates a hello executable file for us. Run./hello to view the output result of the program. In the command line, GCC indicates that we use GCC to compile our source program. The-O option indicates that the executable file name output by the compiler is hello and hello. C is our source program file.

The GCC compiler has many options. Generally, we only need to know a few of them. -O options are known, indicating the executable file name that we want to output. -C option indicates that the compiler only needs to output the target code without outputting executable files. The-G option indicates that the compiler is required to provide information for future debugging of the program during compilation.

With these three options available, we can compile our own simple source program. If you want to know more options, you can view the GCC help documentation, there are many details about other options.

2. Write makefile

Suppose we have the following program, the source code is as follows:

/* Main. C */

# Include "mytool1.h"

# Include "mytool2.h"

Int main (INT argc, char ** argv)

{

Mytoolpattern print ("hello ");

Mytool2_print ("hello ");

}

/* Mytool1.h */

# Ifndef _ mytool_1_h

# DEFINE _ mytool_1_h

Void mytooltypesprint (char * print_str );

# Endif

/* Mytool1.c */

# Include "mytool1.h"

Void mytooltypesprint (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 it like this.

Gcc-C main. c

Gcc-C mytool1.c

Gcc-C mytool2.c

Gcc-O main. O mytool1.o mytool2.o

In this way, we can also generate the main program, which is also very troublesome from time to time. However, if one day we modified one of the files (for example, mytool1.c), would we have to re-enter the above command? You may say that this is easy to solve. I wrote a shell script and asked her to help me complete it. Yes, it can play a role for this program. But when we want to make things more complex, if our program has hundreds of source programs, should the compiler recompile them one by one?

To this end, smart programmers have come up with a good tool to do this. This is make. We only need to execute the following make to solve the above problem. Before executing make, we need to compile a very important file. -- Makefile. For the above program, a possible MAKEFILE file is:

# This is the MAKEFILE file of the above program.

Main: Main. O mytool1.o mytool2.o

Gcc-O 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, we only need to execute the make command when we modify the file in the source program, our compilers only compile the files related to the files we modified. She doesn't even want to handle other files.

Next we will learn how to compile makefile.

In makefile, # the start line is the comment line. The most important thing in makefile is the description of the dependency of the file. The general format is:

Target: Components

Tab rule

The first line indicates the dependency. The second row is the rule.

For example, the second line of the MAKEFILE file above

Main: Main. O mytool1.o mytool2.o

The dependent object (components) of Target main is main. O mytool1.o mytool2.o when the dependent object is modified after the target is modified, it is necessary to execute the command specified by the rule line. Execute gcc-O main. O mytool1.o mytool2.o as described in the third line of makefile above. Note that the tab in the Rule line indicates that there is a tab key.

Makefile has three very useful variables. [Email protected], $ ^, and $ respectively indicate the following meanings:

[Email protected] target file, $ ^ -- all dependent files, $ <-- the first dependent file.

If we use the above three variables, we can simplify our MAKEFILE file:

# This is the simplified makefile.

Main: Main. O mytool1.o mytool2.o

Gcc-O [email protected] $ ^

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 the simplification, our makefile is simpler, but sometimes people want to be simpler. Here we will learn the default rule of a makefile.

. C. O:

Gcc-C $ <

This rule indicates that all. O files are dependent on the corresponding. c files. For example, if mytool. O depends on mytool. C, the makefile can also be changed:

# This is the simplified makefile.

Main: Main. O mytool1.o mytool2.o

Gcc-O [email protected] $ ^

. C. O:

Gcc-C $ <

Well, our makefile is similar. If you want to know more about makefile rules, you can view the relevant documents.

3. Library Link

Compile the following program.

/* Temp. C */

# Include

Int main (INT argc, char ** argv)

{

Double value;

Printf ("value: % F \ n", value );

}

This program is quite simple, but the following error occurs when we compile it with GCC-O temp. C.

/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 log. Although we include the correct header file, we still need to connect to the determined library during compilation. In Linux, to use mathematical functions, we must connect to the mathematical library. Therefore, we need to add the-LM option. Gcc-O temp. C-LM can be correctly compiled. Someone may ask, why didn't we connect to the library when we used the printf function? Yes. For the implementation of some common functions, the GCC compiler automatically connects to some common libraries, so we do not need to specify them ourselves. Sometimes we need to specify the library path when compiling the program. In this case, we need to use the-L option of the compiler to specify the path. For example, we have a library under/home/Hoyt/mylib, so we need to add-L/home/Hoyt/mylib during compilation. For some standard libraries, we do not need to point out the path. As long as they are in the path from the default library. The path of the system's default library is/lib/usr/local/lib under these three paths. We can leave the path unspecified.

Another problem is that sometimes we use a function, but we don't know the library name. What should we do at this time? Sorry, I don't know the answer to this question. I have only one silly solution. First, I went to the path of the standard library to check if there are any libraries related to my functions. Then I found the library file (libpthread. A) of the thread function ). Of course, if you cannot find it, there is only one stupid method. For example, I want to find the library where the sin function is located. You have to use nm-O/lib/*. So | grep sin> ~ /Sin command, and then see ~ /Sin file, where you can find it. In the sin file, I will find a line of libm-2.1.2.so like this: rj9fa0 W sin so that I know that sin is in the libm-2.1.2.so library, I can use the-LM option (remove the lib and the later version logo, and m is left, so it is-lm ). If you know how to find it, please let me know. I am very grateful. Thank you!

4. program debugging

The program we write is unlikely to succeed at one time. In our program, there will be many unexpected errors. At this time, we will debug our program.

The most common debugging software is GDB. if you want to debug the program on the GUI, you can select xxgdb. remember to add the-G option during compilation. for more information about how to use GDB, see the gdb help file. Since I have never used this software, I cannot tell how to use it. However, I don't like to use GDB to track a program. I usually use it to output the value of the intermediate variable in the program to debug the program. Of course, you can choose your own method, and there is no need to learn from others. Now there are a lot of IDE environments, which have their own debugs. You can select a few to try and find out what you like.

5. header files and system help

Sometimes we only know the approximate form of a function, do not remember the exact expression, or do not remember the description of the function in the header file. At this time, we can seek help from the system.

For example, if we want to know the exact form of the fread function, we only need to execute the man fread system to output a detailed explanation of the function. And the header file where the function is located. If we want to write the description of this function, when we execute man write, the output result is not what we need. Because what we want is the description of the write function, but it is the description of the write command. To get the description of the write function, we need to use man 2 Write. 2 to indicate that the write function we use is a system call function, and another commonly used function is 3 to indicate that the function is a C library function.

Remember that man is our best assistant at any time.

[Email protected] $ ^ % <used in makefile

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.