Go to: how to generate a. So library file in Linux

Source: Internet
Author: User
$ Gcc-C hello. C-O hello. o

1. connect to a static database
Connect to a static library and use the AR command. ar actually means archive.
$ Ar cqs libhello. A hello. o
2. connect to a dynamic library
GCC is used to generate a dynamic library. because there may be multiple versions, the version number is usually specified:
$ Gcc-shared-wl,-soname, libhello. so.1-O libhello. so.1.0 hello. o

In addition, two symbolic connections are established:
$ Ln-s libhello. so.1.0 libhello. so.1
$ Ln-s libhello. so.1 libhello. So
In this way, the dynamic Connection Library of libhello is generated. The most important thing is to pass the GCC-shared parameter so that it is generated as a dynamic library rather than a common execution program.
-Wl indicates that the following parameter is-soname, and libhello. so.1 is directly transmitted to the connector ld for processing. In fact, each database has a soname. When the connector finds that it is finding such a name in the library, the connector will embed the soname into the binary file in the link, instead of the actual file name it is running, during program execution, the program will find the file with the soname name, rather than the file name of the library. In other words, soname is the identification mark of the library.
The main purpose of this operation is to allow the coexistence of database files of multiple versions in the system. Normally, the name library file is the same as the soname file.
Libxxxx. So. Major. Minor
XXXX indicates the database name, Major indicates the main version, and minor indicates the minor version.

As long as the header file is used. c file and. # include "your. H "If. in the upper-level directory of the C file, make the following changes: # include ".. /Your. H "will let your compiler find it!

This is good: http://colintrace.blogdriver.com/colintrace/1049722.html
This is also quite detailed: http://blog.csdn.net/lijing3933/archive/2008/01/28/2070381.aspx

1. Create a shared library
1. Compile slist. cpp separately. The-FPIC option must be passed in during compilation to tell the compiler to generate location-independent code.
Location-independent code can be loaded to any location of the address space without modification.

[Root @ Lee SRC] # ls
Main. cpp slist. cpp slist. h
[Root @ Lee SRC] # G ++-FPIC-g-C slist. cpp

2. When you link to the library, pass the-shared option to the linker and link the target file slist. OTO a shared object.
Libslist. so.1.0.1
Each shared database has a specific search name (soname). The search name conventions are as follows:
Lib + Library name +. So +. Version Number
In the file system, the search name is a symbolic link pointing to the real name. Each Shared Library also has a specific real name, as agreed below:
Search name +. sub version +. Release number
You can use a special compiler option-WL and option to pass the option to LD and separate multiple options with commas,
To get the best results on all systems, link libslist to the Standard C ++ library.

[Root @ Lee SRC] # G ++-g-shared-wl,-soname, libslist. so.1-O libslist. so.1.0.1 slist. O-lstdc ++
[Root @ Lee SRC] # ls
Libslist. so.1.0.1 main. cpp slist. cpp slist. h slist. o

Libslist. so.1 is the search name, libslist. so.1.0.1 is the real name, and slist. O is the target file (or a list of multiple target files)
-Lstdc ++ is the library to be accessed by the database (or the library list-llibrary). For details about this option, refer to the appendix .)

3. Create a link from soname to the library
[Root @ Lee SRC] # ln-Fs libslist. so.1.0.1 libslist. so.1
[Root @ Lee SRC] # ln-Fs libslist. so.1 libslist. So

4. Use-L to enable the linker to search for a database in the current directory and use-lslist to tell it which database to link.
[Root @ Lee SRC] # G ++-g-C main. cpp-O main. o
[Root @ Lee SRC] # G ++-g-o main. O-L.-lslist
[Root @ Lee SRC] # ls
Libslist. So libslist. so.1.0.1 main. cpp slist. cpp slist. o
Libslist. so.1 main. O slist. h

5. Run the command
[Root @ Lee SRC] # LD_LIBRARY_PATH = $ (PWD)./main
4-> 17-> 19-> 10-> 23-> 21-> 11-> 20

LD_LIBRARY_PATH
Provides the directory path used to search the database, with the colon as the interval. Normally, it should not be set because the system file
/ECT/lD. So. conf provides the default path.

Ii. Use the Linked Library
When running a program, the dynamic loader usually finds the library required by the program in the/ECT/lD. So. conf. d directory. However, if
LD_LIBRARY_PATH environment variable is set. It first scans the directories listed in LD_LIBRARY_PATH. for section 5 above, if you run
Command./main, the library cannot be found. For example:
[Root @ Lee SRC] #./main
./Main: Error while loading shared libraries: libslist. so.1: cannot open shared object file: no such file or directory

There are two solutions to the above problems (I can think ):
1 If LD_LIBRARY_PATH is not set:
Copy libslist. So to the/usr/local/lib directory (likewise, you can copy it to/usr/lib)
[Root @ Lee SRC] # cp libslist. So/usr/local/lib
[Root @ Lee SRC] # ldconfig/usr/local/lib
[Root @ Lee SRC] #./main
4-> 17-> 19-> 10-> 23-> 21-> 11-> 20

2 If LD_LIBRARY_PATH is set:
Edit the. bash_profile File
Add LD_LIBRARY_PATH: = $ LD_LIBRARY_PATH:/path/to/libslist. So (excluding libslist. So)
Run the. bash_profile file.
[Root @ Lee SRC] #./root/. bash_profile
[Root @ Lee SRC] #./main
4-> 17-> 19-> 10-> 23-> 21-> 11-> 20

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.