linux生成MakefIle文件

makefile文件是linux里面编译c/c++文件的配置工具,首先linux需要安装gcc编译器:

huangxudong@huangxudong-X456UR:~/cpp/makedemo$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

huangxudong@huangxudong-X456UR:~/cpp/makedemo$ 

因为是要自动生成Makefile文件,所以需要下载一个工具,automake

root@huangxudong-X456UR:/etc/init.d# sudo apt-get install automake
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
automake 已经是最新版 (1:1.15.1-3ubuntu2)。
automake 已设置为手动安装。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 15 个软件包未被升级。
root@huangxudong-X456UR:/etc/init.d# 

cpp源文件:

#include 

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

1、生成模板文件,autoscan;

huangxudong@huangxudong-X456UR:~/cpp$ cd make
huangxudong@huangxudong-X456UR:~/cpp/make$ ls
main.cpp
huangxudong@huangxudong-X456UR:~/cpp/make$ autoscan
huangxudong@huangxudong-X456UR:~/cpp/make$ ls
autoscan.log  configure.scan  main.cpp
huangxudong@huangxudong-X456UR:~/cpp/make$ 

2、修改模板configure.scan文件为configure.sc并修改部分内容:

huangxudong@huangxudong-X456UR:~/cpp/make$ mv configure.scan configure.ac
huangxudong@huangxudong-X456UR:~/cpp/make$ ls
autoscan.log  configure.ac  main.cpp
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main.cpp])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

3、使用aclocal生成两个文件:

huangxudong@huangxudong-X456UR:~/cpp/make$ aclocal
huangxudong@huangxudong-X456UR:~/cpp/make$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  main.cpp
huangxudong@huangxudong-X456UR:~/cpp/make$ 

4、创建Makefile.am,这个文件主要是用来创建Makefile.in的

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.cpp

5、创建config.h.in文件:autoheader

huangxudong@huangxudong-X456UR:~/cpp/make$ autoheader
huangxudong@huangxudong-X456UR:~/cpp/make$ ls
aclocal.m4      autoscan.log  configure.ac  Makefile.am
autom4te.cache  config.h.in   main.cpp
huangxudong@huangxudong-X456UR:~/cpp/make$ 

6、生成其他需要的文件:automake -a,主要文件Makefile.in

huangxudong@huangxudong-X456UR:~/cpp/make$ automake -a
configure.ac:8: installing './install-sh'
configure.ac:8: installing './missing'
Makefile.am: installing './depcomp'
huangxudong@huangxudong-X456UR:~/cpp/make$ ls
aclocal.m4      autoscan.log  configure.ac  install-sh  Makefile.am  missing
autom4te.cache  config.h.in   depcomp       main.cpp    Makefile.in
huangxudong@huangxudong-X456UR:~/cpp/make$ 

7、生成configure脚本:autoconf

huangxudong@huangxudong-X456UR:~/cpp/make$ autoconf
huangxudong@huangxudong-X456UR:~/cpp/make$ ls
aclocal.m4      config.h.in   depcomp     Makefile.am
autom4te.cache  configure     install-sh  Makefile.in
autoscan.log    configure.ac  main.cpp    missing
huangxudong@huangxudong-X456UR:~/cpp/make$ 

8、执行configure脚本:

huangxudong@huangxudong-X456UR:~/cpp/make$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for g++... g++
checking whether the C++ compiler works... yes
checking for C++ compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
huangxudong@huangxudong-X456UR:~/cpp/make$ 

9、生成Makefile文件:make

huangxudong@huangxudong-X456UR:~/cpp/make$ make
make  all-am
make[1]: 进入目录“/home/huangxudong/cpp/make”
g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
mv -f .deps/main.Tpo .deps/main.Po
g++  -g -O2   -o main main.o  
make[1]: 离开目录“/home/huangxudong/cpp/make”
huangxudong@huangxudong-X456UR:~/cpp/make$ 

10、执行链接之后的文件:

huangxudong@huangxudong-X456UR:~/cpp/make$ ./main
Hello world!
huangxudong@huangxudong-X456UR:~/cpp/make$ 

注意:开放源代码需要在里面创建readme文件,install文件等等

你可能感兴趣的:(linux)