"Linux Programming (fourth edition)" --- Chapter 1: Getting

A large sample projects throughout this book: a simple database application for music CD recording details


unix operating system is a multi-user multi-tasking


unix

unix operating system to encourage a particular programming style, the following is a typical procedure and unix system with the characteristics

  • simple
  • Focus
    when the user emergence of new demand, the gadgets combine more complex tasks have been completed, but not all features are placed on a large program
  • Reusable components
    will be implemented as the core application library, with a simple and flexible programming interface, complete document library can help others to develop similar programs
  • Filter
    for converting the input and produces an output
  • Open file formats
  • Flexibility
    to avoid arbitrary field length limit or the number of records

Linux

Linux is a unix-like kernel implementation may be freely distributed, and the underlying core of an operating system, the purpose is to ensure that in addition to containing linux code can be freely distributable, but does not integrate any proprietary code


GNU

The purpose of the GNU project is trying to create a system compatible with unix, unix name but not by restricting private ownership and source code operating system and development environment


Linux program showed two special types of files

  • Executable: The computer program can be run directly
  • Script files: a set of a set of instructions, instruction by the another program (i.e., interpreter) is performed

/ bin: binary file directories stored for use when the system starts program
/ usr / bin: User binaries directory, used to store the user's standard procedure
/ usr / lacal / bin: Local binaries directory, used to store software installation program


Like Linux, like UNIX, use a colon (:) to separate entries in the path variable in
Linux use a forward slash (/) directory is divided in file name, Windows is the backslash (\)


For Linux developers to the position of connectivity software tools and development resources stored in the system it is important

Here are some important directories and files

1, the application :
program the system to provide normal use, including tools for program development, can be found in the directory / usr / bin; the system administrator to add the program to a particular host or local network is usually in / usr / local / bin or / opt find
recommendations for system-level application, put it in / usr / local directory to run and access required to file
the X window system is usually installed in / usr / X11 or / usr / bin / X11 directory
2, the header file :
when using c programming language or any other language, header files need to provide definitions and declarations of the constants of the system functions and library function call, on the c language, these header files almost always located in / usr / include directory and its subdirectories

-
when calling the c language compiler, you can use -I to include the header file in a subdirectory or non-standard locations
such as:

$ gcc -I/usr/openwin/include fred.c

Instructs the compiler not only in the standard position, also in / usr / openwin / include directory to find the header file included in the program fred.c

-
header file with grep to search for include certain definitions and function prototypes is very convenient. If you want to know for returning the exit status from the program defined by #define name, you simply switch to the / usr / include directory, and then grep command to search for possible names part
as follows:

grep EXIT_ *.h

Export

