How to Compile with MSVC via Command Line

It is not always necessary to use the Visual Studio debugger on Windows.


How to Compile with MSVC via Command Line


Using the command line formerly seemed to be a nerd thing, but over time it was noticed that it is much more practical and easier than the graphical mode, in addition to having a Huge performance gain!

In this article we will see that you can use the cl command from the MSVC compiler from Microsoft to compile your programs!

ATTENTION

Pictures are not in English, but you can understand how the corresponding path in your language!


01. Open Developer PowerShell

First of all make sure you have Visual Studio installed.

After creating your project, which does not necessarily need to have been created in Visual Studio, it can be in VS Code, Vim, Code::Blocks and others.

To compile, just search for the word Developer PowerShell in the Windows Start Bar and choose the option: Developer PowerShell for VS 2019, remembering that in this case refers to the 2019 version of Visual Studio, but if you have the other version, the year/version number will appear. And click Open.

MSVC


02. Navigate to your C/C++ file

In the image there is the use of the commands cd, ls and cat by PowerShell, but use Developer PowerShell.

MSVC


03. Compile your C/C++ file(s)

To compile just use the cl command, as shown below:

cl main.cpp

And then to run, run the file name with a backslash:

.\main.exe

MSVC


04. Using Parameters for MSVC

You can also create any binary name you like similar to GCC and Clang:

cl main.cpp -o program
.\program.exe

MSVC

There are also other parameters, however, the model used by MSCV is not always the use of dash - but a slash, example for Wall, O2 and others:

cl /Wall /O2 main.cpp -o output
.\output.exe

MSVC

For more parameters run the command:

cl -help

MSVC


Alternative within Visual Studio

You can also open Developer PowerShell or even CMD through Visual Studio, saving time-consuming use of the Graphical Debugger.

To do this, just click on: Tools > Command Line > Developer PowerShell or Developer Command Prompt: MSVC

And use the same procedures listed above: MSVC


For more information go to: MSVC address.


windows cpp cppdaily


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles