Detailed Makefile.am

 

Makefile.am is a higher-level rule than Makefile. Just specify what target to be generated, what source file it is generated from, what directory to install into, etc.

Table 1 lists executable files, static libraries, header files and data files, and four general formats for writing Makefile.am files.



Table 1 General format of Makefile.am


For executable files and static library types, if you only want to compile and do not want to install into the system, you can use noinst_PROGRAMS instead of bin_PROGRAMS, and noinst_LIBRARIES instead of lib_LIBRARIES.

Makefile.am also provides some global variables for all targets to use:


Table 2 Global variables available in Makefile.am

Try to use relative paths in Makefile.am. The system predefines two basic paths:


Table 3 Path variables available in Makefile.am

We mentioned the installation path above, and automake sets the default installation path:

1) Standard installation path

The default installation path is: $(prefix) = /usr/local, which can be overridden by ./configure –prefix=<new_path>.

Other predefined directories include: bindir = $(prefix)/bin, libdir = $(prefix)/lib, datadir = $(prefix)/share, sysconfdir = $(prefix)/etc and so on.

2) Define a new installation path

For example, for test, you can define testdir = $(prefix)/test, then test_DATA = test1 test2, then test1 and test2 will be installed as data files in the $(prefix)/ /test directory.

We first need to create a Makefile.am in the top-level directory of the project (ie project/) to specify the included subdirectories:

SUBDIRS=src/lib src/ModuleA/apple/shell src/ModuleA/apple/core

CURRENTPATH=$(shell /bin/pwd)

INCLUDES=-I$(CURRENTPATH)/src/include -I$(CURRENTPATH)/src/ModuleA/apple/include

export INCLUDES

Since each source file will use the same header file, we include the header file used when compiling the source file in the top Makefile.am, and export it, see the blue part of the code.

We compile the swap.c file in the lib directory into a libswap.a file, which is called by the apple/shell/apple.c file, then the Makefile.am in the lib directory is as follows:

noinst_LIBRARIES=libswap.a

libswap_a_SOURCES=swap.c

INCLUDES=-I$(top_srcdir)/src/includ

Careful readers may ask: Why is bin_LIBRARIES given in Table 1, but here is noinst_LIBRARIES? This is because if you only want to compile and don't want to install to the system, use noinst_LIBRARIES instead of bin_LIBRARIES, and for executable files, use noinst_PROGRAMS instead of bin_PROGRAMS. For installation, the library will be installed in the $(prefix)/lib directory, and the executable file will be installed in ${prefix}/bin. If you want to install the library, an example of Makefile.am is as follows:

bin_LIBRARIES=libswap.a

libswap_a_SOURCES=swap.c

INCLUDES=-I$(top_srcdir)/src/include

swapincludedir=$(includedir)/swap

swapinclude_HEADERS=$(top_srcdir)/src/include/swap.h

The last two lines mean to install swap.h into the ${prefix}/include /swap directory.

Next, for the executable file type, we will discuss how to write Makefile.am? For compiling files in the apple/core directory, the Makefile.am we wrote is as follows:

noinst_PROGRAMS=test

test_SOURCES=test.c

test_LDADD=$(top_srcdir)/src/ModuleA/apple/shell/apple.o $(top_srcdir)/src/lib/libswap.a

test_LDFLAGS=-D_GNU_SOURCE

DEFS+=-D_GNU_SOURCE

#LIBS=-lpthread

由于我们的test.c文件在链接时,需要apple.o和libswap.a文件,所以我们需要在 test_LDADD中包含这两个文件。对于Linux下的信号量/读写锁文件进行编译,需要在编译选项中指明-D_GNU_SOURCE。所以在 test_LDFLAGS中指明。而test_LDFLAGS只是链接时的选项,编译时同样需要指明该选项,所以需要DEFS来指明编译选项,由于 DEFS已经有初始值,所以这里用+=的形式指明。从这里可以看出,Makefile.am中的语法与Makefile的语法一致,也可以采用条件表达 式。如果你的程序还包含其他的库,除了用AC_CHECK_LIB宏来指明外,还可以用LIBS来指明。

如果你只想编译某一个文件,那么Makefile.am如何写呢?这个文件也很简单,写法跟可执行文件的差不多,如下例所示:

noinst_PROGRAMS=apple

apple_SOURCES=apple.c

DEFS+=-D_GNU_SOURCE

我们这里只是欺骗automake,假装要生成apple文件,让它为我们生成依赖关系和执行命令。所以当你运行完automake命令后,然后修改apple/shell/下的Makefile.in文件,直接将LINK语句删除,即:

…….

clean-noinstPROGRAMS:

-test -z “$(noinst_PROGRAMS)” || rm -f $(noinst_PROGRAMS)

apple$(EXEEXT): $(apple_OBJECTS) $(apple_DEPENDENCIES)

@rm -f apple$(EXEEXT)

#$(LINK) $(apple_LDFLAGS) $(apple_OBJECTS) $(apple_LDADD) $(LIBS)


Guess you like

Origin blog.csdn.net/panpanloveruth/article/details/7280669