GCC programming basics in Linux

Source: Internet
Author: User
Tags mathematical functions

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, If You Want To compile a C language source program, we need to use the GNU gcc compiler. below
We use an example to illustrate 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 will generate a hello executable file for us. Execute./hello to see the program
The command line GCC indicates that we use GCC to compile our source program, and the-O option indicates
The executable file 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 option we
The executable file name that we want to output.-C indicates that we only need the compiler to output
The target code without outputting the executable file.-G option indicates that the compiler is required
For future debugging information.
With these three options, we can compile a simple source program we have written. If you
For more options, see the GCC help documentation, which provides many details on other options.
Ming.
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 troublesome from time to time. However, if we consider
If one day we modify one of the files (for example, mytool1.c), do we need
New command? Maybe you will say that this is easy to solve. I wrote a shell script and asked her to help me.
It's okay to complete it. Yes, it works for this program, but when we take things
To be more complex, if our program has hundreds of source programs, should the compiler refresh it?
?
For this reason, smart programmers have come up with a good tool to do this. This is make.
You only need to execute the following make command to solve the above problem. Before we execute make, we must first
Compile a very important file. -- makefile. For the above program
The 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, but when did we modify the files in the source program?
As long as the make command is executed, our compilers only compile the files related to the files we modified.
She doesn't even want to handle its 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 to describe the dependency of the file.
Description of the link. The general format is:
Target: Components
Tab rule
The first line indicates dependency, and the second line indicates rules.
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 object is modified, it is necessary to execute the specified life cycle in the Rule line.
To execute gcc-O main. O, just as the third line of makefile mentioned above.
Mytool1.o mytool2.o note that the tab in the Rule line indicates that there is a tab key.
Makefile has three very useful variables: $ @, $ ^, and $ <, which indicate the following meanings:
$ @ -- 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 $ @ $ ^
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 simplify it. Here
Let's learn the default rules for a makefile.
. C. O:
Gcc-C $ <
This rule indicates that all. O files are dependent on the corresponding. c files. For example, mytool. O depends on
Makefile like mytool. C can also be changed:
# This is the simplified makefile.
Main: Main. O mytool1.o mytool2.o
Gcc-o $ @ $ ^
. C. O:
Gcc-C $ <
Well, our makefile is similar. If you want to know more about makefile rules, you can check
See the relevant documentation.

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 when we use gcc-O temp. C to compile it, we will see the following:
.
/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, but we still need to connect to the determined library during compilation. in Linux, in order to use mathematical functions, I
You must connect to the math library. Therefore, we need to add the-LM option. GCC-O temp. C-lm so that we can
Correct compilation. Someone may ask, why didn't we connect to the library before using the printf function? Yes
For the implementation of some common functions, the GCC compiler will automatically connect to some common libraries, so that we do not
It is necessary to specify the library path when compiling the program.
We need to use the-L option of the compiler to specify the path. For example, we have a library under/home/Hoyt/mylib.
In this way, we need to add-L/home/Hoyt/mylib during compilation. For some standard libraries, we do not
It is necessary to specify the path. As long as they are in the path from the default library, you can. The path of the default library of the system/lib
The/usr/lib/usr/local/lib Libraries under these three paths can be left unspecified.
Another problem is that sometimes we use a function, but we do not know the library name.
What should I do? Sorry, I do not know the answer to this question. I have only one silly solution.
Go to the standard library path and check if there are any libraries related to my functions. Then I found the thread.
(Thread) function library file (libpthread. a). Of course, if you cannot find it, there is only one stupid way. For example, if you want to find
In 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, find it there. In the sin file, I will find such a line of libm-2.1.2.so: 9fa0
W sin so I know that sin is in the libm-2.1.2.so library, and I can use the-LM option (remove the front
The Lib and the later version mark, M is left, so it is-lm ).

4. program debugging
The program we write is unlikely to succeed at one time.
There are many unexpected errors. At this time, we need to debug our program.
The most common debugging software is GDB. If you want to debug a program on the GUI, you can select
Select xxgdb. Remember to add the-G option during compilation. For more information about how to use GDB, see the Help File of GDB.
I have never used this software, so I cannot tell how to use it, but I don't like to use GDB.
A program is annoying. I usually use it to output the value of the intermediate variable in the program to debug the program.
However, you can choose your own method without having to learn from others. Now you have many ide environments
You have already installed a debugger. You can try it and find your favorite one.

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 is described in the header file. In this case, 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.
Will output a detailed explanation of the function and the description of the header file where the function is located.

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.