Dynamic link library *.so (generate, Invoke) under Linux

Source: Internet
Author: User
Tags naming convention

"Summary" Dynamic library *.so in Linux under the C and C + + programming often encounter, recently on the site to find a few articles about dynamic library compilation and link, here to make a note.

1, the compilation of dynamic library

Here's an example of how to build a dynamic library. Here is a header file: So_test.h, three. c Files: test_a.c, TEST_B.C, TEST_C.C, we compile these files into a dynamic library: libtest.so.

//so_test.h:#include "stdio.h"void test_a();void test_b();void test_c();
//test_a.c:#include "so_test.h"void test_a(){  printf("this is in test_a...\n");}
test_b.c:#include "so_test.h"void test_b(){  printf("this is in test_b...\n");}
//test_c.c:#include "so_test.h"void test_c(){  printf("this is in test_c...\n");}

Compile these files into a dynamic library: libtest.so

$ gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so
2, the dynamic library link

In 1, we have successfully generated a dynamic link library of our own libtest.so, below we have a program to invoke the function in this library. The source file for the program is: test.c.

test.c:#include "so_test.h"int main(){test_a();test_b();test_c();return0;}

TEST.C and dynamic library libtest.so link to execute file test:
g CCTesT.C?L.?LTesT?oTesTMeasurementTryis aNoMovingStateevenConnect,asFruitcolumnoutLIbTesT.so,that?should betheis aevenConnectis often . LDD test
Execute test and you can see how it calls functions in the dynamic library.

3. Compilation parameter parsing

The main thing is an option for the GCC command line:
-shared This option specifies to generate a dynamic connection library (let the connector generate the export symbol table of type T, and sometimes the export symbol of the weakly connected W type) without which the external program cannot connect. Equivalent to an executable file

-fpic: represents compiled to a location-independent code, without this option, the compiled code is location-dependent, so dynamic loading is a way of copying code to meet the needs of different processes, but not to achieve the purpose of real code segment sharing.

-L.: Indicates the library to be connected in the current directory

-ltest: The compiler has an implicit naming convention when looking for dynamic connection libraries, which is to precede the given name with Lib, followed by. So to determine the name of the library

Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.

Of course, if you have root permissions, you can modify the/etc/ld.so.conf file, and then call/sbin/ldconfig to achieve the same purpose, but if there is no root permission, then only the output Ld_library_path method.

4. Attention

Call the dynamic library when there are several problems often encountered, and sometimes, clearly has the library's header file in the directory through the "-I" include, the library is located in the file through the "-l" parameter boot, and specified the "-L" library name, but through the LDD command to see, it is dead and alive can not find your designated link , you have to specify the directory of the dynamic library by modifying the Ld_library_path or/etc/ld.so.conf file. This is usually done to solve the problem where the library cannot be linked.

Under Linux, you can use the Export command to set this value and enter it under the Linux terminal:


And then enter: Export
The correct settings are displayed
The export mode fails after restarting, so you can also modify the Ld_library_path variable with VIM/ETC/BASHRC.
For example:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dynamic link library *.so (generate, Invoke) under Linux

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.