Jasoneer
Jasoneer

Reputation: 2088

Visual C++ Command Line Compiler (CL.EXE) Redirect OBJ Files

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?

Upvotes: 13

Views: 13783

Answers (3)

Lillian
Lillian

Reputation: 1

A way i do builds on my projects(when i work with windows of course) and because there's not many files is:

cl /MP /c <your c source files here> /Fo:.\obj\ /I .\include

so this way i create first object files into a directory like .\obj, normally CL.EXE automatically names these files with the name of the source files, so, no need to put any OUT_NAME and o link them all into a final .exe i just use:

cl <your obj source files here> /Fe:.\bin\<final binary name>.exe

this basically just link them all together.

i highly recommend checking the MS Docs, they can be really helpful for all kinds of things

hope this was helpful in some way!

Upvotes: -1

commanDOS
commanDOS

Reputation: 21

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.

Upvotes: 2

Adrian Ratnapala
Adrian Ratnapala

Reputation: 5713

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.

Upvotes: 18

Related Questions