argp.h:#define ARGP_HELP_EXIT_ERR       0x100 /* Call exit(1) instead of returning.  */
argp.h:#define ARGP_HELP_EXIT_OK        0x200 /* Call exit(0) instead of returning.  */
argp.h:  (ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR)
argp.h:  (ARGP_HELP_SHORT_USAGE | ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR)
argp.h:  (ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG | ARGP_HELP_EXIT_OK \
stdlib.h:#define        EXIT_FAILURE    1       /* Failing exit status.  */
stdlib.h:#define        EXIT_SUCCESS    0       /* Successful exit status.  */

All the above grep command to search for files ending in .h string EXIT_ in the current directory.

3, the library file
library is a collection of pre-compiled function set, usually consisting of a set of functions interrelated to perform a common tasks, such as database access routines (dbm library)
standard system libraries typically stored in / lib and / usr / lib directory of
library files must follow specific naming conventions and the need to explicitly specify on the command line
library files that start with lib, followed by part pointed out that this is what the library (c represents the c language library, m represents math library) the last part of the file name to start, then given a type library file:

  • .a representative of traditional static library
  • Representatives .so shared library

Libraries usually exist in a static library and shared library formats, you can use ls / usr / lib view.
Can give the full path name or library file with the -l flag tells the compiler to search for libraries

-
such as:

$ gcc -o fred fred.c /usr/lib/libm.a

The above command requires the compiler to compile files fred.c, name the file produced by the compiler is fred, in addition to the standard search c language library, but also to solve the math library search function references, the following command can achieve the same effect

$ gcc -o fred fred.c -lm

In the above code, -lm represent standard library directories (in this case, / usr / lib) libm.a named libraries, -lm Another benefit is that if there is a shared library, the compiler will automatically select the shared library

The -L flag added by the compiler library search path, such as:

$ gcc -o x11fred -L/usr/openwin/lib x11fred.c -lXll

Under libX11 library version with / usr / openwin / lib directory to the compiler and linker X11fred
4, static library
when the program needs to use a function in the library, a header file that contains the function declaration. The compiler and linker responsible for linked together to form a single executable program code files and libraries. Need to use the -l option indicates outside the library c language runtime also you need to use

Static library has become the archive (archive), ending in .a, such as c language standard library is /usr/lib/libc.a and X11 libraries /usr/lib/libX11.a

Use ar (on behalf of archive, namely the establishment of the archive) program and use gcc -c function can be compiled separately, you can create your own static library maintenance

-
will establish a library, fred and bill contains two functions, which will call a function in the sample program, these two functions only prints a welcome message.
(1) Create a source file, named fred.c and BILL.C:
fred.c

#include<stdio.h>

void fred(int arg){
        printf("fred:we passed %d\n",arg);
}

bill.c

#include<stdio.h>
void bill(char *arg){
        printf("bill: we passed %s\n",arg);
}

(2) separately compiled object files generated two functions to be included in the library file. Use -c prevents the compiler to create a complete program.

gcc -c bill.c fred.c

(3) create a header file lib.h, the function declaration in the library file:
lib.h

/*
This is lib.h. It declares the functions fred and bill for users
*/

void bill(char *);
void fred(int);

(4) create a sample program program.c, remember to include the header file lib.h:

#include<stdlib.h>
#include "lib.h"

int main(){
        bill("Re:CREATORS");
        exit(0);
}

(5) compile files and links with bill.o:

gcc -c program.c
gcc -o program program.o bill.o
./program

The results are:

bill: we passed Re:CREATORS

(6) to create and use a library file. Use ar program creates an archive file and add the target files to the archive. The reason is called ar program is that it will merge several separate files into one large file to create an archive file or collection. You can also create an archive file of any type with the program ar

ar crv libfoo.a bill.o fred.o

Export

a - bill.o
a - fred.o

(7) through the steps to create a good library files, files are also added two goals went in. Also want to generate a table of contents for the library, you can ranlib command:

ranlib libfoo.a #威函数库生成一个内容表

Now you can use the library, you can add the library file in the list of files used by the compiler to create your own program:

gcc -o program program.o libfoo.a
./program

Export

bill: we passed Re:CREATORS

-L option can be accessed through the library, since not saved in the standard location, use the -L option to tell the compiler where to find it:

gcc -o program program.o -L. -lfoo

-L tells the compiler in the current directory ( . ) Finding libraries
-lfoo tell libfoo.a library use

To view contained in the target file, an executable file or library function, use the command nm

When the program was created, containing only the actual needs of the library function
Here Insert Picture Description
5, the shared library
drawback is that static libraries when running multiple applications and use the same library of functions, memory, there will be multiple copies of the same function, It would be a waste of memory and disk space .
Shared library location to save the static library is the same as in Linux, the shared version of the standard math library is /usr/lib/libm.so

When the program uses shared libraries, the program itself no longer contains the function code, but the code can access shared references at runtime. When the compiled program is loaded into memory to perform the function references are resolved and generated calls to the shared library, it is necessary only to the shared library is loaded into memory.
Thus the system can only keep one copy of the shared library for many applications call, and the shared library can be updated independently of the applications that depend on it
when the Linux launch the application, the library version will consider the application needs to function, preventing function the latest version of the library so that older applications can not be used

For Linux, the load shared libraries and resolver client application function references (dynamic loader) is ld.so , there may be others. Additional search locations for shared libraries can be configured in the /etc/ld.so.conf file. If the file has been modified, you need to perform (x11 need to be added as shared libraries after installation x window system) ldconfig processing

Ldd by running the tool can view the program requires shared library. As running ldd on the sample application, you see the following results

ldd program

Export

linux-vdso.so.1 (0x00007fffe1489000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f423f490000)
/lib64/ld-linux-x86-64.so.2 (0x00007f423fc00000)

Can be seen from the above, the standard c language function library (libc) is shared (.so), the version number of the program needed to 6

Shared library is similar to the dynamic link library used by Windows.
.so libraries correspond to .DLL files are loaded at runtime.
.a similar to .LIB library files are included in the executable program.


Getting Help

man command to access the online manual pages

GNU software and other free software will also use the online documentation system called the info. Or it may be by emacs editor info command to browse the online documentation of all procedures info

Such as:
View GNU C compiler (gcc) documents:

(1) Check the man page:

man gcc

result:

GCC(1)                                                   GNU                                                   GCC(1)

NAME
       gcc - GNU project C and C++ compiler

SYNOPSIS
       gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-Wpedantic]
           ...
           ...
           ...
           ...

(2) using gcc info View document

info gcc

Output:

GCC(1)                                                   GNU                                                  GCC(1)

NAME
       gcc - GNU project C and C++ compiler

SYNOPSIS
       gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-Wpedantic]
           ...
           ...
           ...

Guess you like

Origin blog.csdn.net/qq_40061206/article/details/91365024