C Programming learning records in Linux

Source: Internet
Author: User
1. Linux Link Library Introduction
In Linux, You Can Package Multiple Target files into a library file so that they can be called at any time during programming without having to rewrite or define them. This package is called a library function. Library files are a collection of pre-compiled functions, which are all written in accordance with reusable principles. They are usually composed of a group of interrelated functions used to complete a common task. For example, a function (curses Library) used to handle screen display.
1. Basic Knowledge:
In Linux, GCC requires preprocessing, compilation, assembly, and connection during program compilation.
(1) In the pre-processing phase, the main process is # include and # define. It inserts the. h file contained in # include into the position where # include is located to generate the. I file;
(2 ). the compilation stage is the most important stage. At this stage, GCC first checks the syntax and then converts the file into an assembler to generate. s file; the above two output files are all text files. We can use commands such as CAT text processing to read these output files;
(3) In the Assembly stage, translate *. s files into binary machine command files *. O, which can be understood only with the help of disassembly tools such as GDB;
(4) In the connection phase, GCC connects all *. O files into an executable file in this phase, and the connection to the library files is also completed in this step.
2. standard and non-standard databases
Library functions can be divided into two categories: standard and non-standard (custom) databases.
(1) standard library files are public. Any user in the system can use these library functions, which are generally stored in the/lib or/usr/lib directory, and provide include calls in the form of header files. During compilation, the C language compiler (more specifically, the linked program) should be told to find the library files. By default, it will only find the standard library files.
When the standard library is used, GCC and other compilation programs can be automatically connected. Therefore, you only need to include the defined header files, such as libc.. Libc. A is a standard C function library that contains basic functions such as memory management or input/output operations.
(2) When connecting non-standard libraries, you must add-lname (Name: Remove lib and tail. A or. the Library name after so) parameter; non-standard libraries can be placed in any directory, usually in the current directory, but when placed in a non-system default search path, you must use-ldir (DIR is the path name) to specify the search path.
For example, mathematical functions are not part of the C standard library. They are composed of mathematical libraries/usr/lib/libm. therefore, when using the mathematical functions in this library, except for # include <math. h> set the header file/usr/include/math. H is added to the program file, and the GCC-LM option must be explicitly used to connect to this library (mathematical library libm. A is stored in the default library search path/usr/lib of the system. Therefore, the-ldir parameter is not required ).
3. Static and shared libraries
The function library is generally divided into two formats: static and shared (also called dynamic library). library files must follow certain naming rules. The library file name will always start with Lib, the following section describes the function library (for example, C indicates that this is a C language library, and M indicates that this is a mathematical library ). The last part of the file name starts with a period (.), and the type of the library file is given. A is a static library file, and. So and. SA are shared library files. The. o file must be used to generate both dynamic and static databases.
In Linux, the/lib and/usr/lib directories are used as the default Library (dynamic library or static library) search path, therefore, you can directly use the libraries in these two directories without setting the search path. For a database that is not in the default library search path, you must add the location of the database to the library search path (such as modifying environment variables), or when the program is connected, use the-l parameter to specify.
However, during runtime, the program also needs to connect to the dynamic library. Therefore, for the dynamic library, it is best to add the location of the database to the search path of the database or copy the database to the default search directory of the system. Static databases do not have this restriction.
(1) static library
A static library is also called an archive. The file names are used by convention. end of a, such as C standard library/usr/lib/libc. a, X11 Library/usr/x11r6/lib/libx11.a, etc. It contains several. O files. Each member in the static library (. o file) has no special format and is only a set of. O files. During the connection, the file code of the static library will be copied to the executable file, so the executable file of the static connection is generally larger.
In Linux (UNIX), use the tool "Ar" to maintain and manage the static library, create a static library, and run the AR command. At the system prompt, type the following command to create the static library file libmyhello. a.
# Ar-RC libmyhello. A hello. o
The disadvantage of the static library is that if we run multiple programs at the same time and they all use functions from the same function library, there will be many copies of the same function in the memory, and many copies of the same function in the program file itself. This consumes a lot of valuable memory and hard disk space.
(2) Shared Library
Also known as a dynamic library, many UNIX systems support shared libraries, while eliminating unnecessary consumption of static libraries in memory and hard disks. The code of the dynamic library is not added to the executable file, but loaded when the program is executed.
The default storage location of the shared library is the same as that of the static library, but it has different file suffixes. On a typical Linux system, the shared version of the C standard library is/usr/lib/libc. So n, where N is the main version number. When the static library and the dynamic library have the same name, the GCC command takes precedence over the dynamic library.
You can use GCC to create a dynamic library. At the system prompt, type the following command to get the dynamic library file libmyhello. So.
# Gcc-shared-fpci-O libmyhello. So hello. o
4. Comparison
The static library will be connected to the target code during program compilation. This static library is no longer needed when the program is running. The dynamic library is not connected to the target code during program compilation, but is loaded only when the program is running. Therefore, dynamic inventory is required when the program is running.
If the program loads the library documentation during compilation, the static library is used. If the target code is loaded at runtime, it becomes a dynamic library. In other words, if a static library is used, the static library code is copied to the code segment of the program during compilation, and the program size expands. If a dynamic library is used, only the name and function name of the Library Document are retained in the program, and the Library Document and function body are searched at run time. The size of the program does not change much.
The principle of the static library is to change the space for time, increase the program volume, and reduce the running time. The dynamic library is to change the space for time, which increases the running time, but it reduces the size of the program itself.
5. Summary
Standard static library or shared library, path and library name do not need to be explicitly specified;
Non-standard static library. The specified database name must be displayed. The path must be specified when the default path is not used;
Non-standard shared library. during compilation, the database name must be specified, and the path name must not be specified in the default path,
When running, you need to add the library location to the library search path or copy the Library to the default path when it is not in the default path.
6. GCC-related commands
Usage tips:
(1) Add the path of the header file to the library after the-I parameter of GCC;
(2) Add the directory of the library file after the-l parameter of GCC;
(3) Add the library file name after the-l parameter of GCC, But remove the lib and. A extensions. For example, if the library file name is libtest. A, the parameter is-ltest.

