Making a static library into a dynamic library

Source: Internet
Author: User

First, System description

UBUNTU12.04TLS 64-bit


Second, the production of Static library

(1) To write a program that needs to be made into a static library (according to the actual situation, this is just a simple example)

Bar.h #ifndef _bar_h #define _BAR_H void BAR (int i); #endif

Bar.c #include <stdio.h> #include <stdlib.h> #include "bar.h" void bar (int i) {PR intf ("hello!    I ' m bar, i=%d\n ", i); }

Foo.h #ifndef _foo_h #define _FOO_H void FOO (int i); #endif

foo.c #include <stdio.h> #include <stdlib.h> #include "foo.h" void foo (int i) {PR intf ("hello!    I ' m foo, i=%d\n ", i); }

(2) make a static library

Gcc-fpic-c foo.c bar.c ar rcs LIBSTATICLIB.A foo.o bar.o

Compilation generates FOO.O and BAR.O, resulting in LIBSTATICLIB.A

One of the most important is the -fpic parameter, if there is no such parameter, if it is 32-bit system in the production of dynamic library, there is no problem, but 64-bit system has a problem, the production of dynamic library will be

Error:

/usr/bin/ld:foo.o:relocation r_x86_64_32 against '. Rodata ' can not is used when making a shared O Bject; Recompile with-fpic
Foo.o:could not read Symbols:bad value
COLLECT2:LD return 1
Therefore, the static library is made into a dynamic library on a 64-bit machine, and the-fpic parameter must be used


(3) Simple test

Main.c #include <stdio.h> #include <stdlib.h> #include "foo.h" #include "bar.h" int m        Ain () {int i = 9;            int J = 8;        Foo (i);            Bar (j);    return 0; }

GCC main.c-o test-l.-lstaticlib./test

Results:

Hello! I ' m foo, i=9
Hello! I ' m bar, i=8

Third, make the dynamic library with the static library

(1) Extract the static library from the previous step to get the target file

Ar-x LIBSTATICLIB.A

will get target files foo.o and BAR.O

(2) Making the target file into a dynamic library

Gcc-fpic-shared-o libsharedlib.so foo.o BAR.O

Generate a dynamic library libsharedlib.so

(3) Type the dynamic library path under the load path

Export ld_library_path=.: $LD _library_path

This is only temporary, the terminal shutdown is invalid, want to take effect permanently, modify the configuration file

(3) Simple test

GCC main.c-o test-lsharedlib./test

Results:

Hello! I ' m foo, i=9
Hello! I ' m bar, i=8


Iv. Summary

(1) The-fpic parameter is not only used when making a dynamic library, but when the static library is made into a dynamic library on a 64-bit machine, this parameter is used when compiling the static library.

(2) The dynamic library needs the path of the braking load, so the load path needs to be configured

(3) Some advantages and disadvantages of dynamic library and Static library

Each program in memory will have a copy of the code, while the dynamic library has only a single copy in memory

Static library compiled programs can be directly ported to run elsewhere, while the dynamic library program will not be able to execute because the link library cannot be found

The static library compiles a large program size

This article is from the "Snow Dancer" blog, please be sure to keep this source http://happytree007.blog.51cto.com/6335296/1708996

Making a static library into a dynamic library

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.