Wednesday, April 10, 2019

Windows: Build C++ from command line

If you have Visual Studio 2017, to build a C++ project from a "normal" - not developer - command line:
  1. Add the following to system path: 
    1. To use cl.exe: c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\Hostx64\x64\
    2. To use vcvars64.bat: c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\
  2.  Open a command window and issue the following commands
    1. vcvars64
    2. If you are in a drive other than C:, let's say D:, you have to issue the command D: because after vcvars64, the drive reverts to C:
    3. cl a.cpp b.cpp
For example, in order to build mms-cpp, I created a batch with the following content:

set PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\Hostx64\x64\;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\
call vcvars64
d:
call cl API.cpp Main.cpp

Note that there should be no whitespace after "PATH=" and the additional two paths should not be enclosed with quotation marks.

No comments: