Automake: compile a complete project-dynamic library + Executable File

Source: Internet
Author: User
Tags automake

Automake: compile a complete project-dynamic library + Executable File

The previous blog automake briefly introduced the automake usage process, but the real project must be very complicated, including different directories, some generating dynamic libraries, some executable files, this article uses a simple opencv project to introduce how to use automake in large projects. In this article, the outermost layer contains two directories. One directory is src, which contains the source files of different modules, and the last is to generate a dynamic library. The other directory is sample, which is an application, call the dynamic library in src to generate an executable file, while src contains different directories. Each subdirectory finally generates an so dynamic library, as shown below, smooth, sharpen, and segment generate three dynamic libraries respectively, while processManage calls the three of them to generate a dynamic library, finally, the main function in the sample calls the processManage interface to indirectly call the smooth, sharpen, and segment dynamic libraries to implement an application:

The procedure is as follows:

1. Execute the autoscan command in the outermost directory (same as src and sample) to generate the autoscan. log and configure. scan files.

2. mv configure. scan configure. ac and modify configure. ac (configure. in was used earlier)


#-*-Autoconf-*-# Process this file with autoconf to produce a configure script. AC_PREREQ ([2.68]) AC_INIT (segment, 1.0, email) AM_INIT_AUTOMAKE (segment, 1.0) # manually add AC_CONFIG_SRCDIR ([sample/segment. cpp]) # used to check the validity of the source code directory. You can select any source code file to AC_CONFIG_HEADERS ([config. h]) # Checks for programs. AC_PROG_CXXAC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES ([Makefile src/smooth/Makefile src/sharpen/Makefile src/segment/Makefile src/processManage/Makefile sample/Makefile]) # All makefiles to be generated AC_OUTPUT


3. Use aclocal to generate the aclocal. m4 File

4. Run the autoconf command to generate the configure file.

5. Run the autoheader command to generate config. h and config. h. in.

6. Create the Makefile. am file in each directory where Makefile needs to be generated.

6.1: if it is a parent directory (the same level as src and sample), it must contain SUBDIRS = src sample (pay attention to the order), which is equivalent to calling the subdirectory to generate Makefile. If there are no other operations, only this row is allowed. similarly, Makefile in src (same as smooth, sharpen, segment, processManage. am also has only one row, I .e. SUBDIRS = smooth sharpen segment processManage. A dynamic library is generated for Makefile. am in the three directories of smooth, sharpen, and segment. The method is similar. Makefile. am in the smooth directory is provided here:


Primary des = 'pkg-config opencv -- cflags '-I. /# projectlibdir = $ (libdir) projectlib_PROGRAMS = libsmooth. so # Name of the dynamic library libsmooth_so_SOURCES = GaussSmooth. cpp MedianSmooth. cpp GaussSmooth. h Smooth. hlibsmooth_so_LDFLAGS =-shared-fpic # GCC dynamic library compilation Option
For more information about the macro, see the previous blog table.


For Makefile. am in processManage, the call for the other three so statements is slightly different. The details are as follows:


Primary des = 'pkg-config opencv -- cflags '-I .. /smooth/-I .. /sharpen-I .. /segment/-I. /projectlibdir = $ (libdir) projectlib_PROGRAMS = libmanage. solibmanage_so_SOURCES = processManage. cpp processManage. hlibmanage_so_LDFLAGS =-shared-fpic # The GCC dynamic library compilation option libmanage_so_LDADD =-L .. /smooth/-L .. /sharpen/-L .. /segment/-lsmooth-lsharpen-lsegment # dependent Library
The Makefile. am of the executable files in the sample is shown as follows:



AUTOMAKE_OPTIONS = foreignINCLUDES=`pkg-config opencv --cflags` -I../src/processManage/ -I../src/smooth/ -I../src/sharpen/ -I../src/segment/ -I. bin_PROGRAMS = segmentsegment_SOURCES = segment.cppLIBS = `pkg-config opencv --libs` segment_LDADD =-L../src/smooth/ -L../src/sharpen/ -L../src/segment/ -L../src/processManage/  -lsmooth -lsharpen -lsegment -lmanage   
Note: Because the dynamic library dependent on GCC must be placed behind the-o parameter, 'pkg-config opencv -- libs' must be placed in the libs variable, the Makefile generated later shows that _ LDADD and _ LIBS are behind-o, while _ LDFLAGS is behind-o, 'pkg-config opencv -- libs' cannot be placed in _ LDADD.
6.2: If you use libtool to generate a dynamic library, you can use libtool to generate a portable dynamic library. The generated dynamic library is. the lo file, which calls the subdirectory in its own directory. in/libs. so file, use. lo file and usage. the so file method is similar, but you only need to use the-lz parameter. The template is as follows (a simple project example is used below ):



AUTOMAKE_OPTIONS = foreignlib_LTLIBRARIES = libhello. la # Name of the dynamic library to be generated libhello_la_SOURCES = test. cpp # source file and header file, separated by Spaces
The following is a template for the executable file:



AUTOMAKE_OPTIONS = foreignINCLUDES =-I .. /include # bin_PROGRAMS = hello hello_SOURCES = hello. cpphello_LDADD =-L .. /lib/-lz .. /lib/test. lo # dependent library files
7. If you want to generate a. lo dynamic library, libtoolize -- automake -- copy -- force


8. touch news readme authors ChangeLog

9. Run the automake -- add-missing command to generate the Makefile. in file.

10. Run the./configure command to generate the Makefile file.

11. Run make & make install.

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: 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.