我的项目几乎总是包括:
Foo.h 和 Foo.cpp 对
一些额外的头文件 util.h 等
编写makefile的最简单方法是什么
运行
$CC -c foo.cpp
对于每个 .cpp 文件,保持对其对应的 .h 文件的依赖
- 提供一些我可以手动添加额外依赖的方法
- 包括与我的手动设置 $LIBS 变量的链接步骤。
我使用 Linux(Ubuntu) 和 gcc/g++。
最佳答案
请使用 automake
。您将获得适当的依赖项跟踪、符合 GNU Makefile Standards 的 makefile。 (例如,make install
做正确的事情并遵守 DESTDIR
和 prefix
),能够根据需要检查系统异常并支持构建适当的分发压缩包。
这是一个最小的 configure.ac
:
-*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.61])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AM_INIT_AUTOMAKE([foreign])
# 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
和一个最小的Makefile.am
:
## Process this file with automake to generate Makefile.in
bin_PROGRAMS = foo
foo_SOURCES = foo.cpp bar.h baz.h quux.cpp
运行 autoreconf -i
生成配置脚本,然后是 ./configure
和 make
。
这是一个 excellent autotools tutorial .
关于linux - gcc/g++ 的简易 makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3746498/