简单的 makefile 生成实用程序?
有谁知道有一个工具可以通过扫描目录中的源文件来生成 makefile?
这可能很天真:
- 不需要检测外部依赖项,
- 使用默认的编译器/链接器设置
Does anyone know of a tool that generates a makefile by scanning a directory for source files?
It may be naive:
- no need to detect external dependencies
- use default compiler/linker settings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以编写一个
Makefile
来为您执行此操作:只需将其放在源层次结构的根目录中并运行
make
(您将需要 GNU Make 才能实现此功能)。(请注意,我不太熟悉
Makefile
ish,所以也许这可以更容易地完成。)You can write a
Makefile
that does this for you:Just place this in root directory of your source hierarchy and run
make
(you'll need GNU Make for this to work).(Note that I'm not fluent in
Makefile
ish so maybe this can be done easier.)CMake 可以做到这一点,甚至可以创建 makefile 和 Visual Studio 项目。 http://www.cmake.org/
您所需要做的就是创建一个包含以下内容的 CMakeLists.txt 文件:下面几行:
然后进入一个干净的目录并输入:
这将在该干净的构建目录上创建 makefile。
CMake does it and it even creates makefiles and Visual Studio projects. http://www.cmake.org/
All you need to do is creating a CMakeLists.txt file containing the follwing lines:
Then go into a clean directory and type:
That will create makefiles on that clean build directory.
这就是我将用于一个简单项目的内容:
唯一的限制是,如果您正在构建一个名为 myApp 的可执行文件。然后源文件之一应命名为 myApp.cpp(这是我放置 main 的位置)。
This is what I would use for a simple project:
The only restriction is that if you are building an executable called myApp. Then one of the source files should be named myApp.cpp (which is where I put main).
有一个非常古老的脚本,称为“makedepend”,用于制作非常简单的 makefile。从那以后我几乎所有的事情都改用 cmake 了。
这是 wiki 文章 http://en.wikipedia.org/wiki/Makedepend,请注意底部的替代方案列表包括 automake 中的 depcomp 和 gcc 中的 -M 标志。
编辑:正如有人在另一个问题中向我指出的那样,
gcc -MM *.cpp > > Makefile
生成一个相当不错的简单 makefile。您只需在前面添加 CPPFLAGS 和用于构建整个二进制文件的规则...其形式为:There's a very old script called 'makedepend' that used to make very simple makefiles. I've since switched over to cmake for almost everything.
Here's the wiki article http://en.wikipedia.org/wiki/Makedepend, note the list of Alternatives at the bottom including depcomp in automake, and the -M flag in gcc.
EDIT: As someone pointed out to me in another question,
gcc -MM *.cpp > Makefile
produces a rather nice simple makefile. You only have to prepend your CPPFLAGS and a rule for constructing the entire binary... which will take the form:那么为什么要编写脚本呢?假设您的所有项目源文件都是
*.cpp
并且位于当前目录中:Makefile 会将使用默认编译器/链接器设置的所有源文件构建为以当前目录名称命名的可执行文件。
否则,我通常建议人们尝试 SCons 而不是更简单直观的地方。额外的好处是,无需手动编写
clean
目标,源/标头依赖性检查是内置的,它是本机递归的并支持正确的库。Why script then? Provided that all your project source files are
*.cpp
and in current directory:The Makefile would build the all the source files with default compiler/linker settings into an executable named after the name of the current directory.
Otherwise, I generally recommend people to try SCons instead of make where it is much simpler and intuitive. Added bonus that there is no need to code manually
clean
targets, source/header dependency checking is built-in, it is natively recursive and supports properly libraries.如链接讨论中所述,HWUT 是一个工具
可以生成漂亮的 Makefiles,搜索依赖项并将文件包含在您告诉它的目录中。在 Windows 上,您需要安装 MinGW 和 Ctags。在 Linux 下,gcc 和 ctags 最有可能存在。它是开源的并且可以免费使用。
特别是,当为某些大型项目的一些现有模块生成单元测试且内聚力较差时,此功能可以轻松地为您节省数小时甚至数天的时间。
As described in the linked discussion, HWUT is a tool that
can generate pretty Makefiles, searching for dependencies and include files in directories that you tell it. On windows you need to install MinGW and Ctags. Under Linux gcc and ctags are most likely present. It is OpenSource and free to use.
Especially, when generating Unit Tests for some already existing modules of some larger project with bad cohesion, this feautures easily spares you hours or even days.