Linux dynamic Static Library use

Source: Internet
Author: User

#include <stdio.h> #include "cac.h" int main (void) {    printf ("add =%d\n", add (5,3));    printf ("Sub =%d\n", sub (5,3));    return 0;}

int add (int a,int b) {    return a+b;}

int sub (int a,int b) {return a-a    ;}

int add (int a,int b); int sub (int a,int b);

Generation of dynamic libraries
1) The target file is generated first, but at this point the compiler option-FPIC and linker options-shared are added,
g++-fpic-c ADD.C
g++-fpic-c SUB.C
Generate intermediate files ADD.O and SUB.O
2) next Generate dynamic library
g++-shared–o libcac.so ADD.O sub.o

Libcac.so is the target dynamic library we generate. We use dynamic libraries and main.cpp programs to generate executable programs later
The above two parts can also be synthesized one step:
g++-fpic-shared add.cpp Sub.cpp-o libcac.so

Using the dynamic link library
When compiling a program, it is consistent to use a dynamic-link library and a static library, using the "-L Library Name" method to link the library file when the executable file is generated.
1) Use the command:
Gcc-o main main.c-l./-LCAC
2)-l specifies the dynamic link library's-LCAC, which is the calling rule for dynamic libraries. The dynamic library naming method under the Linux system is lib*.so, while the link indicates that bit-l*,* is the name of the library itself.
3) However, the program will prompt the following error while loading shared Libraries:libtiger.so:cannot open Shared object file:no such file or direct
This is due to the fact that the program did not find a dynamic link library when it ran. The concept of linking a dynamic library to a runtime using a dynamic-link library at compile time is different, and at run time, the dynamic link library that the program links to needs to be in the system directory.
4) A. The most convenient solution under Linux is to copy the libtiger.so to the absolute directory/lib (however, if the superuser can, so use sudo oh, pro). You can generate an executable program.
B. The second method is to place the directory of the dynamic-link library in the program search path, which can be added to the environment variable Ld_library_path:
Export ld_library_path= ' pwd ': $LD _library_path You can also generate an executable program after you execute this command

Generate a static library

AR cr libcac.a add.o sub.o

Using a static library

g++-o main main.cpp-l. -lcac

Linux dynamic Static Library use

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.