Linux Learning to build static libraries, dynamic libraries, write simple makefile

Source: Internet
Author: User

Create a static library
Build four files Bin (executable file), Lib (library), include (header file), src (release source file)

Here's the cumming for add

Execute in src file

1) gcc-c ADD.C//compile ADD.C source file to generate ADD.O target file
2) AR crsv. /LIB/LIBADD.A ADD.O//Archive The target file *.o, generate the LIB*.A, and put the generated file in Lib
3) Gcc-o main main.c-l (Larger love dolls, the path of the library). /lib–l (Lowercase love puppet, library named Add) add–i (uppercase I): /include (Put header file)


Create a dynamic library
Gcc-fpic-c ADD.C. /include (drop header file) generate ADD.O target file
Gcc-shared-o libadd.so ADD.O generate libadd.so Dynamic Library The name of the library is add
Place this library in the Lib file of the root directory
Gcc-o Main main.c–l Add: /include (Put header file) (note the library name written here, no Lib and. So)


Dynamic library vs. static library comparison:

The dynamic library is only used for linking when it is executed.

Dynamic libraries can be used by multiple programs, so also known as shared libraries

Static libraries will be integrated in large programs, program execution is not clamped in the static library, so the static library will be cumbersome to upgrade the program, but easier to deploy

Dynamic libraries are easy to upgrade, but not easy to deploy


Using makefile

Three file bin (executable file), include (header file), src (drop source file)

Target Item Dependencies
./bin/main:./src/my_add.o./src/my_minus.o./SRC/MAIN.O//To generate an executable file for *.O files
g++./src/my_add.o./SRC/MY_MINUS.O/src/main.o-o./bin/main-i./include
./src/my_add.o:./src/my_add.cpp
g++-C/src/my_add.cpp-o/src/my_add.o-i./include//Generate MY_ADD.O
./src/my_minus.o:./src/my_minus.cpp
g++-C/src/my_minus.cpp-o/src/my_minus.o-i./include//Generate MY_MINUS.O
./src/main.o:./src/main.cpp
g++ -C/src/main.cpp-o/src/main.o-i./include//Generate MAIN.O



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.