与任意许多主电源的制作file。如何用一个命令编译所有主电源?

发布于 2025-01-27 09:11:57 字数 1735 浏览 1 评论 0原文

我仍然很新的模式模式。我一直在基于模板制作和项目结构的项目进行项目,但是我对所有Makefile指令都在做什么。

我想对此进行修改,以便在 /src /mains中的所有.c文件,每个文件带有不同的主呼叫都可以编译为自己的可执行文件。

原始模板没有 /src /mains文件夹, /src中只有一个文件可以包含一个主函数调用。

首先,项目结构

root
|
|------ Makefile 
|
|------ build
|        |
|        |--- apps
|        |      |--- prog
|        |      |--- hello
|        |
|        |---- objects
|               |--- *.o
|
|----- include
|        |--- *.h
|                
|------ src
         |--- *.c // for each .h (no main calls)
         |--- mains
                |--- prog.c (call to main)
                |--- hello.c (call to different main)

当前Makefile

而无需手动为每种品牌编写新的链接规则,我发现没有成功。我只是要在此处留下清洁,未修改的makefile,我想更改此操作,以便它自动将所有文件构建为“电源”到 /build /apps

现在,它使可执行的可执行文件称为prog in /build /build /apps ,使用来自 /src的主要函数调用,

CXX      := gcc
CXXFLAGS := -pedantic-errors -Wall -Wextra -Werror
LDFLAGS  := -lm
BUILD    := ./build
OBJ_DIR  := $(BUILD)/objects
APP_DIR  := $(BUILD)/apps
TARGET   := prog                 # OP: this probably needs to become a wildcard, right?
INCLUDE  := -Iinclude/
SRC      := $(wildcard src/*.c)
OFLAGS   := -O1 -flto

OBJECTS  := $(SRC:%.c=$(OBJ_DIR)/%.o)
DEPENDENCIES := $(OBJECTS:.o=.d)

all: build $(APP_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %.c
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -MMD -o $@ $(OFLAGS)

$(APP_DIR)/$(TARGET): $(OBJECTS)
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)

-include $(DEPENDENCIES)

build:
    @mkdir -p $(APP_DIR)
    @mkdir -p $(OBJ_DIR)

clean:
    -@rm -rvf $(OBJ_DIR)/*
    -@rm -rvf $(APP_DIR)/*

如果您可以直接修改Makefile,那将是很棒的,我也很感激任何解释,因此下次我不需要模板。

另外,您想如何构建项目?

谢谢!

I am still newish to makefile patterns. I have been working on a project based on a template makefile and project structure, however I have a good sense of what all the makefile instructions are doing.

I would like to modify this so that all the .c files in /src/mains, each with a different main call gets compiled to its own executable.

The original template had no /src/mains folder, and only one file in the /src could contain a main function call.

First the project structure

root
|
|------ Makefile 
|
|------ build
|        |
|        |--- apps
|        |      |--- prog
|        |      |--- hello
|        |
|        |---- objects
|               |--- *.o
|
|----- include
|        |--- *.h
|                
|------ src
         |--- *.c // for each .h (no main calls)
         |--- mains
                |--- prog.c (call to main)
                |--- hello.c (call to different main)

Current Makefile

Without manually writing new linking rules for each make, I found no success. I am just going to leave the cleaner, unmodified makefile here, I'd like to change this so that it automatically builds all the files in mains to /build/apps

As of right now it makes an executable called prog in /build/apps, using a main function call from within /src

CXX      := gcc
CXXFLAGS := -pedantic-errors -Wall -Wextra -Werror
LDFLAGS  := -lm
BUILD    := ./build
OBJ_DIR  := $(BUILD)/objects
APP_DIR  := $(BUILD)/apps
TARGET   := prog                 # OP: this probably needs to become a wildcard, right?
INCLUDE  := -Iinclude/
SRC      := $(wildcard src/*.c)
OFLAGS   := -O1 -flto

OBJECTS  := $(SRC:%.c=$(OBJ_DIR)/%.o)
DEPENDENCIES := $(OBJECTS:.o=.d)

all: build $(APP_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %.c
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) $(INCLUDE) -c 
lt; -MMD -o $@ $(OFLAGS)

$(APP_DIR)/$(TARGET): $(OBJECTS)
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)

-include $(DEPENDENCIES)

build:
    @mkdir -p $(APP_DIR)
    @mkdir -p $(OBJ_DIR)

clean:
    -@rm -rvf $(OBJ_DIR)/*
    -@rm -rvf $(APP_DIR)/*

If you could modify the Makefile outright that would be great, I would also appreciate any explanation so I don't need a template next time.

Also more generally, How do you like to structure your projects?

Thanks!

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

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

发布评论

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

评论(1

锦欢 2025-02-03 09:11:57

MAKE根本不知道编程语言。它只是一种接收您一组规则和依赖树的工具。

因此,创建一个将所有程序作为依赖项的目标。将其添加到.phony目标,因为它不是真实的文件。

还要编写您的规则,以便每个程序都可以独立于其他程序构建。如果您的程序是使用通用模块构建的,则可以具有协同效应。

选项-p-n-d有助于调试Makefile。一如既往,阅读手册和教程是必须的。

Make has no knowledge of programming languages at all. It is simply a tool that receives your set of rules and dependency trees.

So create a target that has all your programs as dependencies. Add it to the .PHONY target, as it is not a real file.

Also write your rules so that each program can be built independently from the others. You can have synergies, if your programs are built with common modules.

The options -p, -n and -d are helpful to debug the Makefile. Reading the manual and tutorials is a must, as always.

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