gpt4 book ai didi

c++ - makefile中的多种编译模式

转载 作者:行者123 更新时间:2023-11-28 07:04:05 25 4
gpt4 key购买 nike

我想用一个make文件在多个模式下生成一个项目,然后每个模式在“正常”和“调试”模式下,即:

我有以下文件(实际上有更多文件,但这将有助于表明我的观点):

  • 内核/核心/main.cpp
  • 内核/处理器/Processor.cpp
  • kernel/processor/x86/Processor.cpp
  • 内核/处理器/x86_common/Processor.cpp
  • kernel/processor/x64/Processor.cpp

我希望能够通过以下方式使用我的 makefile:

make x86
(compiles all files except "kernel/processor/x64/Processor.cpp")
(enables the pre-processor directives X86 & X86_COMMON)

还有,

make x86debug
(compiles all files except "kernel/processor/x64/Processor.cpp")
(enables the pre-processor directives X86 & X86_COMMON & DEBUG)
(puts "-g -ggdb" infront of all gcc/g++/as arguments)

等等。

目前我有以下 makefile,虽然它可以工作,但只允许我在 x86 Debug模式下编译,现在我正在将我的软件移植到其他平台,我希望能够指定要构建的模式。

CC = i586-elf-g++
CFLAGS = -g -ggdb -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -std=gnu++11 -Isrc/system/include -DX86 -DX86_COMMON
LD = i586-elf-gcc
LDFLAGS = -g -ggdb -ffreestanding -O2 -nostdlib -lgcc
AS = i586-elf-as
ASFLAGS = -g -ggdb

OBJECTS = src/system/kernel/core/main.o
ALL_OBJECTS = $(OBJECTS) $(X86_OBJECTS)

X86COMMON_OBJECTS = src/system/kernel/core/processor/x86_common/Processor.o

X86_OBJECTS = $(X86COMMON_OBJECTS) src/system/kernel/core/processor/x86/boot.o
X86_LINKER = src/system/kernel/core/processor/x86/link.ld
X86_OUTPUT = bin/kernel_x86.bin

.PHONY: clean
clean: $(ALL_OBJECTS)
rm $(ALL_OBJECTS)

.PHONY: all
all: $(X86_OUTPUT)

$(X86_OUTPUT): $(X86_LINKER) $(OBJECTS) $(X86_OBJECTS)
$(LD) $(LDFLAGS) -T $(X86_LINKER) $^ -o $@

%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@

%.o: %.asm
$(AS) $(ASFLAGS) $< -o $@

您可能会说,我不是 make 方面的专家,因此我们将不胜感激任何帮助/想法。

最佳答案

CFLAGSLDFLAGS 中删除 -g,并添加以下 PHONY:

.PHONY: x86_debug
x86_debug: CFLAGS += -g
x86_debug: LDFLAGS += -g
x86_debug: $(X86_OUTPUT)

在正常模式下编译:make。要在 Debug模式下编译:make x86_debug

它可能不完全符合您的期望,但很容易修改

关于c++ - makefile中的多种编译模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22025330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com