System Programmer growth plan-Engineering Management (III)

Source: Internet
Author: User
Tags gtk automake

Indicate the source and author's contact information during reprinting.

Article Source: http://www.limodev.cn/blog
Author contact: Li xianjing <xianjimli@gmail.com>

 

System Programmer growth plan-Engineering Management (III)

Function library

Now we use automake to manage the previously created function library. This is a basic function library. Let's name it base.

O directory structure
Base root directory
Base/SRC Source Code directory

O create a makefile Template
The base/makefile. am content is:
Subdirs = SRC

The base/src/makefile. am content is:

lib_LTLIBRARIES=libbase.lalibbase_la_SOURCES= darray.c /    darray.h /    dlist.c /    dlist.h /    darray_iterator.h /    dlist_iterator.h /    hash_table.c /    hash_table.h /    invert.c /    iterator.h /    linear_container_darray.c /    linear_container_darray.h /    linear_container_dlist.c /    linear_container_dlist.h /    linear_container.h /    queue.c /    queue.h /    sort.c /    sort.h /    stack.c /    stack.h /    typedef.hlibbase_la_LDFLAGS=-lpthreadnoinst_PROGRAMS=darray_test dlist_testdarray_test_SOURCES=darray.cdarray_test_CFLAGS=-DDARRAY_TESTdlist_test_SOURCES=dlist.cdlist_test_CFLAGS=-DDLIST_TESTbasedir=$(includedir)/basebase_HEADERS=darray.h dlist.h iterator.h linear_container_dlist.h typedef.h /    darray_iterator.h  dlist_iterator.h  linear_container_darray.h  /    linear_container.hEXTRA_DIST=/    linear_container_test.c /    invert_ng.c /    darray_iterator.c /    dlist_iterator.c /    test_helper.c

Ltlibraries is the keyword. Lt indicates libtool. libtool is a script used to encapsulate the differences between shared libraries on different platforms. We don't need to care about its implementation.

Libbase. La is the name of the function library. The extension uses. La instead of. So or. A, and both the shared library and static library are generated. Libbase_la_sources is the source file required to generate libbase. La.
Ldflags is a keyword used to specify the parameters required for a link.-lpthread indicates the link libpthread. So.

Noinst_programs is a keyword that indicates an executable file that does not need to be installed, usually a test program. For simplicity and clarity, not all test programs are written here.

Cflags is a keyword used to specify parameters during compilation and preprocessing.

Headers is a keyword that lists the header files to be installed. Xxx_headers and xxxdir must be used together. The latter indicates the location to be installed. The header files listed in base_headers will be installed in the basedir directory.

O create an Autoconf template.
Run:
Autoscan
MV Configure. Scan Configure. In

Modify Configure. In as described earlier to get the following content:

AC_PREREQ(2.61)AC_INIT(base, 0.1, xianjimli@hotmail.com)AC_CONFIG_SRCDIR([src/invert.c])AC_CONFIG_HEADER([config.h])AM_INIT_AUTOMAKE(base, 0.1)# Checks for programs.AC_PROG_CCAC_PROG_LIBTOOL # Checks for libraries.# FIXME: Replace `main' with a function in `-lpthread':AC_CHECK_LIB([pthread], [main]) # Checks for header files.AC_HEADER_STDCAC_CHECK_HEADERS([stdlib.h string.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics.AC_C_INLINEAC_TYPE_SIZE_T # Checks for library functions.AC_FUNC_MALLOCAC_FUNC_REALLOC AC_CONFIG_FILES([Makefile                 src/Makefile])AC_OUTPUT

Different from the above:
Ac_prog_libtool is used to check the libtool script.
Ac_check_lib is used to check whether the shared library exists.
Ac_check_headers is used to check whether the header file exists.
Ac_func_malloc is used to check whether the standard malloc function exists.

O collects the M4 macros used.
Run: aclocal

O generate a configuration header file template.
Run: autoheader

O create README, news, changelog, and authors files.

O generate the file required by libtool.
Run: libtoolize-force-copy

The main function of this command is to generate ltmain. Sh, while ltmain. Sh is used to generate libtool scripts.

O generate makefile. in and the required script.
Run: automake-

O generate the configure script.
Run: Autoconf

O generate the final makefile.
Run:./configure-Prefix = $ home/usr

O Compile and run: Make

O Installation
Run: make install

O release software packages
Run: Make Dist

The compiled file is installed in the/home/lixianjing/usr/lib/directory:
Libbase. A libbase. La libbase. So libbase. so.0 libbase. so.0.0.0

Static Library: libbase.
Dynamic library: libbase. So
Libtool packaging: libbase. La

After the header file and library are installed, the caller still needs to know the following information before using them:

Where are header files and libraries installed?
Which other modules are dependent on?

To solve this problem, we need to use another tool named PKG-config. PKG is short for package. PKG-config is used to query the configuration information of a specified package, such as the package name, description, version number, header file, library, and dependency. To enable PKG-config to work normally, the implementer of the software package must provide a configuration file with the extension of PC.

In the system, the PKG-config configuration file is usually stored under/usr/lib/pkgconfig/and/usr/local/lib/pkgconfig/. below is GTK +-2.0.pc:

prefix=/usrexec_prefix=/usrlibdir=/usr/libincludedir=/usr/includetarget=x11 gtk_binary_version=2.10.0gtk_host=i386-redhat-linux-gnu Name: GTK+Description: GIMP Tool Kit (${target} target)Version: 2.12.10Requires: gdk-${target}-2.0 atk cairoLibs: -L${libdir} -lgtk-${target}-2.0Cflags: -I${includedir}/gtk-2.0

The previous part is some Defined variables, followed by some keywords:

Name: Name
Description: Function Description
Version: Version Number
Requires: the dependent Software Package
Libs: the caller's Link parameter.
Cflags: the compilation parameter of the caller.

Because prefix and other variables are determined only when the software package is configure, they cannot be directly written to the PC file. We can generate configure Based on the template file.

The template file name is base. PC. in and the content is:

prefix=@prefix@exec_prefix=${prefix}libdir=${prefix}/libincludedir=${prefix}/include Name: @PACKAGE_NAME@Description: a basic library.Version: @VERSION@Requires:Libs: -L${libdir} -lbaseCflags: -I${includedir}/base

This template file is the same as the replacement rule of makefile. In. Variables enclosed by two @ are replaced with the values detected by Configure. variables such as @ prefix @ are standard variables.

Modify base/makefile. Am and add the following two lines of code:

Pkgconfigdir =$ {libdir}/pkgconfig
Pkgconfig_data = base. PC

This is the method for installing the data file. pkgconfig is not a keyword. Just take a descriptive name. Dir and _ data are keywords with the same prefix. The former indicates the directory to be installed, and the latter indicates the file to be installed. By convention, the PC file is installed under $ {libdir}/pkgconfig.

Modify Configure. In and add the input file base. PC.
Ac_output ([base. PC])

Put it in ac_config_files. It also tells the file to be generated by the configure script.

After you run configure again, the base. PC is generated with the following content:

prefix=/home/lixianjing/usr/localexec_prefix=${prefix}libdir=${prefix}/libincludedir=${prefix}/includeName: baseDescription: a basic library.Version: 0.1Requires:Libs: -L${libdir} -lbaseCflags: -I${includedir}/base

(The prefix parameter is the same as that specified in configure .)

In the next section, we will learn how callers use PC files.

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.