我的 VS 项目似乎不使用响应文件(也许项目太小?),但是使用 nmake 的“内联文件”很容易让nmake 生成和使用响应文件。以下是一个简单的 makefile,它将使用 cl 命令行的响应文件来编译 hello.c:
PROJNAME=hello
all:
cl @<<
-c $(PROJNAME).c
-Ox
<<
基本上,将<< 放在要传递内联的命令上,然后在命令之后简单地放置文件内容并用另一个<< 标记作为分隔符结束内联文件。
在该 makefile 上运行 nmake 会导致:
C:\temp>nmake
Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
cl @C:\Users\mikeb\AppData\Local\Temp\nmAC77.tmp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl -c hello.c
-Ox
hello.c
可以看到内联文件中展开了nmake宏。
有关更多详细信息,请参阅MSDN docs(尽管文档非常少 - 他们可以使用一两个示例)。
更新:
如果你真的想要或需要捕获 VS 正在使用的响应文件并且找不到其他方法来执行此操作,请编译以下程序并替换 VS 正在运行的程序,例如cl.exe,用它(显然将原始程序文件存储在某处,以便您可以反转操作):
#include <stdio.h>
void dump(char const* fname)
{
FILE* fp = NULL;
char buf[1000];
char* result = NULL;
printf("Dumping response file \"%s\":\n\n", fname);
fp = fopen(fname, "r");
if (!fp) return;
do {
result = fgets(buf, sizeof(buf), fp);
if (result) printf("%s", buf);
} while (result);
return;
}
int main(int argc, char** argv)
{
int i;
for (i = 1; i < argc; ++i) {
char* fname;
if (argv[i][0] != '@') {
// not a response file
continue;
}
fname = &(argv[i][1]);
dump(fname);
}
return 0;
}