2. memory functions in Linux
(1). getpagesize (obtain the memory page size)
# Include <unistd. h>
Size_t getpagesize (void );
The size of the returned page, in bytes. The system page size is different from the hardware page size.
(2). MMAP (Create memory ing)
# Include <sys/Mman. h>
Void * MMAP (void * ADDR, size_t length, int Prot, int flags, int FD, off_t offset );
Maps the content of a file to the memory, and the access to the memory area is to directly read and write the file content.
(3). munmap (remove memory ing)

3. Time Functions

(4). ftime (obtain the current time and date)
# Include <sys/timeb. h>
Int ftime (struct timeb * TP );
Returns the current time and date from the structure indicated by TP. The TP structure is defined:
Struct timeb {
Time_t time; // The number of seconds since January 1, January 1, 1970
Unsigned short millitm; // millisecond
Short timezone;
Short dstflag;
};
(5). gettimeofday (get the current time)
# Include <sys/time. h>
# Include <unistd. h>
Int gettimeofday (struct timeval * TV, struct timezone * tz );
Settimeofday (set the current time)

4.

(6). FFs (find the first value as the real digit in an integer)
# Include <string. h>
Int FFs (int I );
FFS judges each bit in the parameter I byte from the low to the highest, and returns the position where the first bit value is 1.

5. String Functions

(7). # include <strings. h>
Char * index (const char * s, int C );
Char * rindex (const char * s, int C );
Find the position where character C appears in string S.
Similar to strchr
(8). strcasecmp (case-insensitive comparison string ignored)
# Include <strings. h>
Int strcasecmp (const char * S1, const char * S2 );
(9) strfry randomly restructured the string content
# DEFINE _ gnu_source
# Include <string. h>
Char * strfry (char * string );
Because strfry changes the content of the string parameter, the string parameter must point to the memory address that can be written.
Int strncasecmp (const char * S1, const char * S2, size_t N );

6. encryption functions

(10). crypt encrypt the password or data
# DEFINE _ xopen_source
# Include <unistd. h>
Char * crypt (const char * Key, const char * salt );
-Lcrypt must be added during GCC compilation.
(11). getpass obtains password input.
# Include <unistd. h>
Char * getpass (const char * prompt );

7. Data Structure Functions

(12). # include <stdlib. h>
Void * bsearch (const void * Key, const void * base,
Size_t nmemb, size_t size,
INT (* COMPAR) (const void *, const void *));

8. inter-process communication functions

(13). Create a named MPs queue
# Include <sys/types. h>
# Include <sys/STAT. h>
Int mkfifo (const char * pathname, mode_t mode );
(14). Pipe build Pipeline
# Include <unistd. h>
Int pipe (INT fields [2]);
Pipe () creates a pipeline and returns the file description with the fields array parameter. Filedes [0] is the read end of the MPs queue, and filedes [1] is the write end of the MPs queue.
(15). popen create pipeline I/O
# Include <stdio. h>
File * popen (const char * command, const char * type );
Int pclose (File * stream );
Function input value: Command: Refers to a string ending with a null Terminator. This string contains a shell command and is sent to/bin/sh for execution with the-C parameter, that is, the shell executes the type: "R": The file pointer is linked to the commond standard output, that is, the result of this command produces the output "W": The file pointer is connected to the standard input of the command, that is, the result of this command is input.
Function return value: Success: file stream pointer error:-1
(16). Connect to establish a socket connection
# Include <sys/types. h>/* See Notes */
# Include <sys/socket. h>
Int connect (INT sockfd, const struct sockaddr * ADDR, socklen_t addrlen );

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.