gcc/g++ 的简单 makefile

发布于 2024-09-24 05:24:28 字数 341 浏览 4 评论 0原文

我的项目总是包含:

  1. 成对的 Foo.h 和 Foo.cpp

  2. 一些额外的标头 util.h 等

的 makefile 最简单的方法是什么

  • 编写

    运行

    <前><代码>$CC -c foo.cpp

,保留对其相应 .h 文件的依赖关系

  • 提供了某种方式,使我可以手动添加额外的依赖关系
  • 包括与我的手动设置 $LIBS 变量的链接步骤。

我使用 Linux(Ubuntu) 和 gcc/g++。

My projects almst always consist of:

  1. Pairs of Foo.h and Foo.cpp

  2. Some extra headers util.h etc.

What is the simplest way to write a makefile that

  • Runs

    $CC -c foo.cpp
    

for each .cpp file, keeping a dependency to its coresponding .h file

  • Provides some way that I can manually add extra dependencies
  • Includes a linking step with my manuall set $LIBS variable.

I work with Linux(Ubuntu) and gcc/g++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拥抱我好吗 2024-10-01 05:24:29

这是我的一个项目中的一个示例 - 您只需将新对 foo1.ccfoo1.h 放入其中,它们就会自动为您构建:

# determine all sources and from that all targets
sources :=              $(wildcard *.cpp)
programs :=             $(sources:.cpp=)

## compiler etc settings used in default make rules 
CXX :=                  g++
CPPFLAGS :=             -Wall 
CXXFLAGS :=             -O3 -pipe 
LDLIBS :=  

# build all and strip programs afterwards 
all:                    $(programs) 
                        @test -x /usr/bin/strip && strip $^ 

Here is an example from one of my projects -- you can simply drop new pairs foo1.cc and foo1.h in there and they will automagically be built for you:

# determine all sources and from that all targets
sources :=              $(wildcard *.cpp)
programs :=             $(sources:.cpp=)

## compiler etc settings used in default make rules 
CXX :=                  g++
CPPFLAGS :=             -Wall 
CXXFLAGS :=             -O3 -pipe 
LDLIBS :=  

# build all and strip programs afterwards 
all:                    $(programs) 
                        @test -x /usr/bin/strip && strip $^ 
一腔孤↑勇 2024-10-01 05:24:28

请使用automake。您将获得正确的依赖项跟踪以及符合 GNU Makefile 标准的 makefile (例如,make install 执行正确的操作并尊重 DESTDIR前缀),能够根据需要检查系统怪癖并支持构建适当的分发 tarball。

这是一个最小的 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 生成配置脚本,然后运行 ​​>./configuremake

这是优秀的自动工具教程

Please, just use automake. You'll get proper dependency tracking, makefiles that comply with the GNU Makefile Standards (e.g., make install does the correct thing and respects DESTDIR and prefix), the ability to check for system quirks as needed and support for building proper distribution tarballs.

This is a minimal 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

and a minimal Makefile.am:

## Process this file with automake to generate Makefile.in
bin_PROGRAMS = foo
foo_SOURCES = foo.cpp bar.h baz.h quux.cpp

Run autoreconf -i to generate the configure script, followed by ./configure and make.

Here is an excellent autotools tutorial.

小嗷兮 2024-10-01 05:24:28

这个怎么样:

%.o: %.cpp %.h
    $(CC) -c 
lt; -o $@

# Some things have extra dependencies. (Headers like util.h are unlikely
# to change, but you can handle them this way if you really want to.)
#
# foo.o and bar.o both depend on baz.h
foo.o bar.o: baz.h

# foo.o also depends on gab.h and jig.h
foo.o: gab.h jig.h

# You will need a list of object files. You can build it by hand:
OBJ_FILES = foo.o bar.o snaz.o # and so on

# ...or just grab all the files in the source directory:
SOURCE_FILES = $(wildcard *.cpp)
OBJ_FILES = $(SOURCE_FILES:.cpp=.o)

# It is possible to get this from the environment, but not advisable.
LIBS = -lred -lblue

