Makefile,我如何使用它们? [英] Makefiles, how can I use them?

查看:101
本文介绍了Makefile,我如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

^这不是重复的问题^

更新

我是编程的新手,我现在只遇到makefile。

I'm fairly new to programming and I've only now just come across makefiles.

我下载了一堆教程和源代码代码来自过去几个月的各个地方;直到现在我只使用VS解决方案,并一直避免他们。

I've downloaded a bunch of tutorials and source code from a variety of places over the last few months; until now I've only been using VS solutions and been avoiding them.

我已经看到在谷歌使用Makefiles的多种方法,但他们都有不同的复杂性。 。

I've seen multiple methods of using Makefiles on google, but they all differ in complexity...

我没有关于他们的线索或如何使用它们。感谢。

I don't have a clue about them or how to use them. Thanks.

编辑(重新输入):

对于明显的混乱,我的问题是(并始终是):

To summarise, due to the obvious confusion, my questions are (and always were):

1) Is there a good tutorial that comes recommended or is it easy to get started with Makefiles?
2) How do you launch and run an existing Makefile? And, 
3) Do Makefiles hold any vital data about the program or could I just use the `.h` and `.cpp` files that come with them in a new solution built from scratch?


推荐答案

何时使用 Makefile 是一个意见的问题。当然,您可以使用其他构建器(例如 omake ,但有很多其他)。你肯定希望一个建筑系统,只要你有多个翻译单位在你的软件。如果使用 make Makefile 通过详细描述依赖性来描述构建过程。通常,通过简单的 make 命令完成程序的构建。

The question of when to use Makefile is a matter of opinion. Certainly you can use some alternative builder (like e.g. omake, but there are many others). You certainly want some building system as soon as you have more than one translation unit in your software. If using make the Makefile describes the build process by detailing dependencies. Often, building you program is just done thru the simple make command.

cmake qmake automake 等... Makefile generator 。它们生成一个复杂的 Makefile ,通常不是使用GNU make的所有功能。

Some utilities, e.g. cmake, qmake, automake, etc... are Makefile generators. They generate a complex Makefile which is usually not using all the features of GNU make.

如果您决定使用 make ,我建议您使用 GNU make (这也是一个意见)。在这种情况下,请仔细阅读其文档,我觉得这些文档非常易读,其中包含好教程。不要忘记使用最新版本:GNU make的4.0版本于2013年10月发布,带来了很多有趣的新功能(插件,最重要的是扩展性通过

If you decide to use make, I recommend using GNU make (this is also an opinion). In that case, read carefully its documentation, which I find quite readable and which contains a good tutorial. Don't forget to use a recent version: the version 4.0 of GNU make was released in october 2013 and brings a lot of interesting new features (plugins, and most importantly extensibility thru guile) whih are interesting for complex builds.

调试复杂 Makefile -s考虑使用 remake (特别是 remake -x )。

To debug complex Makefile-s consider using remake (notably as remake -x).

实际上, GNU make 有很多内置规则。你可以使用 make -p ;因此请使用它们。

In practice, GNU make has a lot of built-in rules. You get them with make -p; so use them.

您还可以选择对 POSIX make 实用程序(这是一个标准的规范,但不如 GNU make ,它可以配置为遵循POSIX规范)。

You may also choose to code for the POSIX make utility (which is a standard specification, but less powerful than GNU make, which can be configured to follow the POSIX spec).

如何编码 Makefile ,以及软件复杂性。如果您有2或3个 *。cc 文件,其中有一个 .h (例如自动依赖关系,由GCC和提供)),这是值得使用的一个软件的许多源文件(有时生成C ++源代码,所以构建系统必须知道)。

How to code a Makefile is a matter of taste, and of software complexity. If you have 2 or 3 *.cc files with a single .h common header, you don't want all the fancy tricks (e.g. automatic dependencies, as provided by GCC and make) that are worth using on a software of many hundreds source files (and sometimes C++ source code is generated, so the build system has to know that).

这里是一个小的GNU Makefile 来帮助您:

Here is a tiny GNU Makefile to help you:

## file Makefile
CXX= g++
CXXSOURCES= main.cc other.cc
CXXOBJECTS= $(patsubst %.cc, %.o, $(CXXSOURCES))
OPTIMFLAGS= -g -O
PACKAGES= glib-2.0
PKGCONFIG= pkg-config
CXXFLAGS= -std=c++11 -Wall $(OPTIMFLAGS)
CPPFLAGS:=  $(shell $(PKGCONFIG) --cflags $(PACKAGES))
LIBES:= $(shell $(PKGCONFIG) --libs $(PACKAGES)) -ldl
.PHONY: all clean

all: myprog
myprog: $(CXXOBJECTS)
    $(LINK.cc) -rdynamic $^ $(LIBES) -o $@

$(CXXOBJECTS): yourheader.h

clean:
    $(RM) *.o *.so *.orig *~ myprog core* *.hh.gch
## eof Makefile

我不声称这是一个完美的例子(不是)。小心,表格对于 make 很重要,我无法在上面输入。您将使用 make all (或只是 make )来构建您的程序, make

I don't claim it is a perfect example (it is not). Be careful, tabulations are significant for make and I am not able to type them above. You'll use make all (or just make) to build your program, and make clean to remove non-source files.

对于不清楚的原因,有时 Makefile 可能使用自动工具 cmake生成。对于简单的程序,可能不值得学习如何使用它们。

For obscure reasons, sometimes Makefile may be generated using autotools or cmake. For simple programs, it might not worth the effort to learn how to use them.

有些情况下, GNU make 或适当的:在 MELT 中,我几乎放弃使用它(通过调用某些 autogen 从它生成shell脚本),因为它是一个引导语言,其中一组C ++文件是从一组MELT文件生成自己的,所以你有一个多个到多个准圆依赖关系。可能使用GUILE扩展可能有很多帮助。但这种噩梦的情况是罕见的。

There are some few cases when GNU make is not worthwhile or appropriate: in MELT I nearly gave up using it (by calling some autogen generated shell script from it) because it is a bootstrapped language where a set of C++ files is generating itself from a set of MELT files, so you have a multiple to multiple quasi-circular dependency relation. Probably using a GUILE extension could have helped a lot. But such nightmare situations are rare.

PS:我回答作为一个Linux用户;不知道什么应用 - 或者不是 - 我从来没有使用的Windows。另请参见有关Linux上的C或C ++编程的这些提示

PS2: Paul Evans的回答是相关的:不要自己写 Makefile ,复制一些现有的,以满足您的需要。

PS2: Paul Evans' answer is relevant: don't write your own Makefile, copy some existing one to suit your needs.

这篇关于Makefile,我如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