天天看點

由configure檔案生成Makefile檔案簡單介紹

跟我一起,10分鐘搞懂configure檔案自動生成Makefile檔案簡單步驟.

1.準備源碼 hello.c func1.c func1.h func2.c func2.c

hello.c:

#include <func1.h>

void main() {

        m_printf("hello world!\n");

}

func1.c:

#include <func1.h>

void m_printf(const char* str) {

    m_m_printf(str);

}

func2.c:

#include <func2.h>

void m_m_printf(const char* str) {

        printf(str);

}

func1.h

#ifndef func1_h

#define func1_h

#include <func2.h>

void m_printf(const char* str);

#endif

func2.h

#ifndef func2_h

#define func2_h

#include <stdio.h>

void m_m_printf(const char* str);

#endif

2.源碼目錄執行autoscan生成configure.scan, 移動configure.scan至configure.ac,編寫configure.ac

關于如何編寫configure.ac,稍後請看configure.ac編寫規則

configure.ac:

#                                               -*- Autoconf -*-

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

AC_PREREQ([2.69])

AC_INIT([hello], [1.0], [[email protected]])

AM_INIT_AUTOMAKE

AC_CONFIG_HEADERS([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_OUTPUT([Makefile])

3. 執行aclocal生成m4檔案

4. 執行autoheader生成config.h.in

5.編寫Makefile.am

關于如何編寫Makefile.am請看Makefile.am編寫規則

Makefile.am:

noinst_PROGRAMS=hello

hello_SOURCES=hello.c

hello_LDADD=$(top_srcdir)/func1.o \

        $(top_srcdir)/func2.o

hello_LDFLAGS=-I$(top_srcdir)

6. 執行automake

如果出現錯誤

error: required file './compile' not found

error: required file './install-sh' not found

error: required file './missing' not found

error: required file './INSTALL' not found

執行 automake --add-missing

如果出現錯誤

Makefile.am: error: required file './NEWS' not found

Makefile.am: error: required file './README' not found

Makefile.am: error: required file './AUTHORS' not found

Makefile.am: error: required file './ChangeLog' not found

Makefile.am: installing './COPYING' using GNU General Public License v3 fil

執行touch NEWS README ChangeLog AUTHORS

然後在執行automake -a

7. 執行autoconf生成configure檔案

8. ./configure 生成Makefile

9. 測試Makefile

執行make

繼續閱讀