Getting started with UNIX/linux

Source: Internet
Author: User
UNIXlinux entry UNIX philosophy: simplicity, centralization, reusable components, filters, open files & amp; 26684; type and flexibility. UNIX/linux:

Simplicity, centralized, reusable components, filters, open file formats and flexibility.

Linux program design

In linux, two special types of files are used: executable files and script files. Executable files are programs that computers can run directly. A script file is a collection of commands that are executed by another program (The Interpreter.

The search path is configured by the system administrator. it usually contains the standard path of some storage system programs, including:

/Bin: binary file directory, used to store binary files used when the system is started

/Usr/bin: the user's binary file directory, used to store the user's standard program

/Usr/local/bin: local binary file directory, used to store programs installed in specific software

Develop System-guided applications:

It is usually stored in a specific directory reserved by the system. Programs provided by the system for normal use, including tools for program development, can be found in the directory/usr/bin; programs added by the system administrator for a specific host or local network are usually found in/usr/local/bin or/opt.

System administrators generally prefer the usr/local directory because it separates files provided by the vendor and the applications provided by the system.

The gcc driver of the GUN writing system is generally located in the/usr/bin or/usr/local/bin directory, but it runs the applications supported by various compilers from other locations. For linux systems, this location may be a version-specific subdirectory under the/usr/lib/gcc-lib/directory.

Header file:

For the C language, the header file is almost always under the/usr/include directory and its subdirectories. Header files dependent on specific linux versions can be found in/usr/include/sys and/usr/include/linux.

Other programming systems also have their own include files and store them in directories that can be automatically searched by the corresponding editor. For example, the/usr/include/X11 Directory of the S window system and the/usr/include/g ++ directory of gun c ++.

When calling the C compiler, you can use-I to mark include files stored in subdirectories or non-standard locations.

For example

Gcc? I/usr/openwin/include fred. c

It instructs the compiler not only to find the header file included in the program fred. c in the/usr/openwin/include directory.

It is convenient to use the grep command to search for header files containing certain definitions and function prototypes. For example

$ Grep EXIT _ *. h

...

Stdlib. h # define EXIT_FAILURE 1

Stdlib. h # define EXIT_SUCCESS 0

...

$

The grep command searches for the string EXIT _ in all. h files in the current directory _. In this example, it finds the definition we need in the stdlib. h file.

Library files

A library is a set of pre-compiled functions. these functions are all written in accordance with reusable principles. They are usually composed of a group of interrelated functions and execute a common task.

Standard system library files are generally stored in the/lib and/usr/lib directories.

The name of the library file always starts with lib, and the subsequent part specifies the library (for example, c indicates the c language library, and m indicates the mathematics library ). The last part of the file name starts with., and then the type of the file to be exported.

. A represents the traditional static function library

. So represents the Shared Function Library

Static library

Static libraries, also known as archive, all of their files end with.. For example, the standard C language function library/usr/lib/libc.

We can easily create and maintain our own static libraries, as long as we use ar programs and gcc? The c command compiles the functions separately. We should try to save the functions to different source files. if the functions need to access public data, we should put them in the same source file and use the static variables declared in the file. You must use the-l option to specify the library to be used outside the standard C language runtime library.

Experiment: static library

1) Create source files (named fred. c and bill. c respectively)

# Include

Voidfred (int arg)

{

Printf ("fred: you passed % d \ n", arg );

}

# Include

Int bill (char * arg)

{

Printf ("bill: you passed % s \ n", arg );

}

2) compile these functions separately to generate the target file. -The c option is used to prevent the compiler from creating a completed program.

$ Chen123 @ ubuntu :~ /C ++/archive $ gcc-c bill. c fred. c

$ Chen123 @ ubuntu :~ /C ++/archive $ ls *. o

Bill. o fred. o

3) compile a program to call the bill function. Create a header file to declare the functions in our library file, and include the header file in the source files fred. c and bill. c.

Void bill (char *);

Void fred (int );

4) call the program (program. c)

# Include "lib. h"

Int main ()

{

Bill ("hello world ");

Return 0;

}

5) compile and test the program

$ Chen123 @ ubuntu :~ /C ++/archive $ gcc-cprogram. c

$ Chen123 @ ubuntu :~ /C ++/archive $ gcc-oprogram program. o bill. o

$ Chen123 @ ubuntu :~ /C ++/archive $./program

Bill: you passed hello world

6) command to create and use a library file. We use the ar program to create an archive file and add the target file.

Chen123 @ ubuntu :~ /C ++/archive $ ar crvlibfoo. a bill. o fred. o

A-bill. o

A-fred. o

7) add the library file to the file list of the compiler command line to create our program

Chen123 @ ubuntu :~ /C ++/archive $ gcc-oprogram program. o libfoo.

Chen123 @ ubuntu :~ /C ++/archive $./program

Bill: you passed hello world

You can also use the-l option to access our library functions, but because it does not have a standard location, we must use the-L option to indicate where the compiler can find it, as shown below:

Chen123 @ ubuntu :~ /C ++/archive $ gcc-o programprogram. o-L .? Lfoo

The-L. option indicates that the compiler searches for the function library in the current directory. -The lfoo option indicates that the compiler uses the function library libfoo..

To view the target file (. o type), the function library (. a) or executable file can use the nm command. Check program and libfoo. a. The libfoo. a function library contains two functions: fred and bill, and program contains only the function bill. When creating a program, it only contains the function library which is actually needed. Although the header file in the program contains all function declarations in the function library, this does not mean that the entire function library is included in the final program.

Dynamic library

One disadvantage of the static library is that when we run many applications simultaneously and use functions from the same function library, multiple copies of the same function will exist in the memory, there are multiple copies of the program file itself, which consumes a lot of valuable memory and disk space.

The storage location of the shared library is the same as that of the static library, but the shared library has different file suffixes. In a typical linux system, the shared version of the standard math library is/usr/lib/libm. so.

When a program uses a shared library, it does not contain function code, but references the shared code that can be accessed during runtime. When the compiled program is loaded into the memory for execution, the function reference is parsed and a call to the shared library is generated. if necessary, the function reference is loaded into the memory. In this way, the system only keeps one copy of the shared library and uses it for many applications at the same time, and only one copy is saved on the disk. Another advantage is that the shared library and new applications can be independent of other applications.

For linux systems, the program responsible for loading the shared library and parsing the client program function reference is ld. so, maybe it is a ld-linux.so.2.

We can use the running tool ldd

To view the shared libraries required by the program. For example,

Chen123 @ ubuntu :~ /C ++/archive $ ldd program

Linux-gate.so.1 => (0x00392000)

Libc. so.6 =>/lib/i386-linux-gnu/libc. so.6 (0x001ec000)

/Lib/ld-linux.so.2 (0x00a90000)

In this example, we can see that the standard C language function library (libc) is shared (. so), and our program requires that the motherboard number be 6. The dynamic loader is ld-linux.so.2.

Get help

You can use the man command to access the online manual page or the info online document system.

When reading the online manual page, you can use spaces to read the next page, press Enter to read the next line, and press q to exit.

Related Article

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.