How to compile a dynamic link library with gcc *.so file dynamic Library

Source: Internet
Author: User
Go: How to compile. So dynamic library Q: I source files for main.c, x.c, y.c, z.c, header file for x.h,y.h,z.h
How to compile into. So dynamic library.
Compilers with GCC
We'd better give you a detailed explanation of the parameters.

For:
# claims to move the connection library, assuming the name is libtest.so
GCC x.c y.c z.c-fpic-shared-o libtest.so

# Connect MAIN.C and Dynamic connection libraries to build an executable file
GCC main.c-l.-ltest-o Main

# Output LD_LIBRARY_PATH environment variable, one side dynamic library loader can find the required dynamic library
Export ld_library_path= $LD _library_path:.

# test whether the dynamic connection, if listed libtest.so, then should be connected to the normal
LDD Main

# It's not going to be done.

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

-L.: Indicates the library to connect to in the current directory

-ltest: When the compiler looks for a dynamic connection library, there is an implied naming rule, that is, 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 goal, but if you do not have root permissions, then you can only use the output Ld_library_path method.

Example:
Compilation and use of dynamic link library *.so--


Dynamic library *.so is often encountered when programming in C and C + + under Linux. Recently found a few articles in the Web site to introduce dynamic library compilation and links, finally understand this has not been very understanding of dongdong, here to make a note, but also for the dynamic Library link library and distressed brothers to provide a little help.
1, the compilation of dynamic library

The following is an example of how to generate 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 several files into a dynamic library: libtest.so.

So_test.h:



#include <stdio.h>

#include <stdlib.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_A.C:



#include "So_test.h"



void Test_c ()

{

printf ("This is in test_c...\n");

}



Compile these several files into a dynamic library: libtest.so



$ gcc test_a.c test_b.c test_c.c-fpic-shared-o libtest.so


2, Dynamic library links

In 1, we have successfully generated our own dynamic link library libtest.so, we use 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 ();



return 0;

}



L deliver the test.c and dynamic library libtest.so chain into the execution file test:



$ gcc test.c-l.-ltest-o Test



L test whether the dynamic connection, if listed libtest.so, then should be connected to the normal



$ LDD Test



L Execute test to see how it calls a function in a dynamic library.
3, compile parameter analysis
The main thing is an option for the GCC command line:
-shared This option specifies that a dynamic connection library is generated (which allows the connector to generate an export symbol table of type T, and sometimes a weakly connected W-type export symbol) without which the external program cannot connect. Equivalent to an executable file

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

L-l.: Indicates the library to connect to in the current directory

L-ltest: When the compiler looks for a dynamic connection library, there is an implied naming rule, that is, precede the given name with Lib, followed by. So to determine the name of the library

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

L 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 you do not have root permissions, then only the output Ld_library_path method.
4. Note

When you call a dynamic library, there are a few problems that are often encountered, sometimes, it is clear that the library's header file directory through the "-I" included, the library file is guided by the "-L" parameter, and specified the "-L" library name, but through the LDD command to see if you can not find the link you specify the so file , all you have to do is modify the Ld_library_path or/etc/ld.so.conf file to specify the directory of the Dynamic library. This is usually done to solve the problem that the library cannot be linked.

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.