Common rules for Makefile

Source: Internet
Author: User
Tags mathematical functions

First, preface

This article describes the basic knowledge required for C programming under Linux. In this article, we will learn the following:

    • SOURCE program Compilation
    • The writing of Makefile
    • Library's Links
    • Debugging of the program
    • header files and system help
Ii. body 1. Compiling the source program

Under Linux, if you are compiling a C language source program, we will use the GNU GCC compiler. Let's take an example to illustrate how to use the GCC compiler.
Suppose we have the following very simple source program (HELLO.C):

int Main (int argc,Char * *argv) {    printf ("Hello linux\n  ");}

To compile this program, we simply execute it at the command line:

Gcc-o Hello hello.c

The GCC compiler will generate a hello executable file for us. Execution./hello can see the output of the program. GCC in the command line indicates that we are using GCC to compile our source program, and the-O option indicates that we require the compiler to give us the output of the executable file named Hello and hello.c is our source program file.

The GCC compiler has a number of options, and generally we need to know just a few of them. The- o option we already know, indicates the executable file name we are asking for output. The-c option means that we only require the compiler to output the target code, not the necessary output of the executable file. The-G option indicates that we are asking the compiler to provide us with information for debugging the program at compile time. Knowing the three options, we can compile our own simple source program, if you want to know more options, you can see the GCC help documentation, there are many other options detailed instructions.

2.Makefile of writing

Suppose we have one of the following programs, the source code is as follows:

Main.c

 /*   main.c    #include   " mytool1.h    #include   " mytool2.h  "  int  Main (int  argc,char  **ARGV) {mytool1_print (  hello  Span style= "color: #800000;" > " "; Mytool2_print (  " hello   );}  

Mytool1.h

/**/#ifndef _mytool_1_h#define _mytool_1_hvoid mytool1_print ( Char *print_str);#endif

mytool1.c

/**/"mytool1.h"void mytool1_print (Char *print_str) {    printf ("This ismytool1 print%s\n", Print_ STR);}

Mytool2.h

#ifndef _mytool_2_h #define _mytool_2_hvoid mytool2_print (char *print_str);#endif

Mytool2.c

/**/"mytool2.h"void mytool2_print (Char *print_str) {    printf ("This ismytool2 print%s\n", Print_ STR);}

Of course because this program is very short we can compile it like this

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

In this case we can also produce the main program, but also not very troublesome. But if we think about it, if one day we change one of the files (say mytool1.c), then do we have to re-enter the above command? Maybe you can say, this is easy to solve Ah, I write a shell script, let her help me to complete No. Yes, for this program, it can play a role. But when we make things a little more complicated, if our program has hundreds of source programs, do we have to compile the compiler again?

To do this, smart handlers come up with a good tool to do this, and that's make. As long as we do the following make, we can solve the above problem. Before we execute make, we need to write a very important file first. --makefile. For the above program, one of the possible makefile files is:

# This is the makefile file for the program above MAIN:MAIN.O mytool1.o    mytool2.o -o main main.o mytool1.o mytool2.omain.o:main.c mytool1.h mytool2.h    -c main.cmytool1.o:mytool1.c Mytool1.h    -c mytool1.cmytool2.o:mytool2.c mytool2.h    c mytool2.c

With this makefile file, but when we modify the source program of what files, we just execute make command, our compiler will only compile and we modify the files related to the file, the other files she did not want to get married.

Below we learn how makefile is written .

Lines that begin in makefile are comment lines. The most important thing in Makefile is a description of the dependencies of the file. The General format is

Target:componentstab rule

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

For example, the second line of the makefile file above us.

MAIN:MAIN.O MYTOOL1.O MYTOOL2.O

Indicates that our target main's dependent object (components) 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. Just like our makefile on the third line. To execute Gcc-o main MAIN.O mytool1.o mytool2.o Note the tab in the Rule line indicates that there is a TAB key.

The makefile has three very useful variables. The meanings of [email protected],$^,$< representatives are:
[Email protected] Target file, $^--all dependent files, $<--the first dependent file.


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

