Use autotools to assist in Linux Program Development

Source: Internet
Author: User
Tags automake
We have mentioned the development of dynamic and static libraries in Linux. You may have noticed that although our program is still small, however, when a compilation error occurs, we need to do a lot of work to start from the beginning. Sometimes we forget what step we did. There is no doubt that the first thing you think of is to write a makefile by yourself. But why not use autotools.

Required tools:
Autoscan
Autoreconf
Autoconf
Automake
Libtool

Let's create a small project.
Test/lib/test1.c
# Include <stdio. h>

Int hello_world1 ()
{
Printf ("Hello world1/N ");
Return 1;
}

Test/lib/test2.c
# Include <stdio. h>

Void hello_world2 ()
{
Printf ("Hello World2/N ");
}

Test/APP. c
# Include <stdio. h>

Int main ()
{
Hello_world1 ();
}

Now we need to compile the files under the lib directory into a static library and integrate them into the final app.
$ Autoscan
$ Ls
App. c autoscan. Log Configure. Scan lib
$ MV Configure. Scan Configure. In
The autoscan command will be used to generate the initial configure. In file.

Now let's change the configure. In File
Join
Am_init_automake ([-wall-werror])
Initialize automake, enable all warnings, and report them as errors.

Ac_prog_ranlib
Declare the static library compilation tool as ranlib, and gun ranlib is another form of Ar

Ac_config_files ([
Makefile
LIB/makefile])
Declare makefile and LIB/makefile as output files

Test/makefile. AM
Subdirs = lib
Bin_programs = app
App_sources = app. c
App_ldadd = lib/libtest.
Build lib recursively
APP in the bin directory
The app source code is app. C.
The Link Library of the app is lib/libtest..

Test/lib/makefile. AM
Lib_libraries = libtest.
Libtest_a_sources = test1.c test2.c
The file installed in the $ (libdir) directory is libtest..
The source code for compiling libtest. A is test1.c test2.c.

$ LS-R
.:
App. c Configure. In lib makefile. AM

./Lib:
Makefile. Am test1.c test2.c
$ Autoreconf -- install
Configure. In: 6: Installing './missing'
Configure. In: 6: Installing './install-Sh'
LIB/makefile. AM: Installing './depcomp'
Flag -- install indicates copying a lost auxiliary file.
The output of each person may be a little different, and there is basically no problem if there is no error
$./Configure -- prefix ~ /Test
...
-- Prefix specifies the directory to be installed
$ Make & make install
Someone may find an app executable program in the directory, but it is not the target binary file we want.
He is a bash script. What we want is already in ~ /Test/bin directory

Okay. Let's check if it is correct.
$ Cd ~ /Test
$ LS-R
.:
Bin lib

./Bin:
App

./Lib:
Libtest.
$ LDD bin/APP
Linux-gate.so.1 => (0x0011e000)
Libc. so.6 =>/lib/libc. so.6 (0x00ad2000)
/Lib/ld-linux.so.2 (0x00101000)
No evidence of Dynamic Link libtest.
$ Bin/APP
Hello world1

Dynamic library compilation is a bit different.
Test/configure. In
Change ac_prog_ranlib to ac_prog_libtool.
Add ac_disable_static to disable static library generation.

Test/makefile. AM
Change app_ldadd = lib/libtest. A to app_ldadd = lib/libtest. La in
For dynamic libraries, libtool helps generate a common form of La files.

Test/lib/makefile. AM
Lib_ltlibraries = libtest. La
Libtest_la_sources = test1.c test2.c
Note: lib_ltlibaries,
Indicates the library format generated by libtool.

Form of continuing to compile the static library
Note: To avoid confusion, clear ~ /Test File Content
$ Autoreconf -- install
$./Configure -- prefix ~ /Test
$ Make & make install

Remaining test results
$ Cd ~ /Test/
$ LDD bin/APP
Linux-gate.so.1 => (0x0011e000)
Libtest. so.0 =>/home/secularbird/test/lib/libtest. so.0 (0x0011f000)
Libc. so.6 =>/lib/libc. so.6 (0x00ad2000)
/Lib/ld-linux.so.2 (0x00101000)
We can see that the operation of the app needs to be connected to the corresponding libtest file.
$ Bin/APP
Hello world1
If you are interested, try to move the app to another place for execution.

Pay attention to the details during each step. Okay, good luck

References
Man ar
Man ranlib
Autotool book 1.5
Man autoreconf
Http://www-src.lip6.fr/Alexandre.Duret-Lutz/autotools.html
 

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.