How to compile and run C program using command line in Windows

Open a developer command prompt in Visual Studio 2022

If you’ve installed Visual Studio 2022 on Windows 10 or later, open the Start menu, and choose All apps. Then, scroll down and open the Visual Studio 2022 folder (not the Visual Studio 2022 app). Choose Developer Command Prompt for VS 2022 to open the command prompt window.


Can Visual Studio compile C++?

You can use Visual Studio to create Standard C++ programs. By following the steps in this walkthrough, you can create a project, add a new file to the project, modify the file to add C++ code, and then compile and run the program by using Visual Studio.

Installing the Needed Tools

I will start the project on Windows using VS Code and the Visual Studio Build Tools, but if you wish, you can start with a different IDE, or even a different operating system. In a later article, I will discuss using the gnu tools on Linux (Ubuntu).

So let’s begin.

Installing Visual Studio Code and Extensions

On Windows, the latest version of Visual Studio Code is available on its download page. Select the appropriate version; click on the Windows button for the x64 version, or one of the ARM links for ARM if that is applicable to you. The download will begin. When it is completed, run the downloaded file.

Next, we need two VSCode extensions. Start VS Code and display the extensions panel (select View → Extensions from the main menu). In the search box, enter C++. A number of C and C++ extensions are displayed. You want the one called C++. Make sure it is from Microsoft. This extension provides Intellisense, debugging, and browsing capabilities. Click on the Install button to install it.

The second extension is CMake Tools. Search for and install it.

Installing Visual Studio Build Tools

We need the build tools provided by Visual Studio. Don’t worry, we aren’t installing Visual Studio, just the build tools.

On the Visual Studio downloads page, move down into the All Downloads section. As I write this, the current version of Visual Studio is 2019, so I will be referring to it in this section. If a later version is available, use that instead. Select Tools for Visual Studio 2019. Click on the Download button for Build Tools for Visual Studio 2019. Download and save the file. When the download has completed, open the file. This starts Visual Studio Installer. Again, don’t worry, we are not installing Visual Studio, just the build tools. When the installer window opens select only the build tools. After some time (several minutes), the install will complete. Close the installer.

Open the Windows Start menu and start Developer Command Prompt for VS 2019; do not open the standard command prompt or Powershell. At the command prompt, enter:

The following should be displayed, although the version number may be different:

If the message says that it cannot find CMake, then the build tools did not install correctly.

You will almost always be starting VS Code from the command line of Developer Command Prompt, so you will probably want to add it to the Productivity section of the Start menu.

Installing Git

We will need git. If you have done any development work, you probably already have it installed. If not, Git for Windows is available here.

2. Unarchive the source code

Source code is often delivered as an archive because source code usually consists of multiple files. You have to extract it before interacting with it, whether it’s a tarball, a zip file, a 7z file, or something else entirely. 

Once you’ve unarchived it, change the directory into the extracted directory and have a look around. There’s usually a README file at the top level of the directory. This file, ideally, contains guidance on what you need to do to compile the code. The README often contains information on these important aspects of the code:

  • Language: What language the code is in (for instance, C, C++, Rust, Python).
  • Dependencies: What other software you need to have installed on your system for this application to build and run.
  • Instructions: The literal steps you need to take to build the software. Occasionally, they include this information within a dedicated file intuitively entitled INSTALL.

If the README file doesn’t contain that information, consider filing a bug report with the developer. You’re not the only one who needs an introduction to source code. Regardless of how experienced they are, everyone is new to source code they’ve never seen before, and documentation is important!

Angband’s maintainers link to online instructions to describe how to compile the code. This document also describes what other software you need to have installed, although it doesn’t exactly spell it out. The site says, “There are several different front ends that you can optionally build (GCU, SDL, SDL2, and X11) using arguments to configure such as --enable-sdl, --disable-x11, etc.” This may mean something to you or look like a foreign language, but this is the kind of stuff you get used to after compiling code frequently. Whether or not you understand what X11 or SDL2 is, they’re both requirements that you see pretty often after regularly compiling code over a few months. You get comfortable with the idea that most software needs other software libraries because they build upon other technologies. In this case, though, Angband is very flexible and compiles with or without these optional dependencies, so for now, you can pretend that there are no additional dependencies.

Using CMake With Visual Studio Code

Look at the status bar on the VS Code window. If it looks similar to this:

then terminate and restart VS Code. The status bar

then terminate and restart VS Code. The status bar should now look like this:

From left to right, master* indicates that you are

From left to right, master* indicates that you are editing the git master branch and that changes have been made. The symbols and 0s indicate that there are currently no errors in workspace. Next is a button that will run CMake (CMake: [Debug]). No Kit Selected indicates that the build tools have not yet been selected; we will get to them in a moment. Following that is a Build button, the default target, a bug button that will run the application in debug mode, and a button that will run the application without starting the debugger. The remainder of the status bar provides other information that we are not currently concerned with.

