Dynamic library/Static library (dependent) under Linux

Source: Internet
Author: User

A dynamic library based on dynamic library

Libfun.so relies on dynamic library libtest.so (the function intnothing () in the libfun.so Dynamic library calls the libtest.so () function in intmytest), And Main.exe called the libfun.so function int nothing ().

Here are two ways to work:

Common ways of 1>

g++-shared-fpic-o libtest.so Test.cpp

g++-shared-fpic-o libfun.so function.cpp-l./-i./-ltest---Here ldd can see libfun.so dependent libtest.so ( LDD may need to be in the libfun.so directory to perform the export ld_library_path=./)

g++-o main.exe main.cpp-l./-i./-lfun---Compile libfun.so when you have specified a dependency libtest.so, there is no need to specify

Or

2> not used in the usual way

g++-shared-fpic-olibtest.so Test.cpp

g++-shared-fpic-o libfun.so function.cpp---Here ldd no dependency libfun.so

g++-o main.exe main.cpp-l./-i./-lfun-ltest---because the compile libfun.so did not specify that it depends on libtest.so, so add-ltest

From 2>, when the dynamic library generated under Linux, even if there is no function defined in the dynamic library, when compiling the link, the default will not prompt the error, is able to pass.

However, an error occurs when the executable file links to a dynamic library with undefined functions . Specific explanations can be viewed through the man LD.

The test found that, whether in a static library or a dynamic library, you can only declare a function and not define this function, but can call this undefined function in the library, the creation of the library can be successful, the original always thought that only static libraries can be used, because the static library generation is not required to link. It is only now known that dynamic library static can be used in this way.

Ii. Dynamic libraries/static libraries dependent on dynamic library/static Library

A static library is just a collection of object objects, and using the AR command, you can package the. o file generated by the compilation into a static library.

Generating static libraries is compiled without links, while dynamic libraries are generated with both compiled and linked actions.

When a static library is linked by another program (executable program or dynamic Library), the linker copies the code that is used in the program from the static library file to the application.

The static library is generated without a link, so when it is generated it does not need to specify the external library on which it depends, and the dynamic library generates a link action, which of course specifies which external libraries it depends on.

Such as:

$ gcc-c Libhello-o libhello.o
$ ar RCS LIBHELLO.A libhello.o

This compiles the. o File and then packs it with the AR command to get the static library, which is no link action.

As described in, if a dynamic library relies on a dynamic library, the dynamic library will know the dynamic libraries it relies on. So, what if a dynamic library relies on a static library? At this point, when the dynamic library is generated, it will copy the code of the static library into the dynamic Library program. For example, Liba.so relies on LIBB.A, you must specify a dependent static library LIBB.A in makefile when generating liba.so, and you want to specify LIBB.A as the lookup directory. At the same time if the executable program C.out dependent on liba.so, then a.out only need to point out that it relies on the dynamic library liba.so, a.out do not need to link to the static library LIBB.A.

Also, does a static library depend on a static library/dynamic library? As described above, when a static library is generated, it is not linked, so you do not need to specify the external static library or the dynamic library on which it depends. As a result, it is possible to specify a dependent library that links to the static library whenever the static library is linked.

Dynamic library/Static library (dependent) 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.