Linux: Learn how to create static libraries, dynamic libraries, simple makefile writing, and linuxmakefile

Source: Internet
Author: User

Linux: Learn how to create static libraries, dynamic libraries, simple makefile writing, and linuxmakefile

Create a static library
Create four file bin (Executable File), lib (Library), include (header file), src (put source file)

The library started here is add

Run

1) gcc-c add. c // compile the add. c source file to generate the add. o target file.
2) ar crsv ../lib/libadd. a add. o // archive the target file *. o, generate lib *. a, and put the generated file in lib.
3) gcc-o main. c-L (the path for storing the Library ).. /lib-l (lower-case doll, Database Name: add) add-I (upper-case I ).. /include (put the header file)


Create a dynamic library
Gcc-fPIC-c add. c ../include (put the header file) to generate the add. o target file
Gcc-shared-o libadd. so add. o generate libadd. so dynamic library. The Library name is add
Put this library in the lib file in the root directory
Gcc-o main. c-l add ../include (put the header file) (note the library name written here, without lib and. so)


Comparison between dynamic and static libraries:

The dynamic library is linked only when it is executed.

A dynamic library can be used by multiple programs, so it is also called a shared library.

Static libraries will be integrated into large programs, and program execution will not be clipped to static libraries. Therefore, static libraries will be difficult to upgrade due to bloated programs, but it is easier to deploy

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


Use makefile

Three file bins (executable files), include (header files), and src (put source files)

Target item dependency
./Bin/main:./src/my_add.o./src/my_minus.o./src/main. o // generate an executable file for the *. o file
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




Multi-level directory makefile, static library

Compile the files that need to generate the dynamic library in the lib directory, generate the dynamic library, and install it in the standard library of the system
Program call. The procedure is as follows:
(1) Compile the Makefile. am File
AUTOMAKE_OPTIONS = foreign
Lib_LTLIBRARIES = libhello. la
Libhello_la_SOURCES = test. c
Here, lib_LTLIBRARIES indicates the generated dynamic library, and then specifies the source file that the dynamic library depends on.
Test. c. Multiple source files are separated by spaces.
(2) in the lib directory, use the autoscan command to generate the configure. scan file and change it to configure. in. This
The macro AC_PROG_LIBTOOL must be added, indicating that libtool is used to automatically generate a dynamic library.
# Configure. in
# Process this file with autoconf to produce a configure script.
AC_PREREQ (2.59)
AC_INIT (hello, 1.0, [miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR ([test. c])
# AC_CONFIG_HEADER ([config. h])
# Checks for programs.
AC_PROG_CC
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_PROG_LIBTOOL
AC_CONFIG_FILES ([Makefile])
AC_OUTPUT
(3) execute commands such as aclocal, libtoolize-f-c, autoconf, automake -- add-missing,./configure,
Make and make install the dynamic library to the standard library of the system for calling (generally/usr/local/lib ).
Note: libtoolize provides a standard method to add libtool support to a software package, while GNU libtool is
A common library supports scripts to hide the complexity of dynamic libraries in a unified and portable interface.
4. Generate the hello executable file under the src directory
(1) write the src/Makefile. am File
AUTOMAKE_OPTIONS = foreign
Primary des =-I ../include
Bin_PROGRAMS = hello
Hello_SOURCES = hello. c
Hello_LDADD =-lhello
-Ldir specifies the path of the database to be searched during compilation. Unlike static databases, you do not need to specify the library path when creating a dynamic library.
Path, the compiler automatically searches for the libhello. so file in the standard library.
(2) execute autoscan to generate the configure. scan file, rename it as configure. in, and modify its content.
# Configure. in
# Process this file with autoconf to produce a configure script.
AC_PREREQ (2.59)
AC_INIT (hello, 1.0, [miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR ([hello. c])
# AC_CONFIG_HEADER ([con... the remaining full text>
 
How does one link a static library when writing makefile in linux?

-Llibjpeg. so // Connection Library libjpeg. so
-L/usr/lib // specify the library file path

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.