Linux under GCC compile generate dynamic link library *.so file and call it reproduced

Source: Internet
Author: User
Tags naming convention

Dynamic library *.so in Linux under the C and C + + programming often encounter, recently found a few articles on the Web site to introduce the compilation of dynamic library and links, finally understand this has not been familiar with the east, here to make a note, but also for other dynamic library linked to the library and distressed brothers to provide a little help.
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 was in test_a...\n");
}


TEST_B.C:
#include "So_test.h"
void Test_b ()
{
printf ("This was in test_b...\n");
}



TEST_C.C:
#include "So_test.h"
void Test_c ()
{
printf ("This was 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 ();
return 0;
}
TEST.C and dynamic library libtest.so link to execute file test:
$ gcc test.c-l.-ltest-o Test
Test whether the connection is dynamic, if the libtest.so is listed, then the connection should be OK
$ 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:
Export Ld_library_path=/opt/au1200_rm/build_tools/bin: $LD _library_path: 
and then enter: Export
will show whether it is set correctly
Export mode expires after a restart, so you can also use vim/etc/   Bashrc  Modify the Ld_library_path variable in it.
For example: Ld_library_path= $LD _library_path:/opt/au1200_rm/build_tools/bin.

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.