Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux dynamic libraries and Guide     - Android Qemu GPS module (Programming)

- How to modify the SQL Server auto-increment value and the corresponding solution (Database)

- Linux package management operations Basic entry (Linux)

- Asynchronous JavaScript loading (Programming)

- Shell scripts get a snapshot of the page and generates thumbnails (Linux)

- Ubuntu 14.04 and derivative versions of the user install Pantheon Photos 0.1 (Linux)

- RHEL6 install Python and other packages from source (Linux)

- TeamCity continuous integration in the Linux installation (Linux)

- Disk partition MBR (Linux)

- OpenStack / Dev stack with neutron on Ubuntu 14.04 (Server)

- Transfer MySQL database to MariaDB (Database)

- After restarting network services, DNS address failure (Linux)

- The most simple drive to write and test procedures under linux (Programming)

- How to use Linux iptables tool for network sharing (Linux)

- How to recover deleted files in Linux systems (Linux)

- Restrict console access to Linux servers to improve security (Linux)

- Oracle and MySQL difference between the jdbc (Database)

- How do you know your public IP terminal in Linux (Linux)

- Ubuntu How to install Pacman (Linux)

- CentOS minimal network is unavailable resolved (Linux)

 
         
  Linux dynamic libraries and Guide
     
  Add Date : 2017-04-26      
         
         
         
  File name Linux dynamic library file shaped like libxxx.so, so where is the Shared Object acronym, that can be shared object files.

When a dynamic link library to produce an executable file will not copy the DLL code into the executable file, but a reference to the dynamic library records in the executable file.

When the program executes, go to load dynamic library file. If the dynamic library has been loaded, you do not repeat the load, thus saving memory space.

Linux Step By generating and using dynamic libraries are as follows:

Write the source file.
One or several source files compiled and linked to generate a shared library.
By -L < path> gcc options link -lxxx generated libxxx.so.
Libxxx.so into the standard path-link library, or specify the LD_LIBRARY_PATH, to run programs linked libxxx.so.
We explained in detail below by way of example.

Write the source file

The establishment of a source file: max.c, code is as follows:

int max (int n1, int n2, int n3)
{
    int max_num = n1;
    max_num = max_num < n2 n2: max_num;?
    max_num = max_num < n3 n3: max_num;?
    return max_num;
}
Compiled shared library:

gcc -fPIC -shared -o libmax.so max.c
We'll get libmax.so.

In fact, the above process is divided into two steps to compile and link, -fPIC compiler option, PIC is Position Independent Code Abbreviation, said to generate position independent code, which is characteristic of dynamic libraries needed; -shared link option is to tell gcc Building a dynamic library instead of an executable file.

The above command line is equivalent to:

gcc -c -fPIC max.c
gcc -shared -o libmax.so max.o
Write interfaces file as a dynamic library

In order to let the user know that our dynamic library which interfaces are available, we need to write the corresponding header files.

Establish max.h, enter the following code:

#ifndef __MAX_H__
#define __MAX_H__

int max (int n1, int n2, int n3);

#endif
Testing, dynamic link library to produce an executable file

Establish a use max function test.c, the following code:

#include < stdio.h>
#include "max.h"

int main (int argc, char * argv [])
{
    int a = 10, b = -2, c = 100;
    printf ( "max among 10, -2 and 100 is% d \ n.", max (a, b, c));
    return 0;
}
gcc test.c -L. -lmax generate a.out, which represents -lmax to link libmax.so.
-L. Indicates that the search contains the current path to the library file links.

Note that if the same name exists in the same directory at the same time dynamic and static libraries are in such libmax.so and libmax.a current path,
Gcc will give priority to the dynamic link library.

run

Run ./a.out get the following error message.

./a.out: error while loading shared libraries: libmax.so: can not open shared object file: No such file or directory
Can not find libmax.so, Linux is the original search through /etc/ld.so.cache file to link dynamic libraries.
The program reads /etc/ld.so.cache ldconfig /etc/ld.so.conf file is generated.
(Note, /etc/ld.so.conf and need not include / lib and / usr / lib, ldconfig program will automatically search both directories)

If we add the path to /etc/ld.so.conf libmax.so located, and then run ldconfig with root privileges, updates /etc/ld.so.cache, a.out is running, you can find libmax. so.

But as a simple test example, let's change the system of things, does not seem right.
There is another simple method is to specify the LD_LIBRARY_PATH as a.out.

LD_LIBRARY_PATH =. ./a.out
Program can normally run. LD_LIBRARY_PATH =. Tell a.out, first look for a dynamic link library in the current path.

For executables elf format by ld-linux.so * to complete, and it has search DT_RPATH segment elf files, environment variables LD_LIBRARY_PATH, /etc/ld.so.cache file list, / lib /, / usr / lib directory, locate the library file will load it into memory.

makefile make work automation

Write makefile, as follows:

.PHONY: Build test clean

build: libmax.so

libmax.so: max.o
gcc -o $ @ -shared $ <

max.o: max.c
gcc -c -fPIC $ <

test: a.out

a.out: test.c libmax.so
gcc test.c -L. -lmax
LD_LIBRARY_PATH =. ./a.out

clean:
rm -f * .o * .so a.out
make build will generate libmax.so, make test will generate a.out and execute, make clean will clean up the build and test results.
     
         
         
         
  More:      
 
- MySQL primary and secondary replicate data inconsistencies (Database)
- Use lsof restore accidentally deleted log files or database (Linux)
- Python kills corresponding process according to pid (Programming)
- Linux directory permissions to read and execute permissions difference (Linux)
- CentOS install Redis (Database)
- Linux partition command (Linux)
- Binary tree traversal recursive and non-recursive (cyclic) traversal achieve (Programming)
- Hardware Firewall Basics (Linux)
- The oh-my-zsh into true my zsh (Linux)
- C language sorting instance (select, bubble, insert, binary, fast) (Programming)
- RedHat install GCC problem --- Depends (Linux)
- Build a super simple "hardware" firewall router (Linux)
- Postfix mail service system principle and configuration (Linux)
- Linux command to view the system status (Linux)
- Linux kernel netpoll framework netconsole (Linux)
- JavaScript basic types and type conversion (Programming)
- Ubuntu 15.04 installation MATE 1.10 (Linux)
- 12 Linux Process Management Commands (Linux)
- ASM required disk format process in Solaris platforms (Linux)
- Under Linux using Magent + Memcached cache server cluster deployment (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.