两个编译器的Makefile问题

发布于 2024-11-05 19:13:52 字数 789 浏览 0 评论 0原文

我被要求编写一个 Makefile,需要在两个编译器之间进行选择,并且每个编译器都应该支持 3 个构建版本(调试、发布、测试)。

有很多变量会根据输入(编译器、编译器选项、输出目录、包含目录等)而变化。我的第一个选择是浏览特定于目标的变量并根据目标配置变量。你认为这是个好主意吗?

我对这类变量不是很熟悉。在我看来,如果我这样做:

release: variable1=value1   #release is target
release: variable2=value2

release: 
      # some compilation rule

只会配置 variable1 。我的说法正确吗?


更新

感谢您的回复。我正在尝试通过根据目标配置的附加变量来处理编译器选择问题。但是,问题就在这里。我有以下几行:

release: CFLAGS = -DCORE_SW_VERSION='"$(CORE_SW_VERSION)"' -Wall 
release: CFLAGS += -fgnu89-inline -mno-volatile-cache $(INCLUDE)  
release: TARGET=release

在这行之后,我执行一些 ifeq 序列,在其中决定使用哪个编译器(根据 TARGET 变量值)。 CFLAGS 配置正确,但 TARGET 变量为空。这使我得出结论:您只能配置一个特定于目标的变量。我说得对吗?如果没有,我不知道我做错了什么。请你帮助我好吗?

I am asked to write a Makefile which needs to selects between two compilers, and each of these compilers should support 3 build versions (debug, release, test).

There are a lot of variables that change based on input (compiler, compiler options, output directory, include directories etc). My first option was to go through target-specific variables and configure variables according to target. Do you think this is good idea?

I am not extremely familiar with those kind of variables. It seems to me that if I do something like this:

release: variable1=value1   #release is target
release: variable2=value2

release: 
      # some compilation rule

Only the variable1 will be configured. Am I right about this?


Update

Thank you for your reply. I am trying to deal with compiler selection issue through additional variable which would be configured according to target. But, here is the problem. I have the following lines:

release: CFLAGS = -DCORE_SW_VERSION='"$(CORE_SW_VERSION)"' -Wall 
release: CFLAGS += -fgnu89-inline -mno-volatile-cache $(INCLUDE)  
release: TARGET=release

After this lines, I do some ifeq sequence in which I decide which compiler to use (according to TARGET variable value).
And CFLAGS is configured properly, but the TARGET variable is empty. This leads me to conclusion that you can configure only one target-specific variable. Am I right? If not, I am not aware what I am doing wrong. Could you please help me?

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

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

发布评论

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

评论(1

扛起拖把扫天下 2024-11-12 19:13:52

仅在构建该目标以及该目标的任何先决条件时才定义特定于目标的变量。您不能在整个 makefile 中任意使用特定于目标的变量(因为听起来您正在尝试使用 ifeq)。为此,您可能需要查看 $(MAKECMDGOALS)。我不认为目标特定变量的数量有任何限制,当然不是一个限制。

需要特定于目标的变量或 $(MAKECMDGOALS) 可能是一个警告,表明您正在尝试强制 make 做一些它不应该做的事情。

我不清楚您是否想要构建三个版本(每个版本使用一个编译器进行调试/测试/发布),还是六个版本。假设有三个,这是一个 unix-y Makefile,根据目标使用不同的编译器和 CFLAGS 进行构建。但是,请注意,这可以很容易地使用 RELEASE_CFLAGSRELEASE_CCDEBUG_CFLAGS 等变量进行编码。

all: release debug test

release: CC=gcc
release: CFLAGS=
debug: CC=gcc
debug: CFLAGS=-g
test: CC=cc
test: CFLAGS=-Wall

.PHONY: release debug test
release: release/exe
debug: debug/exe
test: test/exe

OBJECTS := test.o

release/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ 
lt;

debug/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ 
lt;

test/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ 
lt;

release/exe: $(OBJECTS:%=release/%)
        $(CC) $(CFLAGS) -o $@ $^

debug/exe: $(OBJECTS:%=debug/%)
        $(CC) $(CFLAGS) -o $@ $^

test/exe: $(OBJECTS:%=test/%)
        $(CC) $(CFLAGS) -o $@ $^

Target-specific variables are defined only when building that target and any prerequisites of that target. You can't use target-specific variables arbitrarily throughout the makefile (as it sounds like you're trying to do with ifeq). For that, you may want to look at $(MAKECMDGOALS). I don't believe there is any limit on the number of target-specific variables, certainly not a limit of one.

Needing either target-specific variables or $(MAKECMDGOALS) may be a warning that you're trying to do coerce make into doing something it wasn't meant to do.

It's not clear to me whether you want to build three versions (debug/test/release with a single compiler for each one), or six versions. Assuming three, here is a unix-y Makefile to build with different compilers and CFLAGS depending on the target. However, note that this could just as easily be coded with RELEASE_CFLAGS, RELEASE_CC, DEBUG_CFLAGS, etc... variables.

all: release debug test

release: CC=gcc
release: CFLAGS=
debug: CC=gcc
debug: CFLAGS=-g
test: CC=cc
test: CFLAGS=-Wall

.PHONY: release debug test
release: release/exe
debug: debug/exe
test: test/exe

OBJECTS := test.o

release/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ 
lt;

debug/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ 
lt;

test/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ 
lt;

release/exe: $(OBJECTS:%=release/%)
        $(CC) $(CFLAGS) -o $@ $^

debug/exe: $(OBJECTS:%=debug/%)
        $(CC) $(CFLAGS) -o $@ $^

test/exe: $(OBJECTS:%=test/%)
        $(CC) $(CFLAGS) -o $@ $^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文