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

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"
voidtest_a ();
voidtest_b ();
Voidtest_c ();

//TEST_A.C:
#include "so_test.h"
voidtest_a ()
{
printf ("This was in test_a...\n");
}


//TEST_B.C:
#include "so_test.h"
Voidtest_b ()
{
printf ("This was in test_b...\n");
}



//TEST_C.C:
#include "so_test.h"
Voidtest_c ()
{
printf ("This was in test_c...\n");
}
compile these files into a dynamic library: libtest.so
$ gcctest_a.c TEST_B.C test_c.c-fpic-shared-olibtest.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"
Intmain ()
{
test_a ();
test_b ();
Test_c ();
Return0;
}
test.c and dynamic library libtest.so link to execute file test:
$ gcctest.c-l.-ltest-o Test
test whether the connection is dynamic, if the libtest.so is listed, then the connection should be OK
$ lddtest
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:
-sharedThis option specifies to generate the 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).the external program cannot connect without the flag. Equivalent to an executable file

-fpic: Indicates that compiled code is location- independent, so the compiled codes are location-dependent, so dynamic loading is done by copying the code to meet the needs of different processes, rather than achieving the purpose of true code snippet 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's just that you can't find the so file that you specified the link to, and you do this by modifying the Ld_library_path or/etc/ld.so.conf file to specify the directory for the Dynamic library. This is usually done to solve the problem where the library cannot be linked.

You can use the Export command to set this value under Linux,under the Linux terminal, enter:
exportld_library_path=/opt/au1200_rm/build_tools/bin: $LD _library_path: 
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: Ld_library_path= $LD _library_path:/opt/au1200_rm/build_tools/bin.

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

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.