12

The compiler (CL.EXE) can take multiple source files, but likes to generate all the OBJ files in the directory that it is invoked. I couldn't find the compiler flag to set an output directory but I did find one for an individual OBJ, but it can't take multiple sources.

Without having to specify each file to redirect the output and having lots of targets for NMAKE, is there an easy way to do it through CL?

7
  • 2
    The /Fo option was made to do this. Use the IDE's C/C++, Command Line page to see what it does. Commented Oct 9, 2011 at 21:27
  • 2
    I thought I already tried that. The MSDN made it look like it could only be used on one source file. Could you provide an example?
    – Jasoneer
    Commented Oct 9, 2011 at 21:29
  • You don't really need me to post a screenshot of the Command Line settings page for a project do you? Surely you can create you own IDE project? Commented Oct 9, 2011 at 21:34
  • I don't have the IDE installed, only the Windows SDK. Can't you copy and paste the command line the IDE generates?
    – Jasoneer
    Commented Oct 9, 2011 at 21:55
  • Use the Express edition, it is free. Commented Oct 9, 2011 at 22:02

2 Answers 2

17

It turns out the /Fo option actually works, but the directory you specify must end with a backslash. Thus

cl  /Fo.\obj\  -c foo.c fee.c

Works but cl /Fo.\obj -c ... would fail.

1
  • 2
    I've been finding it actually has to end with two backslashes.
    – user673679
    Commented Jun 28, 2019 at 11:19
2

Just to add to the only answer. In case when the obj path is quoted, the trailing backslash has to be either added after the path closing quote, or escaped if added before the quote.

cl  /Fo"quoted path\obj"\  -c foo.c fee.c

OR

cl  "/Foquoted path\obj"\  -c foo.c fee.c

OR

cl  /Fo"quoted path\obj\\"  -c foo.c fee.c

Speaking of NMAKE, similar syntax is expected when passing quoted macro values on NMAKE command line. The trailing backslash seems to be the crucial bit to watch for.

nmake SOMEDIR="quoted path\obj"\

OR

nmake SOMEDIR="quoted path\obj\\"

OR

nmake "SOMEDIR=quoted path\obj"

NOT

nmake SOMEDIR="quoted path\obj\"

as this would result in an escaped quote \" and would grab whatever else followed on the command line and put it into $(SOMEDIR). Took me a while to diagnose such a behavior, hope this would save time to someone else.

1
  • Quoting the path seems to change it to an absolute path, so it seems necessary to add .` to the front for a relative path. Also, it seems like cl` doesn't want to create the directory structure itself for some reason: stackoverflow.com/questions/56805311/…
    – user673679
    Commented Jun 28, 2019 at 11:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.