Sometimes using the visual-studio to build a project is just overkill and You might as-well just build the application without all these unnecessary GUI configuration. I’ve been in that situation multiple times and you know I don’t like all these fancy tools and the less abstraction is better in my opinion.
So I decided to document this process of setting up the environment so I could reference it later when I have to setup a new box or just switch between virtual machines.
As far as I know you can also download and install msvc without having to install the whole visual-studio stuff which is pretty nice. But that’s off topic (Although I might write a blog post about it).
System-Dependent Environment Variables #
Some of the variables used in this post is system dependent and I would recommend you to use the “Developer Command-Prompt” (or Powershell) to get the value of these variables.
Powershell syntax:
echo $env:VARIABLE
Command-Prompt syntax:
echo "%VARIABLE%"
Using The Official VsDevCmd.bat
#
Microsoft provides an official VsDevCmd.bat
and a bunch
of other scripts to initialize the environment. The
VsDevCmd.bat
is located at
%VSINSTALLDIR%\Common7\Tools
and (as far as I know) you
can just run it to get the exact same setup as the Developer
Command-Prompt (or Powershell).
To initialize the environment for an specific architecture you can
use the scripts provided under the
%VSINSTALLDIR%\VC\Auxiliary\Build
directory. mainly:
-
%VSINSTALLDIR%\VC\Auxiliary\Build\vcvarsall.bat
%VSINSTALLDIR%\VC\Auxiliary\Build\vcvars64.bat
%VSINSTALLDIR%\VC\Auxiliary\Build\vcvars32.bat
All of the vcvars
batch scripts call the
VsDevCmd.bat
with specific parameters to setup the
development environment for the given architecture.
NOTE: Running the VsDevCmd.bat
scripts
will send telemetry to microsoft. Fortunatly you have the option to
opt-out by setting the
VSCMD_SKIP_SENDTELEMETRY
environment variable to
something.
Setting-Up The Environment Manually #
Although it’s not recommended, but you might want to just set it up by yourself, and I don’t blame you this also what I like to do.
To setup the environment manually you basically need to add 3
different variables INCLUDE
(include path),
LIB
(library path) and also you need to change the
PATH
environment variable to include the
cl.exe
& link.exe
directory.
There are multiple ways to set these variables, personally I just
use the setx
from the command-prompt. If you have no
idea what you’re doing I would recommend you to search for the
“Edit Environment Variables” in the windows search bar
and use the GUI application to set these variables. This makes it
easier to work with the multi-value environment variables.
Include path (INCLUDE
):
-
%VSINSTALLDIR%\VC\Tools\MSVC\%VCToolsVersion%\include
-
%VSINSTALLDIR%\VC\Tools\MSVC\%VCToolsVersion%\ATLMFC\include
-
%WindowsSdkDir%\include\%WindowsSdkVersion%\ucrt
%WindowsSdkDir%\include\%WindowsSdkVersion%\um
-
%WindowsSdkDir%\include\%WindowsSdkVersion%\shared
-
%WindowsSdkDir%\include\%WindowsSdkVersion%\winrt
-
%WindowsSdkDir%\include\%WindowsSdkVersion%\cppwinrt
Library path (LIB
):
-
%VSINSTALLDIR%\VC\Tools\MSVC\%VCToolsVersion%\ATLMFC\lib\%VSCMD_ARG_TGT_ARCH%
-
%VSINSTALLDIR%\VC\Tools\MSVC\%VCToolsVersion%\lib\%VSCMD_ARG_TGT_ARCH%
-
%WindowsSdkDir%\lib\%WindowsSdkVersion%\ucrt\%VSCMD_ARG_TGT_ARCH%
-
%WindowsSdkDir%\lib\%WindowsSdkVersion%\um\%VSCMD_ARG_TGT_ARCH%
Path Environment variable:
-
%VSINSTALLDIR%\VC\Tools\MSVC\%VCToolsVersion%\bin\Host%PLATFORM%\%VSCMD_ARG_TGT_ARCH%
-
%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin
-
%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja
%VSINSTALLDIR%\MSBuild\Current\Bin\amd64
%VSINSTALLDIR%\VC\vcpkg
%WindowsSdkDir%\Windows Performance Toolkit\
%WindowsSdkDir%\bin\%WindowsSdkVersion%\x86
Some of the path I’ve provided are not actually necessary but
I decided to provide these path’s since one might need it. The
most important one is probably the first one, which contains the
cl.exe
and link.exe
.
REFERENCES #
- Original Microsoft documentation about using MSVC from command-line: https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line
- Original Microsoft guide for compiling a C program: https://learn.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line