nmake根据目标修改宏 [英] nmake modify macro based on target
问题描述
我有一个Makefile.mak,可以在其中基于我的基于C的源代码创建一个test.exe或DLL.我正在使用CL.EXE和NMAKE.
I've got a Makefile.mak where I optionally create a test.exe or a DLL from my C-based source code. I'm using CL.EXE and NMAKE.
我想修改我CFLAGS宏像这样当目标是TEST.EXE:
I'd like to modify my CFLAGS macro like this when the target is TEST.EXE:
CFLAGS = $(CFLAGS) -DMAIN
当然,我在C代码中使用了它:
And, of course, I use this in my C code:
#ifdef MAIN
... int main()... yada yada
#endif
我尝试过
!IF $@ == "test.exe"
但它坠毁并自$逻辑上并不工作@,目标,是不是在makefile中的一部分确定性.
but it crashed out and doesn't work logically since the $@, target, isn't deterministic in that part of the makefile.
定义附加宏的逻辑位置是在定义目标时,但是我不知道如何在不将NMAKE解释为DOS命令的情况下做到这一点.
The logical place to define the additional macro is when defining the target but I don't see how to do that without NMAKE interpreting it as DOS command.
test.exe: test.obj
CFLAGS = $(CFLAGS) -DMAIN
$(LINKER) /out:$@ $(LIB) $*.obj $(LIBS)
使用gmake会更容易,我知道.我没有那个选择.
It'd be easier with gmake, I know. I don't have that option.
推荐答案
我将提出两种解决方案:一个用来做你要求什么,即改性
I will present two solutions: one which does what you request, namely modifying CFLAGS
based on the target, and a second one which may be a better approach.
假设您有一个文件multiply.c
:
#include <stdio.h>
int multiply(int a, int b) {
return a * b;
}
#ifdef MAIN
int main() {
printf("Unit test: multiply(2, 3) = %d\n", multiply(2, 3));
}
#endif
您想添加到静态库
which you would like to add to a static library my_lib.lib
, and also use as a stand-alone unit test.
添加
One standard way of adding -DMAIN
to CFLAGS
is to use NMAKE recursively. The second invocation of NMAKE could use a different makefile or, as as presented here, use the same makefile with a flag to prevent an infinite recursive loop.
TARGETS = my_lib.lib multiply.exe
CFLAGS = -W4 -O2 -nologo -Zi
all: $(TARGETS)
my_lib.lib: multiply.obj
my_lib.lib:
lib -nologo -out:$@ $**
!ifndef RECURSE
multiply.exe:
nmake -nologo CFLAGS="-DMAIN $(CFLAGS)" RECURSE= $@
!endif
multiply.obj: .FORCE
.FORCE:
如果
If RECURSE
is defined, the built-in rule is used to create the test program multiply.exe
.
此解决方案有效.但它要求
This solution works. But it requires that multiply.obj
be remade every time it is used, because there are two versions of it floating around: one with a main
and one without.
第二种解决方案可以区分这些目标文件.
The second solution distinguishes between these object files.
TARGETS = my_lib.lib multiply.exe
CFLAGS = -W4 -O2 -nologo -Zi
all: $(TARGETS)
my_lib.lib: multiply.obj
multiply.exe: $*.TEST_obj
my_lib.lib:
lib -nologo -out:$@ $**
multiply.exe:
link -nologo -out:$@ $**
.c.TEST_obj:
$(CC) -DMAIN $(CFLAGS) -c $< -Fo$@
这给出了:
>nmake -nologo
cl -W4 -O2 -nologo -Zi /c multiply.c
multiply.c
lib -nologo -out:my_lib.lib multiply.obj
cl -DMAIN -W4 -O2 -nologo -Zi -c multiply.c -Fomultiply.TEST_obj
multiply.c
link -nologo -out:multiply.exe multiply.TEST_obj
试图直接从
Trying to create a .exe
file directly from the .c
file, as in:
.c.exe:
$(CC) -DMAIN $(CFLAGS) $<
不起作用,因为这仍然创建
does not work, because this still creates a .obj
file which clobbers the other version.
修改:强>
.SUFFIXES: .TEST_obj
does not seem to be needed.
这篇关于nmake根据目标修改宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!