非标准目录布局中的 makefile

发布于 2024-11-30 02:06:20 字数 576 浏览 0 评论 0原文

一个非常相似的问题,但与经常被问到的问题并不完全相同;我的项目具有以下布局(跨平台,因此不同的 IDE 等)

[Project]
   |
   |--- codeblocks
   |       |- obj
   |--- src
   |--- visual_studio

本质上,我试图获取我的 makefile(位于 codeblocks 目录中),编译 src 中包含的文件,并将目标文件放入进入obj目录。

编译方面我可以毫无问题地实现,但我一生都无法弄清楚如何告诉 make 使用此替代目录作为对象文件<的默认位置< /em>;之后我可以移动它们,但这意味着每次构建时我都必须重新编译整个应用程序,这会破坏 makefile 的对象。

通读 make 文档(是的,我确实首先尝试过,并在途中学到了一些额外的东西!),我遇到了一些示例,例如 $(OBJDIR)/%.o: %.c,但是 make 无法找到使用它的规则,而且我还没有足够的经验来自己解决这个问题:(

谁能指出我在正确的方向或提供一些合适的提示?

A very similar question, but not exact to those asked frequently; my project has the following layout (cross-platform, hence different IDEs, etc.)

[Project]
   |
   |--- codeblocks
   |       |- obj
   |--- src
   |--- visual_studio

Essentially, I'm trying to get my makefile (located in the codeblocks directory), to compile the files contained within src, and have the object files put into the obj directory.

The compilation side I can acheive with no problem, but I can't for the life of me figure out how to tell make to use this alternative directory as the default location for the object files; moving them afterwards I can do, but means I have to recompile the entire application each time I do a build, which defeats the object of the makefile.

Reading through the make documentation (yes, I did try first, and learnt some extra things on the way!), I came across some examples, such as
$(OBJDIR)/%.o: %.c, but make is unable to find the rule to utilize it, and I am nowhere near experienced enough to work it out for myself :(

Can anyone point me in the right direction or provide some suitable hints?

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

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

发布评论

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

评论(2

脸赞 2024-12-07 02:06:20

下面是一个简单的 Makefile 示例,演示了如何执行此操作。

SOURCES = $(wildcard src/*.c)
OBJECTS = $(SOURCES:src/%.c=obj/%.o)

obj/%.o: src/%.c
        $(CC) $(CFLAGS) -c -o $@ 
lt;

target.exe: $(OBJECTS)
        $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)

Makefile 所需的目录结构和文件是:

project/
project/Makefile
project/src/*.c
project/obj

我在我的机器上测试了它,它运行正确:

$ make
cc  -c -o obj/main.o src/main.c
cc -o target.exe obj/main.o  

Here is a simple example Makefile demonstrating how to do this.

SOURCES = $(wildcard src/*.c)
OBJECTS = $(SOURCES:src/%.c=obj/%.o)

obj/%.o: src/%.c
        $(CC) $(CFLAGS) -c -o $@ 
lt;

target.exe: $(OBJECTS)
        $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)

The directory structure and files needed by the Makefile are:

project/
project/Makefile
project/src/*.c
project/obj

I tested this on my machine and it ran correctly:

$ make
cc  -c -o obj/main.o src/main.c
cc -o target.exe obj/main.o  
浪推晚风 2024-12-07 02:06:20

如果您使用 GNU make,则可以使用您所描述的扩展模式规则。然后你可以有一个规则

$(OBJDIR)/%.o: $(SRCDIR)/%.c
        $(CC) $(CFLAGS) -c -o $@ $^

来编译源文件。额外的复杂性是您的链接规则还需要为所有目标文件指定 $(OBJDIR)。您可以使用类似的东西

SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

$(TARGET): $(OBJECTS)
        $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)

,或者,您可以使用VPATH来避免在所有这些地方需要$(SRCDIR)。

If you are using GNU make, you can use the extended pattern rules like you describe. You can then have a rule

$(OBJDIR)/%.o: $(SRCDIR)/%.c
        $(CC) $(CFLAGS) -c -o $@ $^

To compile the source files. The additional complication is that you linking rule needs to also specify $(OBJDIR) for all the object files. You can use something like

SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

$(TARGET): $(OBJECTS)
        $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)

Alternately, you can use VPATH to avoid the need for $(SRCDIR) in all these places.

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