Compile static and dynamic libraries in Linux

Source: Internet
Author: User
In a Linux operating system, compiling static libraries and dynamic library Linux library files are divided into two types: static library (*. file A, which is equivalent *. lib file), the other is the dynamic library (*. so file, which is equivalent *. DLL file ). During compilation, one method is to compile your own. o file (equivalent to Windows. object files) and static libraries are linked to executable files, the other is to compile only. o file. When running, the function in the dynamic library is called, and then loaded into the dynamic library. The compilation method is as follows: 1. If the source file of the static library is main. C and mystatic. C. 1) Compile mystatic. c into the static library libmystatic. A, and call the functions in this library in Main. C. Gcc-c-I inlcude-O mystaic. O mystatic. C // set mystatic. c. compile it into the target file mystatic. O ar-Rs lib/libmystaic. A mystatic. O // set the target mystaic. O adds libmystatic. library A (the Library file must start with LIB), if libmystatic. A does not exist. Create this database. 2. C is compiled into an executable file test. Gcc-I include-l Lib-O test main. c-lmystatic-static is compiled successfully, and libmystatic is generated successfully under the folder Lib. A library file, and the compiled executable program test uses the library libmystatic. A 2. the source file of the dynamic library is: Main. c, mydynamic. c. 1) Compile mydynamic. c into the dynamic library libmydynamic. So, and call the functions in this library in Main. C. The GCC-I include-shared-O lib/libmydynamic. So mydynamic. C // library file must start with LIB 2) Compile main. c into an executable file test. Gcc-I include-l Lib-O test main. C-lmydynamic // without the parameter-static, the default value is dynamic compilation. 3) run the executable program test. When you run the test program, the system searches for mydynamic. So in the default library file directory. Add the compiled dynamic library directory to the system. The specific method is as follows: Modify ~ /. Bash_profile (this file is a hidden file, use LS-~ /Can be displayed.) add LD_LIBRARY_PATH = *** # (*** is libmydynamic. so directory) Export LD_LIBRARY_PATH: log on to the system again and run the executable file test.

3. Use the dlopen dlsynm dlerror dlclose function in the shared library DL during compilation.

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.