生成makefile

在Cygwin中执行测试
  • automake
  • manual make

automake

注意: am文件是需要手工创建的!

ac文件

生成configure.scan文件, 并重命名为configure.ac

$ autoscan
$ mv configure.scan configure.ac

修改配置

注意: 所有新修改的值都需要方括号([])

  • 程序属性
AC_INIT([hex2int], [v0.1], [veic_2005@163.com])
  • 目录结构 如果只有本目录, 使用空值
AM_INIT_AUTOMAKE
AC_PROG_CC

C++ compiler:

AC_PROG_CXX
AC_CONFIG_FILES([Makefile])
  • 检查system头文件
AC_CHECK_HEADERS([string.h])

生成Makefile

aclocal
autoconf

自定义宏(可选)

在ac file中的定义:

AC_CONFIG_HEADERS([config.h])

执行:

autoheader

输出Makefile

automake --add-missing

输出Makefile:

./configure

Example

源代码参见十六进制转十进制

  • main.c
    只保留主程序入口
  • hex2int.c
    保留整个程序的结构
  • hex2int.h
    函数名声明

Makefile.am

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hex2int
hex2int_SOURCES=main.c hex2int.c
include_HEADERS=hex2int.h

configure.ac

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

AC_PREREQ([2.71])
AC_INIT([hex2int], [v0.1], [veic_2005@163.com])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([hex2int.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([string.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

手动Makefile

小项目可以。 注意:所有的命令前必须为TAB,不能使空格!

#https://stackoverflow.com/questions/1484817/how-do-i-make-a-simple-makefile-for-gcc-on-linux
#https://stackoverflow.com/questions/14109724/makefile-missing-separator/14109796
#https://www.gnu.org/software/make/manual/html_node/Pattern-Examples.html#Pattern-Examples
#https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html#Automatic-Variables


TARGET = hex2int
CC = gcc
CFLAGS = -g -Wall


all:$(TARGET)

OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)

%.o: %.c $(HEADERS)
	$(CC) -c $(CFLAGS) $< -o $@

$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -Wall -o $@

clean:
	-rm -f *.o
	-rm -f $(TARGET)

多个 include 路径

Specifies a directory dir to search for included makefiles.

LIB=-L/usr/informix/lib/c++
INC=-I/usr/informix/incl/c++ -I/opt/informix/incl/public

default:    main

main:   test.cpp
        gcc -Wall $(LIB) $(INC) -c test.cpp
        #gcc -Wall $(LIB) $(INC) -I/opt/informix/incl/public -c test.cpp

clean:
        rm -r test.o make.out

参考文档