简单的 makefile 生成实用程序?

发布于 2024-09-16 06:54:26 字数 115 浏览 4 评论 0原文

有谁知道有一个工具可以通过扫描目录中的源文件来生成 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 技术交流群。

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

发布评论

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

评论(6

递刀给你 2024-09-23 06:54:26

您可以编写一个 Makefile 来为您执行此操作:

SOURCES=$(shell find . -name "*.cpp")
OBJECTS=$(SOURCES:%.cpp=%.o)
TARGET=foo

.PHONY: all
all: $(TARGET)

$(TARGET): $(OBJECTS)
        $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@

.PHONY: clean
clean:
        rm -f $(TARGET) $(OBJECTS)

只需将其放在源层次结构的根目录中并运行 make (您将需要 GNU Make 才能实现此功能)。

(请注意,我不太熟悉 Makefileish,所以也许这可以更容易地完成。)

You can write a Makefile that does this for you:

SOURCES=$(shell find . -name "*.cpp")
OBJECTS=$(SOURCES:%.cpp=%.o)
TARGET=foo

.PHONY: all
all: $(TARGET)

$(TARGET): $(OBJECTS)
        $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@

.PHONY: clean
clean:
        rm -f $(TARGET) $(OBJECTS)

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 Makefileish so maybe this can be done easier.)

-柠檬树下少年和吉他 2024-09-23 06:54:26

CMake 可以做到这一点,甚至可以创建 makefile 和 Visual Studio 项目。 http://www.cmake.org/

您所需要做的就是创建一个包含以下内容的 CMakeLists.txt 文件:下面几行:

file(GLOB sources *.h *.c *.cxx *.cpp *.hxx)
add_executable(Foo ${sources})

然后进入一个干净的目录并输入:

cmake /path/to/project/

这将在该干净的构建目录上创建 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:

file(GLOB sources *.h *.c *.cxx *.cpp *.hxx)
add_executable(Foo ${sources})

Then go into a clean directory and type:

cmake /path/to/project/

That will create makefiles on that clean build directory.

爱的十字路口 2024-09-23 06:54:26

这就是我将用于一个简单项目的内容:

CC               = $(CXX)
CXXFLAGS        += -ansi -pedantic -W -Wall -Werror
CPPFLAGS        += -I<Dir Where Boost Lives>


SOURCES          = $(wildcard *.cpp)
OBJECTS          = $(patsubst %.cpp,%.o,$(SOURCES))

all:             myApp
myApp:           $(OBJECTS)

唯一的限制是,如果您正在构建一个名为 myApp 的可执行文件。然后源文件之一应命名为 myApp.cpp(这是我放置 main 的位置)。

This is what I would use for a simple project:

CC               = $(CXX)
CXXFLAGS        += -ansi -pedantic -W -Wall -Werror
CPPFLAGS        += -I<Dir Where Boost Lives>


SOURCES          = $(wildcard *.cpp)
OBJECTS          = $(patsubst %.cpp,%.o,$(SOURCES))

all:             myApp
myApp:           $(OBJECTS)

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).

谢绝鈎搭 2024-09-23 06:54:26

有一个非常古老的脚本,称为“makedepend”,用于制作非常简单的 makefile。从那以后我几乎所有的事情都改用 cmake 了。

这是 wiki 文章 http://en.wikipedia.org/wiki/Makedepend,请注意底部的替代方案列表包括 automake 中的 depcomp 和 gcc 中的 -M 标志。

编辑:正如有人在另一个问题中向我指出的那样, gcc -MM *.cpp > > Makefile 生成一个相当不错的简单 makefile。您只需在前面添加 CPPFLAGS 和用于构建整个二进制文件的规则...其形式为:

CPPFLAGS=-Wall
LDFLAGS=-lm
all: binary_name
binary_name: foo.o bar.o baz.o biff.o

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:

CPPFLAGS=-Wall
LDFLAGS=-lm
all: binary_name
binary_name: foo.o bar.o baz.o biff.o
2024-09-23 06:54:26
  • 无需检测外部依赖项
  • 使用默认编译器/链接器设置

那么为什么要编写脚本呢?假设您的所有项目源文件都是 *.cpp 并且位于当前目录中:

all: $(notdir $(CURDIR))
$(notdir $(CURDIR)): $(subst .cpp,.o,$(wildcard *.cpp))
        $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@

Makefile 会将使用默认编译器/链接器设置的所有源文件构建为以当前目录名称命名的可执行文件。

否则,我通常建议人们尝试 SCons 而不是更简单直观的地方。额外的好处是,无需手动编写clean目标,源/标头依赖性检查是内置的,它是本机递归的并支持正确的库。

  • no need to detect external dependencies
  • use default compiler/linker settings

Why script then? Provided that all your project source files are *.cpp and in current directory:

all: $(notdir $(CURDIR))
$(notdir $(CURDIR)): $(subst .cpp,.o,$(wildcard *.cpp))
        $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@

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.

恰似旧人归 2024-09-23 06:54:26

如链接讨论中所述,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.

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