Create a function library in linux

Source: Internet
Author: User
Article Title: Create a function library in linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Function libraries can be divided into static libraries and dynamic libraries.

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 runs. Therefore, dynamic inventory is required when the program runs.

Program 1: hello. h

# Ifndef HELLO_H

# Define HELLO_H

Void hello (const char * name );

# Endif // HELLO_H

Program 2: hello. c

# Include

Void hello (const char * name)

{

Printf ("Hello % s! \ N ", name );

}

Program 3: main. c

# Include "hello. h"

Int main ()

{

Hello ("everyone ");

Return 0;

}

The. o file is required for both dynamic and static databases. The. o file is compiled and generated first. o file.

# Gcc-c hello. c

1. Create a static database

The naming rules for static library file names are prefixed with lib, followed by the static library name and the extension is.. For example, if the static library name is myhello, the static library file name is libmyhello..

# Ar cr libmyhello. a hello. o

Static Library: you only need to add a declaration (including header files) containing the function you need to use in your source program ), then, it is okay to specify the static library when gcc generates the target file (unless the header file you include is in/usr/include, and the library file is in the standard library/usr/lib,/lib, otherwise, you must specify their paths)

# Gcc-o hello main. c-L.-lmyhello

#./Hello

Hello everyone!

Delete the static library file and run./hello. The program runs normally. This indicates that the static library public function has been linked to the target file.

2: Use the. o file to create a dynamic library

The file extension of the dynamic library is. so.

# Gcc-shared-fPCI-o libmyhello. so hello. o

Dynamic databases are used in the same way as static databases.

# Gcc-o hello main. c-L.-lmyhello

#./Hello

./Hello: error while loading shared libraries: libmyhello. so: cannot open shared object file: No such file or directory

Oh! Error. Check the error message. The dynamic library file libmyhello. so is not found. When the program is running, it searches for the required dynamic library files in the/usr/lib and/lib directories. If it is found, it is loaded into the dynamic library. Otherwise, a message similar to the preceding error will be prompted to terminate the program.

There are three ways to find the generated dynamic library:

1) copy the database to the/usr/lib and/lib directories.

(2) Add the path of the library in the LD_LIBRARY_PATH environment variable.

For example, the dynamic library libhello. so is in the/home/example/lib directory:

$ Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/home/example/lib

(3) modify the/etc/ld. so. conf file, add the path of the Library to the end of the file, and execute ldconfig refresh. In this way, all the library files under the added directory are visible.

When the static library and the dynamic library have the same name, the gcc command takes precedence over the dynamic library.

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.