GCC make static library and dynamic Library summary

Source: Internet
Author: User
making static libraries and dynamic libraries under Linux

Directory structure:

File contents:

Main.c
#include <stdio.h>
#include ". /include/add.h "
int main ()
{
    printf ("%d ", add (1,2));
____________________________________________
//add.c
#include ". /include/add.h '
int Add (int a, int  b)
{return
    a+b;   
}
______________________________________________
//add.h
int Add (int a, int  b)
Dynamic Library production (. So)
Gcc-shared-fpic-o lib/libadd.so SRC/ADD.C

or

Gcc-fpic-c src/add.c    
Gcc-shared-o libadd.so  

using shared libraries

GCC Src/main.c-llib-ladd-o Main                                                                                    

Run an error:

./main:error while loading shared libraries:libadd.so:cannot open Shared object file:no such file or directory

Solution Summary :
1. Add-wl and Rpath

2. Modify Ld_library_path

Export Ld_library_path=~/test/lib: $LD _library_path

3. Use Ldconfig

Put the library on the standard path
CP ~/test/lib/libadd.so/usr/lib
chmod 0775/usr/lib/libadd.so
/update cache
ldconfig
gcc Src/main.c-llib-ladd-o Main       

-fpic: Compiling a dynamic library must, output the code that does not depend on the location
-shared: Compile dynamic library must options
-wi: Pass some parameters to the linker static library production

Gcc-c src/add.c-o ADD.O           //Make obj file
ar-crv lib/libadd.a add.o          //Archive file
gcc src/main.c-llib-ladd-o ma In  //plus L (indicates link directory) L (indicates link library name)

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.