# This is simplified after the makefilemain:main.o mytool1.o mytool2.o     -O [email protected] $^main.o:main.c mytool1.h    mytool2.h-C $<mytool1.o:mytool1.c mytool1.h    -C $<mytool2.o:mytool2.c    mytool2.h-C $<

After simplifying our makefile is a bit simpler, but people sometimes want to be a little simpler. Here we learn a makefile default rule

. C.O:     -C $<

This rule indicates that all the. o files are dependent on the corresponding. c Files (CPP files also have similar functionality, just write. cpp.0: You can ). For example, MYTOOL.O relies on mytool.c so that makefile can also become:

# This is once again simplified MAKEFILEMAIN:MAIN.O mytool1.o mytool2.o     -O [email protected] $^. C.O    :-C $<

Well, our makefile is about the same, if you want to know more about the makefile rule, you can view the corresponding document.

3. Links to Libraries

Try compiling the following program

/** *#includeint main (int argc,Char * *argv) {     Double value;    printf ("value:%f\n", Value);}

This program is fairly simple, but when we compile with Gcc-o temp TEMP.C The error shown below appears.

/tmp/cc33kydu.o:in function ' main':/tmp/cc33kydu.o (. text+0xe): Undefined reference to ' Log'1 exit status

This error occurs because the compiler cannot find a specific implementation of log. Although we include the correct header file, we still need to connect the identified libraries when compiling. Under Linux, in order to use mathematical functions, we have to connect to the math library, for which we want to add the-LM option. Gcc-o temp TEMP.C-LM to compile correctly. Perhaps some people ask, before we use the printf function when there is no connection library? So, for the implementation of some commonly used functions, the GCC compiler will automatically connect some common libraries so that we don't have to specify them ourselves. Sometimes we need to specify the path of the library when compiling the program, and this time we use the compiler's-l option to specify the path. For example, we have a library under the/home/hoyt/mylib, so that we compile the time to add-l/home/hoyt/mylib. For some standard libraries, it is not necessary to point the path. As long as they are in the path to the default library. The path to the default library of the system/lib/usr/lib/usr/local/lib the library below these three paths, we can not specify a path.

Another problem, sometimes we use a function, but we do not know the name of the library, this time what to do? I'm sorry, I don't know the answer to this question, I have only one silly way. First, I looked under the standard library path to see if there were any libraries related to the function I used, so I found the library file (LIBPTHREAD.A) of the thread function. Of course, if you can't find it, there's only one stupid way. For example, I'm looking for a library of sin, which is the function. Had to use Nm-o/lib/*.so|grep sin>~/sin command, and then see ~/sin file, into that inside to find. In the sin file, I will find such a line libm-2.1.2.so:00009fa0 W sin so I know the sin in the libm-2.1.2.so library, I use the-LM option (remove the previous Lib and the later version of the logo, left m so it is- LM). If you know how to find, please tell me quickly, I am very grateful. Thank you!

4. Commissioning of the program

The program we write is unlikely to be successful at one time, and in our program there will be a lot of mistakes that we don't expect, and we'll have to debug our program.
  The most common debugging software is GDB. If you want to debug a program under a graphical interface, you can now choose Xxgdb. Remember to add the-G option when compiling. The GDB help file can be seen with GDB's use. Since I have not used this software, I am not able to say how to use it. But I don't like using GDB. Tracking a program is very annoying, I usually use in the program to output the value of the intermediate variable to debug the program. Of course you can choose your own way, there is no need to learn others. Now that you have a lot of IDE environments, you have a debugger on your own. You can choose a few to try and find one 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 function in that header file is described. We can turn to the system at this time.
For example, if we want to know the exact form of the fread function, we just need to execute the man fread system to output a detailed explanation of the function. and the header file where this function is located is explained. If we are going to write the description of this function, when we execute the man write, the result of the output 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. in order to get the function description of write we want to use the Man 2 write. 2 means we use the Write function is the system call function, there is a commonly used is that the function is C of the library function.
Remember that man is our best assistant no matter what time it is.

Third, the description

This article is organized from http://blog.csdn.net/kesaihao862/article/details/7332528. The linked article is also reproduced from elsewhere, but the original author of the article I have not found, very sorry did not explain the original source.

Common rules for 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.