Static libraries and dynamic libraries under Linux

Source: Internet
Author: User

Tag:echo   test    startup    lib   usr     Environment Variables    inux   shared    no    

The code of the called function in the static library   static library under Linux will be copied to the executable file together at compile time!! Executable files do not need static libraries to be present!   II. Construction and use of dynamic libraries under Linux 1. Building the dynamic library    gcc -fpic -shared   -o  Lib library name .so    Source file List   Example:   gcc   -fPIC -shared -o  libmylib.so  max.c  min.c  2, dynamic library use    gcc   -o  final executable   source file list  -L Dynamic Library catalog  -L library name   Example: int   Getmax (Int,int) and Getmin (Int,int) are packaged in libmylib.so, and now you can copy libmylib.so to the people who want to use your library function!!   people using your library functions may return to this using:   //test.c          int Getmax (int,int);          INT main () {int x,y; int max;scanf ("%d%d", &x,&y), Max=getmax (x, y),//getmax is already implemented in your library function, So no one else in the realization, directly to use the line!! printf ("max=%d\n", Max), return 0;}  executes the command:gcc -o  test  test.c will error: prompt cannot find getmax; correct command:gcc -o   Test test.c -l libmylib.so stored directory    -lmylib error notation:gcc -o  test test.c -L libmylib.so Storage Directory   -llibmylib.so   View labels in a dynamic library:readelf -s  Dynamic Library name    view the dynamic library on which the executable is run: (1) LDD   Executable file name (2) readelf -d    executable file name |grep  shared 3, dynamically linked executable (optional)): The solution is to the executable file at runtime to find the dynamic Library (1) to copy the dynamic library into the/lib or/usr/lib directory; (in embedded development, recommend this method!) )       command: Correct:sudo  cp  libmylib.so /lib/  error:sudo  cp  libmylib.so / lib/mylib.so error:sudo  cp  libmylib.so /lib/libmylib (2) Add Dynamic directory path to environment variable ld_library_path      For example:          export  ld_library_path= Dynamic Library in the same directory: $LD _library_path      If Ld_library_path is required to be active, add the above statement to the end of the ~/.BASHRC file.       Command:gedit   ~/.BASHRC lazy approach (with caution):echo  export ld_library_path= $PWD: \ $LD _library_ PATH>>~/.BASHRC (3) Add the dynamic directory to the/etc/ld.so.conf, the boot will automatically load/etc/ld.so.conf matching files, if necessary to take effect immediately, execute command: ldconfig command: sudo gedit /etc/ld.so.conf  Third, static library, dynamic library use of the difference in the static library called code to generate the executable file will be immediately copied to the execution of the file, the dynamic library called code will be loaded when the executable file is runto the executable file.

Static libraries and dynamic libraries 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.