Using GCC to compile and generate a dynamic link library *. So file

Source: Internet
Author: User

In Linux, the file type does not depend on its suffix, but generally speaking:
. O is the target file, which is equivalent to the. OBJ file in windows.
. So is a shared library and is a shared object, used for dynamic connection, similar to DLL
. A is a static database and multiple. O files are combined for static connections.
. LA is a shared library automatically generated by libtool. It is edited and viewed by VI and mainly records some configuration information. You can use the following command to view *. La file format $ file *. La
*. La: ASCII English text

 

Dynamic library *. so is often encountered in C and C ++ programming in Linux. Recently, I have found several articles on the website to introduce the compilation and link of dynamic libraries, after finally understanding this, I have never been quite familiar with it. Here, I will take a note and provide some help to other brothers who are worried about the dynamic Library Link Library.

1. Dynamic library Compilation

The following example shows how to generate a dynamic library. Here is a head file: so_test.h, three. c files: test_a.c, test_ B .c, and test_c.c. We compile these 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 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 a dynamic link library libtest. So. Next we use a program to call the functions in this library. The source file of the program is test. C.

Test. C:
# Include "so_test.h"
Int main ()
{
Test_a ();
Test_ B ();
Test_c ();
Return 0;
}

L

Link test. C to the dynamic library libtest. So to generate the execution file test:

$ GCC test. C-L.-ltest-o Test

L test whether the connection is dynamic. If libtest. So is listed, the connection is normal.

$ LDD Test

L execute test and you can see how it calls functions in the dynamic library.

3. Compile Parameter Parsing
The most important option is the GCC command line:

-Shared
This option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file

L

-FPIC: indicates that the compiled code is in a separate position. If this option is not used, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.

L

-L.: indicates that the database to be connected is in the current directory.

L

-Ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.

L

LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.

L

If you have the root permission, you can modify the/etc/lD. So. conf file and then call
/Sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only output LD_LIBRARY_PATH.

4. Note


When calling a dynamic library, there are several problems that may occur frequently. Sometimes, it is clear that the directory where the header file of the library is located has been passed "-I"
Include.
The "-l" parameter is guided and the "-l" library name is specified. However, when you run the LDD command to check whether the so file with the specified link is missing, in this case, you need to modify LD_LIBRARY_PATH or/etc/lD. so. CONF file to specify the directory of the dynamic library. This usually solves the problem that the database 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.