In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to generate Makefile under Linux". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to generate Makefile under Linux".
1. Introduction of Makefile
Makefile is used for automatic compilation and linking, a project consists of many files, each file change will lead to the project re-link, but not all files need to be recompiled, Makefile records the file information, in make will decide which files need to be recompiled when linking.
The purpose of Makefile is to let the compiler know which other files you need to rely on to compile one file. When those dependent files change, the compiler automatically finds that the final generated file is out of date and recompiles the corresponding module.
The basic structure of Makefile is not very complex, but when a program developer starts to write Makefile, he often wonders whether what he writes conforms to the convention, and the Makefile he writes is often associated with his own development environment. When the system environment variable or path changes, the Makefile may have to be modified. This causes many problems of writing Makefile by hand, and automake can help us solve these problems very well.
With automake, program developers only need to write some simple files containing predefined macros, autoconf generates configure from one macro file, automake generates Makefile.in from another macro file, and then uses configure to generate a conventional Makefile based on Makefile.in. Next we will introduce the automake generation method of Makefile in detail.
Second, the environment in which to use
The program mentioned in this article is based on the Linux distribution: Linux 2.6.18-194.el5, which contains the autoconf,automake we are going to use. If not, you can download autoconf and automake yourself.
Third, start with helloworld
Let's start with the most commonly used example program, helloworld.
In a nutshell, the following process is:
Create three new files: helloworld.cconfigure.inMakefile.am
Then execute: autoscan; aclocal; autoconf; automake-- add-missing;. / configure; make;. / helloworld
You can see that the Makefile is generated and the helloworld.c can be compiled through. Very simple, a few commands can make a conventional Makefile, how does it feel? Now let's introduce the detailed process:
1. Build a catalogue
Create a helloworld directory under your working directory, which we use to store helloworld programs and related files, such as under / home/my/build:
$mkdir helloword $cd helloworld
2 、 helloworld.c
Then write a hellowrold.c file with your favorite editor, such as the command: vi helloworld.c. Use the following code as the content of helloworld.c.
# include int main (int argc, char** argv) {printf ("Hello, Linux World!"); return 0;}
Save and exit when you are finished. Now you should have a helloworld.c written by yourself in the helloworld directory.
3. Generate configure
We use the autoscan command to help us generate a template file for configure.in from the source code in the directory.
Command:
$autoscan $ls configure.scan helloworld.c
After execution, a file is generated in the hellowrold directory: configure.scan, which we can use as a blueprint for configure.in.
4, generate configure.in
Now rename configure.scan to configure.in, and edit it, modify it as follows, and remove extraneous statements:
Code
#-*-Autoconf-*-# Process this file with autoconf to produce a configure script AC_INIT (helloworld.c) AM_INIT_AUTOMAKE (helloworld, 1. 0) # 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_OUTPUT (Makefile)
5 execute aclocal and autoconf
Then execute the commands aclocal and autoconf, resulting in two files, aclocal.m4 and configure, respectively:
Code
$aclocal $ls aclocal.m4 configure.in helloworld.c $autoconf $ls aclocal.m4 autom4te.cache configure configure.in helloworld.c
You can see that the configure.in content is a number of macro definitions, these macros after autoconf processing will become shell scripts to check system features, environment variables, and parameters necessary for the software.
Autoconf is a tool used to generate automatic configuration software source code scripts (configure). Configure scripts can be run independently of autoconf and can be run without user intervention.
To generate the configure file, you must tell autoconf how to find the macro you are using. The way is to use the aclocal program to generate your aclocal.m4.
Aclocal automatically generates configure.in files according to the contents of aclocal.m4 files. Aclocal is a perl script defined as "aclocal-create aclocal.m4 by scanning configure.ac".
Autoconf creates a configure from configure.in, a template file that enumerates the various parameters needed to compile the software.
Autoconf requires a GNU M4 macro processor to process aclocal.m4 and generate configure scripts.
M4 is a macro processor. Copy the input to the output and expand the macro. Macros can be embedded or user-defined. In addition to expanding macros, M4 has some built-in functions for referencing files, executing commands, integer operations, text operations, loops, and so on. M 4 can be used either as the front end of the compiler or as a separate macro processor.
6. Create a new Makefile.am
Create a new Makefile.am file, command: $vi Makefile.am
The contents are as follows:
Code:
AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=helloworld helloworld_SOURCES=helloworld.c
Automake will automatically generate Makefile.in based on the Makefile.am you write. The macros and targets defined in Makefile.am instruct automake to generate the specified code. For example, the macro bin _ PROGRAMS will cause the compilation and connection targets to be generated.
7. Run automake
Command:
Code:
$automake-- add-missing configure.in: installing `. / install-sh' configure.in: installing`. / mkinstalldirs' configure.in: installing `. / missing' Makefile.am: installing`. / depcomp'
Automake generates files from the Makefile.am file, including the most important Makefile.in.
8. Execute configure to generate Makefile
Code:
$. / configure checking for a BSD-compatible install / usr/bin/install-c checking whether build environment is sane yes checking for gawk gawk checking whether make sets $(MAKE) yes checking for gcc gcc checking for C compiler default output a.out checking whether the C compiler works yes checking whether we are cross compiling no checking for suffix of executables checking for suffix of object files o checking whether we are using the GNU C compiler yes checking whether gcc accepts-g yes checking for gcc option to accept ANSI C none needed checking for style of include used by make GNU checking dependency style of gcc gcc3 Configure: creating. / config.status config.status: creating Makefile config.status: executing depfiles commands $ls-l Makefile-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile
As you can see, Makefile has already been generated at this time.
9. Compile the code using Makefile
Code
$make if gcc-DPACKAGE_NAME= "- DPACKAGE_TARNAME="-DPACKAGE_VERSION= "- DPACKAGE_STRING="-DPACKAGE_BUGREPORT= "- DPACKAGE=" helloworld "- DVERSION=" 1.0 "- I. -I. -g-O2-MT helloworld.o-MD-MP-MF ".deps / helloworld.Tpo"-c-o helloworld.o `deps-f 'helloworld.c' | | echo'. / '`helloworld.c; then mv-f ".deps / helloworld.Tpo".deps / helloworld.Po"; else rm-f ".deps / helloworld.Tpo"; exit 1; fi gcc-g-O2-o helloworld helloworld.o 10, run helloworld Code $. / helloworld Hello, Linux World!
In this way, helloworld is compiled, and if you follow the steps above, you should easily compile the correct helloworld file. You can also try using some other make commands, such as make clean,make install,make dist, to see what effect they will give you. How do you feel? If you can write such a professional Makefile, your boss will be impressed.
IV. Deep understanding and simple understanding
Let's give a more detailed introduction to the above-mentioned commands.
1 、 autoscan
Autoscan is used to scan the source code directory to generate configure.scan files. Autoscan can take the directory name as a parameter, but if you don't use the parameter, then autoscan will assume that the current directory is being used. Autoscan will scan the source files in the directory you specified and create the configure.scan file.
2 、 configure.scan
Configure.scan contains the basic options for system configuration, which are all macro definitions. We need to rename it configure.in.
3 、 aclocal
Aclocal is a perl script. Aclocal automatically generates configure.in files according to the contents of aclocal.m4 files. Aclocal is defined as "aclocal-create aclocal.m4 by scanning configure.ac".
4 、 autoconf
Using autoconf, generate configure files based on configure.in and aclocal.m4. Configure is a script that can set up source programs to adapt to different operating system platforms and generate appropriate Makefile according to different systems, so that your source code can be compiled on different operating system platforms.
The contents of the configure.in file are macros that are processed by autoconf and become shell scripts that check system features, environment variables, and parameters that the software must have. The order of macros in the configure.in file is not specified, but you must add AC_ int macros and AC_ output macros to the front and * * sides of all macros.
In configure.ini:
The # sign indicates a comment, and the rest of the macro will be ignored.
The AC_INIT (FILE) macro is used to check the path to the source code.
The AM_INIT_AUTOMAKE (PACKAGE, VERSION) macro is required and describes the name and version number of the package we are going to generate: PACKAGE is the name of the package and VERSION is the version number. When you use the make dist command, it will generate a helloworld-1.0.tar.gz-like software distribution with the name and version number of the corresponding package.
The AC_PROG_CC macro will check the C compiler used by the system.
AC_OUTPUT (FILE) this macro is the name of the Makefile we want to output.
When we use automake, we actually need to use some other macros, but we can use aclocal to help us generate automatically. After executing the aclocal, we will get the aclocal.m4 file.
Once the configure.in and aclocal.m4 macro files are generated, we can use autoconf to generate the configure file.
5 、 Makefile.am
Makefile.am is used to generate Makefile.in and requires you to write it by hand. Some things are defined in Makefile.am:
AUTOMAKE_OPTIONS this is an option for automake. When executing automake, it checks the directory for the existence of various files that should be included in the standard GNU package, such as AUTHORS, ChangeLog, NEWS, and so on. When we set it to foreign, automake will be checked using the standard of the general software package.
Bin_PROGRAMS this specifies the file name of the executable we want to generate. If you want to generate multiple executables, separate the names with spaces.
Helloworld_SOURCES this is to specify the source code required to generate "helloworld". If it uses multiple source files, separate them with space symbols. For example, if you need helloworld.h,helloworld.c, please write it helloworld_SOURCES= helloworld.h helloworld.c.
If you define multiple executables in bin_PROGRAMS, define a relative filename_SOURCES for each executable.
6 、 automake
We use automake to generate Makefile.in based on configure.in and Makefile.am.
Option-add-missing is defined as "add missing standard files to package", which allows automake to add files necessary for a standard package.
The Makefile.in file we generated with automake conforms to the GNU Makefile convention, and then we just need to execute the shell script configure to produce the appropriate Makefile file.
7 、 Makefile
In Makefile, which conforms to GNU Makefiel conventions, some basic predefined operations are included:
Make compiles source code, links, generates object files and executables according to Makefile.
Make clean clears the object files (files with the suffix ".o") and executables generated by the last make command.
Make install installs the compiled executable file into the system directory, typically the / usr/local/bin directory.
Make dist generates the release package file (that is, distribution package). This command will package the executable and related files into a tar.gz-compressed file that will be used as a package to distribute the software. It generates a file with a name similar to "PACKAGE-VERSION.tar.gz" in the current directory. PACKAGE and VERSION are AM_INIT_AUTOMAKE (PACKAGE, VERSION) that we define in configure.in.
Make distcheck generates and tests the release package to determine the correctness of the release package. This operation will automatically unpack the compressed package file, then execute the configure command, and execute make to confirm that there is no error in the compilation, and * prompt you that the package is ready for release.
Make distclean is similar to make clean, but also deletes all files generated by configure, including Makefile.
5. Process diagram
VI. Concluding remarks
From the above introduction, you should be able to easily generate your own Makefile file and corresponding project file that conforms to GNU conventions. If you want to write a more complex and conventional Makefile, you can refer to the configure.in and Makefile.am files in some open code projects, such as: embedded database sqlite, unit test cppunit.
Thank you for reading, the above is the content of "how to generate Makefile under Linux". After the study of this article, I believe you have a deeper understanding of how to generate Makefile under Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.