We first have to run CMake to create the build files. Click on the CMake: [Debug] button. The first time you do so, a list of build tool kits is displayed below the main menu. Select either Unspecified, or Visual Studio Build Tools 2019 Release - amd64. The No Kit Selected text in the status bar will change to [Visual Studio Build Tools 2019 Release - amd64], and a list of CMake configurations are displayed: Debug, Release, MinSizeRel, and RelWithDebInfo.

Select Debug. This will execute CMake and generate a Visual Studio solution file (.sln) and Visual Studio project files (.vcxproj) if there are no errors. If there are errors, then something is not right with the CMakeLists.txt files or the C++ source files.

If you selected any of the other CMake actions, the executable, library, and debug related files would be placed in other subdirectories. For example, if you build release versions, they will be placed in build/Release.

The first time you run debugging by either clicking on the Bug button, or by selecting Run → Start Debugging, a list of build environments will be displayed just below the main menu. Select C++ (Windows).

To do a clean and rebuild, all we have to do is delete the build directory and all of its contents, then run CMake and Build.

Can GCC compile C++?

GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc ). However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++ library.

6. Build PHP and Extension

Finally, run the nmake command to start the build of your extension with PHP within the directory of the PHP source code with the developer command prompt. NMake is a make utility provided by Microsoft and available in Visual Studio. It is a handy tool for creating automated builds.

Just run the command:

This will start the compilation and will take a while. It will generate a verbose output as well with warnings etc:

Once it finishes, a new directory will appear insi

Once it finishes, a new directory will appear inside the c:/php-src directory, namely Release or Release_TS according to the Thread Safety configuration. There you will find a very basic built of PHP, but most important, the reason why you exclusively built PHP, your extension DLL file (in our case php_apc.dll):

Explore IntelliSense

In your new helloworld.cpp file, hover over vector or string to see type information. After the declaration of the msg variable, start typing msg. as you would when calling a member function. You should immediately see a completion list that shows all the member functions, and a window that shows the type information for the msg object:

You can press the Tab  key to insert the selected

You can press the Tab key to insert the selected member; then, when you add the opening parenthesis, you will see information about any arguments that the function requires.


The final step is to install the code you’ve just compiled. There’s nothing magical about installing code. All that happens is that lots of files get copied to very specific directories. That’s true whether you’re compiling from source code or running a fancy graphical install wizard. Because the code is getting copied to system-level directories, you must have root (administrative) privileges, which get granted by the sudo command.

Simple build system to compile and run source code

After finishing writing the code, we now compile the code. We can build our own custom build system. Go to Tools –> Build System –> New Build System.... Use the following settings (I assume that you are using GCC to compile your code):

Save it and name it C++.sublime-build (build systems in Sublime Text all end with the extention sublime-build). Press Ctrl+Shift+B to select the build system.

Select the C++ build system we have just created a

Select the C++ build system we have just created and press Enter. Then the source file will be compiled and run. If everything goes well, you will see the output of this program in the bottom of Sublime Text window.

How to compile and run a C program using command line?

Once you created your first C program. It’s time for real action. A program is worthless until it is compiled and executed.

Read more about – The C compilation process

Example to compile and run above program

Before you compile and run the above C program. You must be in the same directory as of your C program. To test your program open command prompt and execute following commands.

3. Download PHP Source Code

Now that you have the necessary tools to compile PHP along with your extension, you will need as well the source code of PHP. You can get the source code of the PHP version that you need from the official releases webpage of PHP here.

Once you download the tar/zip file with the code, extract it into a directory with a short path to prevent any problem with the length of the paths in Windows. In our case, we’ll extract the source code of PHP 5.3.8 in the c:/php-src directory:

We'll run the commands to build PHP along with you

We’ll run the commands to build PHP along with your code in this directory within the command prompt later. It’s worth to mention that the ext folder will contain the code of your extension but we will add it in the next step.

Debuggingwith VS Code

This is all fine but now we would like to go one step further. In fact, it is time to debug our code…

To do so, in the Debug menu (ALT+D), select the Open Configurations option with the down arrow key

Then select C++ Windows

A launch.json file similar to the one below should appear

As requested on line 11, let’s modify the program entry. I propose the following modification :

On line 11, now « program » points to the debug version of our code, in the ./build/Debug sub-directory.

Save the launch.json file with CTRL + S

Before to launch a debug session, it is time to go back to the source code and to set a break point on line 4 for example (std::cout…)

Ready? Let’s strike F5…

If everything goes well an empty console should appear on screen. Much more important, the code should stop on line 4 and VS Code should looks like this :

This is a kind of little miracle… I really like that. The code is stopped. Look on the left side, bob is set to a weird value. Now, if we press F10, we move one step forward and the canonical « Hello World » appears in the console. Strike F10 again and bob variable is set to 3.

Starting from now you can edit the code, make all the modifications you want. When you want to debug, set a breakpoint and press CTRL + SHIF + B

Ok, let’s imagine we fixed all the issues in the code and that everything works as expected. Show times! Let’s compile a release version of the code

Next steps

  • Explore the VS Code User Guide.
  • Review the Overview of the C++ extension.
  • Create a new workspace, copy your .vscode JSON files to it, adjust the necessary settings for the new workspace path, program name, and so on, and start coding!