Use the automake toolkit to create a makefile for a cross-compiled shared library
The Autoconf version is 2.67, which is implemented in Debian:
If the version is low, you can use apt-Get
Install automake to install the latest version. libtool is also required, and apt-Get install is also required.
Here we will create a simple function library to introduce:
Create the directory RX and the source directory SRC:
$mkdir rx
$mkdir rx/src
Edit the two source files Rx. C and Rx. h under Rx/src:
mybdebian:/home/myb/php/rx/src# cat rx.h
#ifndef __RX_H_
#define __RX_H_
int fun();
#endif
mybdebian:/home/myb/php/rx/src# cat rx.c
#include "rx.h"
int fun()
{
return 1234;
}
Create makefile. Am files under the RX directory and the Rx/src directory respectively.
Makefile. AM in the RX directory is the top-level makefile. Am file, as long as it contains the subdirectory of the source code. The content is as follows:
AUTOMAKE_OPTIONS=foreign
SUBDIRS=src
The contents of makefile. AM in the Rx/src directory are as follows:
AUTOMAKE_OPTIONS=foreign
lib_LTLIBRARIES=librx.la
librx_la_SOURCES=rx.cinclude_HEADERS=rx.h
Lib_ltlibraries = "librx. La" is the file name of the library to be generated. It must be written as. LA, not. So. The librx. So file is actually generated.
The following librx_la is librx. La. Replace "." With the underscore "_" followed by "_ sources =". After the equal sign, the source file name is separated by spaces when there is more than one.
Include_headers = the header file is used by other programs. During make install, the file is copied to the include directory of the installation directory.
Go back to the upper-level directory and use autoscan to scan the source directory:
$autoscan
$ls
autoscan.log configure.scan Makefile.am src
Change Configure. Scan to configure. ac.
$mv configure.scan configure.ac
AC_OUTPUT([Makefile
src/Makefile])
Compile this file:
$vim configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.67])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/rx.c])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_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/Makefile])
AC_OUTPUT
Modify ac_init to add the package's full name, version number, and bug report email address, for example:
AC_INIT(rx2dlib,0.01,resound@163.com)
Add the following sentence to it:
AM_INIT_AUTOMAKE
This sentence is required.
Because this script is used to generate a shared library, add AC_PROG_LIBTOOL to the next line of AC_PROG_CC:
# Checks for programs.
AC_PROG_CC
AC_PROG_LIBTOOL
Modify the following AC_CONFIG_FILES statement to AC_OUTPUT and remove the following AC_OUTPUT:
AC_OUTPUT([Makefile
src/Makefile])
In this way, configure. ac is created. The modified content is as follows:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.67])
AC_INIT(rx2dlib,0.01,resound@163.com)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/rx.c])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC
AC_PROG_LIBTOOL
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT([Makefile
src/Makefile])
Then, execute the command in order:
$aclocal
$autoconf
$autoheader
$libtoolize --automake
$automake –-add-missing
configure.ac:13: installing `./config.guess'
configure.ac:13: installing `./config.sub'
configure.ac:6: installing `./install-sh'
configure.ac:6: installing `./missing'
src/Makefile.am: installing `./depcomp'
The configure script is generated.
You can use it to configure and generate Makefile.
For example, we want to generate a cross-Compilation Program for ARM.
The shared library used by the CPU. Assume that the compiler we want to use is arm-none-linux-gnueabi-gcc, and when we finally execute it on the ARM board, install the library in the/usr/local/armrx2d directory of the target board. You can configure it as follows:
$./configure --host=arm-none-linux-gnueabi --prefix=/usr/local/armrx2d$make
After Make is complete, you can view the generated library file in a hidden directory. libs directory under the src directory:
Mybdebian:/home/myb/php/rx/src/. libs # ls-l
Total 20
-Rw-r -- 1 root 2368 04-19 22:48 librx.
Lrwxrwxrwx 1 root 11 04-19 librx. la-> ../librx. la
-Rw-r -- 1 root 930 04-19 22:48 librx. lai
Lrwxrwxrwx 1 root 14 04-19 22:48 librx. so-> librx. so.0.0.0
Lrwxrwxrwx 1 root 14 04-19 22:48 librx. so.0-> librx. so.0.0.0
-Rwxr-xr-x 1 root 5422 04-19 22:48 librx. so.0.0.0
-Rw-r -- 1 root 2228 04-19 22:48 rx. o
Go back to the top-level directory and execute make
Install can automatically install the generated Library to the/usr/local/armrx2d/lib directory.
If the following error message is displayed when automake is used
Required file './ltmain. Sh' not found
Because libtool is not configured:
Solution:
$ Libtoolize
-- Automake -- debug -- copy-force
Or:
$ Libtoolize
-- Automake