5

I'm having trouble porting a C++ library to Windows Phone via the command line with nmake. The makefile invokes cl.exe and link.exe using Visual Studio's x86_arm tools and has basic recipes.

I'm using a Visual Studio Phone project as a reference. Under Process Monitor, I noticed Visual Studio uses a response file rather than driving the compiler directly. That is, Visual Studio invoke cl.exe <response file>.

According to the Compiler Options, Response Files (the @ option):

A response file can contain any commands that you would specify on the command line. This can be useful if your command-line arguments exceed 127 characters.

This might be my problem with the port since my command line is over 500 characters (7+ lines with wrap in the console).

Visual Studio uses a temporary response file written to AppData file that is immediately deleted. I want to inspect Visual Studio's response file and try to use one.

Question: How can I capture or inspect a Visual Studio response file?

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

5

You can suppress the deletion of .rsp files by setting the environment variable MSBUILDPRESERVETOOLTEMPFILES to 1.

avakar
  • 32,009
  • 9
  • 68
  • 103
2

My VS projects don't seem to use response files (maybe the projects are too small?), however getting nmake to generate and use a response file is pretty easy using nmake's 'inline files'. The following is a simple makefile that will compile hello.c using a response file to the cl command line:

PROJNAME=hello

all:
    cl @<<
-c $(PROJNAME).c
-Ox
<<

Basically, put a << on the command that you want to pass the inline to, then after the command simply put the contents of the file and end the inline file with another << token as a delimiter.

running nmake on that makefile results in:

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

You can see that nmake macros are expanded in the inline file.

See the MSDN docs for more details (though the docs are pretty sparse - they could use an example or two).


Update:

And if you really want or need to capture the response file the VS is using and can't find another way to do it, compile the following program and replace the program that VS is running, for example cl.exe, with it (obviously stashing the original program file somewhere so you can reverse the operation):

#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;
}
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 1
    Thanks Michael. One of the folks on Super User suggested I remove the `DELETE` permission from the `AppData\Temp` directory, which seemed a little easier. Visual Studio complained, but it also allowed me to inspect the response file. Now if I could only find some documentation on the format.... – jww May 04 '14 at 09:22
  • "My VS projects don't seem to use response files" - When looking at the various log files, it does not appear so (even my don't appear so). However, observing with `ProcMon` will show they are there on the command line for both `cl.exe` and `link.exe`. – jww May 04 '14 at 09:25
  • By the way, I finally found these, but they are not well documented (as with inline files): [CL Command Files](http://msdn.microsoft.com/en-us/library/x2khzsa1.aspx) and [LINK Command Files](http://msdn.microsoft.com/en-us/library/9xch38h8.aspx). – jww May 04 '14 at 09:28
  • 2
    If you're using the nmake "inline" file mechanism, note that you can specify KEEP or NOKEEP after the closing << sequence. For example, < – UweBaemayr May 17 '16 at 17:10