http://boxjerry.blog.163.com/blog/static/1325946902010113115345754
(turn) Fully aware of the GNU C Language Library glibc
2008-11-11 12:04
(turn) Fully aware of the GNU C Language Library glibc
1. What is the C language library used for?
What is the library in English, which the programmer mentions? In short, some of the common program code is stored by the generated file, and later anyone writing a program if you need to use these common code only need to call the library function interface. For example, our C program commonly used printf function, is used to output the information in the program on the screen. If it is required that each person writing a program to write a function to invoke the kernel interface to implement the screen output, it would be a great waste, so the predecessors have written this function related library, in our Linux system, this library is the GNU C language Library glibc, this is the file/usr/lib/ Libc.so. Of course, if you want to use a library file, first you have to have a header file to define each function in the library file, such as/usr/include/stdio.h, when we write the program, we only need to include # # to use printf to output information to the screen.
When we talk about how to build our own library files, we will definitely understand the basics of the library now.
2. How to get libraries and manuals
First, the search for the library file, in the Ubuntu and Debian series of Linux systems, I can use this command:
Apt-cache Search GNU C library |
The output section of this command is as follows:
Glibc-doc-reference-gnu C library:documentation Libc6-gnu C library:shared Libraries Libc6-amd64-gnu C library:64bit Shared libraries for AMD64 Libc6-dbg-gnu C library:libraries with debugging symbols Libc6-dev-gnu C library:development Libraries and Header Files Libc6-dev-amd64-gnu C library:64bit Development Libraries for AMD64 Libc6-i686-gnu C library:shared Libraries [i686 optimized] Libc6-pic-gnu C library:pic Archive Library Libc6-prof-gnu C library:profiling Libraries Libc6-xen-gnu C library:shared Libraries [Xen version] |
Or you can search for libc or glibc
Because we are going to program, that is, to develop, so we have to install Libc6-dev this package.
Use this command to view specific information about this package:
The part of this command output is as follows:
Description:gnu C library:development Libraries and Header Files Contains the symlinks, headers, and object files needed to compile and link programs which use the standard C library. |
You can then run the command to install the package:
Apt-get Install Libc6-dev |
After the installation is complete, you can see what this package provides, using this command:
The output of this command is as follows: After installing this package, the system has the following files.
Because only images and music files are allowed to be uploaded here, a different page is used to store the text messages. |
Dpkg-l all output of the Libc6-dev command is here
Clearly, in the/usr/bin directory are some tool commands that are used to assist us in programming or debugging programs. In the/usr/lib directory are some library files, and we can do a lot of work by invoking the functions provided by these library files directly when we write our programs. In the/usr/include directory is the header file, in the program to include in order to use the function provided by the library. And in the/usr/share/man directory is the online manual, such as a file above is/usr/share/man/man1/gencat.1.gz, then we can see through the man Gencat Gencat related manuals. In the/usr/share/doc/directory is the software with the relevant documentation, it is recommended to use the package before using some of these documents, equivalent to our self-taught teaching materials. Of course, only install Libc6-dev This package may not have any detailed information to you, if the following packages are also installed on the possible data is quite complete.
Glibc-doc-reference-gnu C library:documentation Manpages-dev-manual pages about using Gnu/linux for development Glibc-doc-gnu C library:documentation |
Glibc-doc-reference This is our glibc reference manual, just like some books that may be seen in the World
Manpages-dev is a man manual, and after installing this package, you can printf directly looks at the use of this function. The
Glibc-doc is a document.
Once you have installed the software, you can use the Dpkg-l command to see what these packages provide separately.
For example, Glibc-doc This package provides the following:
all output of the Dpkg-l glibc-doc command is here
and the Manpages-dev package provides the following file:
Dpkg-l All output of the Manpages-dev command is here
3, how to learn from the manual
first of all, I believe that in the content provided by the Glibc-doc package there are these things you must see:
/usr/share/doc/glibc-doc/html /usr/share/doc/glibc-doc/html/libc.html /usr/share/doc/glibc-doc/html/libc_1.html /usr/share/doc/glibc-doc/html/libc_2.html /usr/share/doc/glibc-doc/html/libc_3.html /usr/share/doc/glibc-doc/html/libc_4.html /usr/share/doc/glibc-doc/html/libc_5.html /usr/share/doc/glibc-doc/html/libc_6.html /usr/share/doc/glibc-doc/html/libc_7.html /usr/share/doc/glibc-doc/html/libc_8.html /usr/share/doc/glibc-doc/html/libc_abt.html /usr/share/doc/glibc-doc/html/libc_ovr.html /usr/share/doc/glibc-doc/html/libc_toc.html |
Obviously, this is HTML web-style libc document, quickly open a browser open/usr/share/doc/glibc-doc/html/libc_toc.html to see, believe that after reading these several documents you have some understanding of libc.
Second, some files in the/usr/share/man directory are available in the Glibc-doc and Manpages-dev two software packages. This is what the man command can see.
For example: You may have seen/usr/share/man/man2/chmod.2.gz, which shows a chmod help manual in the 2nd chapter of the Man Handbook. Enter the following command to see how this chmod is programmed to work:
Mans 2 chmod
With this man manual you will know how to write a program to call the function chmod implement the work of modifying the file properties.
Another example: You may see/usr/share/man/man3/abs.3.gz, stating that in the 3rd chapter of the Man Handbook there is an ABS help manual that can be seen. Enter the following command and you will know how to find the absolute value of a number in a program:
Man 3 ABS
I believe you have mastered all the contents of these packages.
/************ about this document ********************************************
*filename: This is how I learned C programming in Linux-fully aware of the GNU C language library glibc
*purpose: How to use glibc to start the C language programming under Linux
*wrote By:zhoulifa ([email protected]) Zhou Lifa (http://zhoulifa.bokee.com)
Linux Enthusiasts Linux knowledge propagator Soho developers are best at C language
*date time:2007-02-05 13:06
*note: Anyone can copy code and use these documents, of course, including your commercial use
* But please follow the GPL
*thanks To:google
*hope: I hope more and more people contribute their strength for the development of science and technology.
* Science and technology stand on the shoulders of giants progress faster! Thanks to the contributions of the source of the predecessors!
*********************************************************************/
4. How to program after seeing the Man handbook?
Some people may ask, in fact quite simple, according to the man manual said to write in the program is OK.
For example man 3 abs when seen:
NAME ABS, Labs, Llabs, imaxabs-compute the absolute value of an integer Synopsis #include <stdlib.h> int abs (int j); Long int Labs (long int J); Long Long int llabs (long long int J); #include <inttypes.h> intmax_t Imaxabs (intmax_t j); DESCRIPTION The ABS () function computes the absolute value of the integer argument J. The labs (), Llabs () and imaxabs () functions compute the Absolute value of the argument J of the appropriate integer type for the function. RETURN VALUE Returns the absolute value of the integer argument, the of the appropriate integer type for the function. |
It is clear here that the ABS function is used to return the absolute value of the numeric J.
We can write a program like this:
#include <stdlib.h>/* Copy the above. * #include <stdio.h>/* because we're going to use the printf function to output information to the screen, man 3 printf will know that this header file is required */ int main (int argc, char * * argv) { int x, y; x =-321; y = ABS (x); printf ("ABS (x) =%d\n", y); return 0; } |
Compile the program with GCC:
GCC ABS.C
To run the program:
./a.out
This program calculates the absolute value of the X variable output to the screen.
Another example: Man 3 sin may see:
NAME Sin, Sinf, sinl-sine function Synopsis #include <math.h> Double sin (double x); float Sinf (float x); A long double sinl (long double x); Link WITH-LM. DESCRIPTION The sin () function returns the sine of x, where x is given in radians. RETURN VALUE The sin () function returns a value between-1 and 1. |
Do it again:
#include <math.h>/* Copy the above. * #include <stdio.h>/* because we're going to use the printf function to output information to the screen, man 3 printf will know that this header file is required */ int main (int argc, char * * argv) { Double x, y; x = 60; y = sin (x); printf ("Sin (x) =%.2f\n", y); return 0; } |
Compile the program with the following command:
GCC SIN.C-LM
Note that there is a-lm more than the above compiler command, how do I know? The man Handbook above tells me: "Link with-lm." Because we used the math library/usr/lib/libm.so, we have to add-LM to reference the library file.
Run the program bar:
./a.out
This program will output a sine value of 60 degrees in radians.
Turn: libc6-dbg libc Library debugging requires installing the library to get debug information to step into