final-thing: $(OBJ_FILES)
    $(CC) $(LIBS) $^ -o $@

How about this:

%.o: %.cpp %.h
    $(CC) -c 
lt; -o $@

# Some things have extra dependencies. (Headers like util.h are unlikely
# to change, but you can handle them this way if you really want to.)
#
# foo.o and bar.o both depend on baz.h
foo.o bar.o: baz.h

# foo.o also depends on gab.h and jig.h
foo.o: gab.h jig.h

# You will need a list of object files. You can build it by hand:
OBJ_FILES = foo.o bar.o snaz.o # and so on

# ...or just grab all the files in the source directory:
SOURCE_FILES = $(wildcard *.cpp)
OBJ_FILES = $(SOURCE_FILES:.cpp=.o)

# It is possible to get this from the environment, but not advisable.
LIBS = -lred -lblue

final-thing: $(OBJ_FILES)
    $(CC) $(LIBS) $^ -o $@
七婞 2024-10-01 05:24:28

也许您可以查看CMake

如果您不熟悉 CMake,它基本上是一个 Makefile 生成器(或 XCode 或 Visual Studio 项目等,具体取决于平台),因此它可以让您只指定所需的变量,并为您处理标头依赖问题, makefile生成等

Perhaps you can check out CMake?

If you're unfamiliar with CMake, it's basically a Makefile generator (or XCode, or Visual Studio Projects, etc, depending on platform), so it lets you specify just the variables you need, and takes care of header dependency issues for you, makefile generation, etc.

拥醉 2024-10-01 05:24:28

下面是一个简单的 shell 脚本,它从给定目录中的所有 .cpp 文件构建 makefile:

# !sh    
if [ $# = 0 ]
then
echo -e "please give executable name"
exit 1
fi

echo -e -n "CC=g++\nOPTIMS=\nLIBS= " > makefile

echo >> makefile
echo -n "$1: " >> makefile
for fic in *.cpp
do
echo -n "${fic%\.cpp}.o " >> makefile
done

echo >> makefile
echo -n -e "\t\$(CC) " >> makefile
for fic in *.cpp
do
echo -n "${fic%\.cpp}.o " >> makefile
done
echo -n -e "-o $1 \$(OPTIMS) \$(LIBS)\n" >> makefile

echo >> makefile
for fic in *.cpp
do
g++ -MM $fic >> makefile
echo -e "\t\$(CC) -c $fic \$(OPTIMS)\n" >> makefile
done

exit 0

它使用 gcc 的 -MM 选项来创建 makefile 依赖行。只需在源目录中创建脚本(我们称之为 micmake),使其可执行(chmod +x micmake)并键入

./micmake go

它将创建一个 makefile 和 make 命令编译您的项目。该可执行文件名为go。如果需要特殊的编译选项或库,可以编辑 makefile。对于更复杂的项目和依赖项,您应该使用 automake、cmake 或 scons。

Here is a simple shell script that constructs a makefile from all .cpp files in a given directory:

# !sh    
if [ $# = 0 ]
then
echo -e "please give executable name"
exit 1
fi

echo -e -n "CC=g++\nOPTIMS=\nLIBS= " > makefile

echo >> makefile
echo -n "$1: " >> makefile
for fic in *.cpp
do
echo -n "${fic%\.cpp}.o " >> makefile
done

echo >> makefile
echo -n -e "\t\$(CC) " >> makefile
for fic in *.cpp
do
echo -n "${fic%\.cpp}.o " >> makefile
done
echo -n -e "-o $1 \$(OPTIMS) \$(LIBS)\n" >> makefile

echo >> makefile
for fic in *.cpp
do
g++ -MM $fic >> makefile
echo -e "\t\$(CC) -c $fic \$(OPTIMS)\n" >> makefile
done

exit 0

It uses the -MM option of gcc for creating makefile dependency lines. Just create the script in the sources directory, (let's call it micmake), make it executable (chmod +x micmake) and type

./micmake go

It will create a makefile and the make command compile your project. The executable is named go. You can edit the makefile if you need special compilation options or libraries. For more complex projects and dependencies, you should use automake, cmake or scons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文