Release and install your own software in Linux

Source: Internet
Author: User

Start with a simple hello world example and then apply it to the actual project. This mainly involves the use of Autoconf related tools. To learn more, it is of great benefit to your daily development.

1. Create a directory structure

The top-level directory named helloworld in the module name contains a src directory to store the source file (helloworld/src). This is the Convention. When multiple sub-modules exist, the source code of each sub-module is placed in its own directory.

2. Create a source file

Create a main. c file under the src directory with only one output item Hello wrold !!!.

3. Create a makefile Template

1) helloworld/makefile. am. File Content: subdirs = SRC. Only a simple line of code indicates that there is a SRC subdirectory under it. If there are multiple subdirectories, separate them with spaces.

2) helloworld/src/makefile. am. File Content: bin_programs = helloworld helloworld_sources = Main. C. This indicates that there is an executable file helloworld, which is compiled from the source file main. C. Programs indicates the executable file to be generated. When multiple executable files exist, separate them with spaces. Bin indicates the directory to be installed for the executable files. Sources indicates the source files required to generate executable files. When multiple source files exist, separate them with spaces. In addition, the. Am extension is short for automake, which is the template used by automake to generate the makefile. In file.

4. Create an aotuconf Template

Run autoscan under helloworld to generate the file Configure. Scan and rename it Configure. In. This is the Autoconf template file. Its content is roughly: #-*-Autoconf -*-
# Process this file with Autoconf to produce a configure script.

Ac_prereq (2.61)
Ac_init (full-package-name, version, bug-Report-address)
Ac_config_srcdir ([src/Main. C])
Ac_config_header ([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

This file is composed of a series of macros which are finally expanded by the command M4 to obtain a script file configure. The main function of configure is to detect system configurations and generate makefile files based on these configurations. For example, ac_prog_cc is used to detect the compiler. ac_config_files and ac_output are used to generate makefile and other data files.

However, this template file cannot be used directly. You need to make the following changes:

Put:
Ac_init (full-package-name, version, bug-Report-address)
Changed:
Ac_init (helloworld, 0.1, xianjimli@hotmail.com)

Full-package-name is the name of the module.

Version is the version number of the module. The initial version number is 0.1. Two-level version is enough for a small module. The major version number before the decimal point is used. The major version number is upgraded only when major updates are made. The number after the decimal point is the minor version number, and should be upgraded each time it is released. After upgrading to 0.9, you can continue to upgrade to 0.10 and 0.11.

Bug-Report-address is the email address of the author or maintainer.

Add an automake initialization script:

Am_init_automake (helloworld, 0.1)

Helloworld is the module name.

0.1 is the version number of the module.

Here it is the same as the preceding parameter. ac_init initializes Autoconf and am_init_automake is the initial automake. In some cases, am_init_automake is not required when only data files are generated and files are not compiled.

Finally, the following file is obtained:

#-*-Autoconf -*-

# Process this file with Autoconf to produce a configure script.

Ac_prereq (2.61)

Ac_init (helloworld, 0.1, xianjimli@hotmail.com)

Ac_config_srcdir ([src/Main. C])

Ac_config_header ([config. H])

Am_init_automake (helloworld, 0.1)

# 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

O The macro used for copying.
Run: aclocal

As mentioned above, configure. In is a series of macros which are carried out by the M4 command. M4 is actually the abbreviation of Macro. 4 indicates that four letters are omitted after M. Similar to i18n (internationalization) and l10n (localization), the numbers represent the number of omitted letters.

Macros such as ac_prog_cc are standard macros (or built-in macros) and do not need to be written by ourselves. However, we need to run the aclocal command to configure aclocal. the macros used in are copied to our project. After aclocal is run in the helloworld directory, the following error occurs in the current directory:

Autom4te. cache is a temporary directory, which is only used to accelerate macro development.
Aclocal. M4 is the macro definition used in Configure. in. For more information, see.

O generate a configuration header file template.
Run: autoheader

The config. h header file is used to define macros that can be referenced in C/C ++ programs, such as the module name and version number. These macros are generated by the configure script, but we need to provide a template file. This template file can be generated using the command autoheader. After running autoheader in the helloworld directory, config. H. In is generated in the current directory. You do not need to modify it.

O create several necessary files.

Readme: describes the functions, usage, and precautions of the module.
News: Describes the latest developments in the module.
Authors: the author and contact information of the module.
Changelog: records the modification history of the module. It has a fixed format:
1. Put the latest modification at the top.
2. For each record, the first line is written on the date, modifier, and contact information. The second line starts with a tab, adds an asterisk, and then writes the reason and position of the modification. For example:

2009-03-29 Li xianjing
* Created
O generate makefile. in and the required scripts.
Run: automake-

This command creates links to several copying depcomp install-SH missing files that point to the files in the system. The most important function of automake is to generate makefile. In files using the makefile. Am template. makefile. In is much more complex than makefile. Am. Fortunately, we don't need to know it.

O generate the configure script.
Run: Autoconf

The Autoconf function is to call M4 to expand the macro in configure. In and generate the configure script, which is the final running script.

O generate the final makefile.
Run:./configure

Configure has two common parameters:
-Prefix is used to specify the installation directory. The default installation directory in Linux is/usr/local.
-Host is used for cross-compilation. For example, programs compiled on the arm Board on an X86 PC are used.

For example:./configure-Prefix =/home/lixianjing/work/ARM-root/usr-host = arm-Linux

O Compilation
Run: Make

O Installation
Run: make install

O release software packages
Run: Make Dist or make distcheck

Make distused to generate a software package, which will contain a file named helloworld-0.1.tar.gz. Generally, the source code in the source code management system (CVS/SVN/GIT) is under development and is unstable, while the released software package is stable and available to users.

Why? Is it a little dizzy? Here, I want to know how it works. In actual operations, we can put some of the previous actions of make in a script file. This script file is usually named autogen. Sh or Bootstrap.

From: http://blog.csdn.net/absurd

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.