nmake根据目标修改宏 [英] nmake modify macro based on target

查看:231
本文介绍了nmake根据目标修改宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

添加是递归地使用NMAKE的一种标准方法. NMAKE的第二次调用可以使用不同的生成文件,或者如这里所提出,使用相同生成文件用标志,以防止无限递归循环.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