Generation and use of static and dynamic libraries in Linux

Source: Internet
Author: User

 

I. creation and use of static databases:

1. Generate a static library: Library name libmylib.

Ar rcs libmylib. a mylib. o

2. copy the static library to the/usr/lib/or/lib/directory.

Cp libmylib. a/usr/lib/

3. Use of static databases

For example, the test file is test. c.

Gcc-0 test. c-lmylib

-L is the option, and mylib is the library name. Mylib is the intermediate part of libmylib. in Linux, it is agreed that all libraries start with lib.

The static library ends with. a, and the dynamic library ends with. so. You do not need to include a prefix or suffix when compiling a program.

Note: The static library name must start with "lib". Otherwise, the compiler cannot find the library.

Ii. Create and use a dynamic library:

1. The following command creates the mylib. c program as a dynamic library:

(1) gcc-fPIC-o mylib. o-c mylib. c

(2) gcc-shared-o libttt. so mylib. o

You can also directly use a command

Gcc-fPIC-shared-o libttt. so mylib. c

2. Linux has two methods to call functions in the dynamic library connection.

(1) gcc-o test. c./libttt. so

(2) cp libttt. so/usr/lib/libttt. so

Gcc-o test. c/usr/lib/libttt. so

Note: When referencing a dynamic library, the PATH must be included. If you only use libttt. so, make sure that the directory of the library contains the PATH environment variable.

Iii. Use dynamic libraries for system functions:

1. void * dlopen (const char * filename, int flag)

Used to open the dynamic link library with the specified name and return a handle

Flag: RTLD_LAZY, RTLD_NEW, RTLD_GLOBAL

RTLD_LAZY: Before dlopen () returns, undefined variables in the dynamic library (for example, external variables such as extern can also be functions)

Do not parse, that is, do not parse the address of this variable

RTLD_NEW: different from RTLD_LAZY. Before dlopen () is returned, the address of each undefined variable is resolved. If the address cannot be parsed, dlopen returns NULL, And the mislocation is "undefined symbol: xxx..."

RTLD_GLOBAL: the parsed variables in the library can also be used in other linked libraries, that is, they are globally valid.

2. void * dlsym (void * handle, char * symbol)

Return the address of the function corresponding to the function name based on the handle and function name of the dynamic link library.

3. int dlclose (void * handle)

Close the dynamic link library. handle is the handle that calls the dlopen function library.

4. const char * dlerror (void)

When the dynamic library link library fails to be executed, dlerror returns an error message. If the execution is successful, NULL is returned.

Example:

       
        main.cint main(void){void *handle;char *error;void (*welcome)();if ((handle = dlopen("./libttt.so", RTLD_LAZY)) == NULL){printf("dlopen error");return -1;}welcome = dlsym(handle, "welcome");if ((error = dlerror()) != NULL){printf("dlsym error");return -1;}welcome();dlclose(handle);return 0;}gcc -ldl -o main main.c
       

-Ldl indicates the library where the dlopen function is located.

Note: dlopen ("./libttt. so", RTLD_LAZY), the directory is in the current directory. If it is not in the current directory, the program runs incorrectly.

After the shared library is updated or the new library is installed, you must run the ldconfig command to update the corresponding items in the/etc/ld. conf file.

If you use RPM for installation, it is generally updated automatically, but this cannot be ensured.

Add-lm when using the Math Library

Gcc-lm

Library tool usage:

Ldd Tool

Ldd is used to show which shared libraries are required for the execution file and where the Shared Library Loading manager finds the required shared library.

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.