Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Rust Go Linux CPU Others Videos
Advertisement

Article by Ayman Alheraki on January 11 2026 10:36 AM

Building C++ Projects with MSVC Without Build Systems

Building C++ Projects with MSVC Without Build Systems

Microsoft Visual C++ (MSVC) is a powerful compiler that integrates with Visual Studio but can also be used independently via the Developer Command Prompt or PowerShell. While most large C++ projects use CMake or MSBuild, it is entirely possible to build and manage projects manually using MSVC’s cl.exe compiler.

This guide walks through using MSVC to compile small and large C++ projects, link libraries, enable C++20, C++23, and C++26, optimize builds, and automate the process with scripts.

Setting Up MSVC Command Line

Before compiling, ensure you have MSVC installed.

Open the Developer Command Prompt

To use MSVC’s compiler (cl.exe), open:

  • x64 Native Tools Command Prompt for VS

  • x86 Native Tools Command Prompt for VS (for 32-bit builds)

Alternatively, run this in PowerShell:

This sets up the correct environment variables for cl.exe.

Compiling a Single C++ File

For a basic program:

This produces main.exe.

Enabling a Specific C++ Standard (C++20, C++23, C++26)

MSVC defaults to C++14. Use /std:c++20 or newer for modern C++ features:

C++23 Features in MSVC:

  • std::expected for error handling.

  • std::mdspan for multi-dimensional views.

  • std::print for modern-style printing.

C++26 Features (Experimental)

  • Deduction in this pointer.

  • Pattern Matching (if approved).

Compiling Multiple Files

For larger projects with multiple source files (main.cpp and helper.cpp):

This compiles and links both files.

Using Object Files (.obj) for Incremental Compilation

Instead of recompiling everything:

  • /c → Only compiles to .obj (no linking).

  • /Fe:output.exe → Sets the executable name.

Managing Large Projects Without Build Systems

For large projects, organize files properly:

Compiling with Include Paths (/I)

If header files are in include/:

Compiling with Object Files

For efficiency, compile separately:

Linking Libraries in MSVC

Linking Standard Libraries (Math, Threads)

Linking a Static Library (.lib)

Linking a Dynamic Library (.dll)

First, ensure you have mylib.lib and mylib.dll, then:

Place mylib.dll in the same folder as my_program.exe for runtime linking.

Enabling Optimizations and Debugging

Optimization Levels

  • /O1 → Optimize for size.

  • /O2 → Optimize for speed.

  • /Ox → Maximum optimizations.

  • /GL → Whole-program optimization.

Example:

Debugging with /Zi and /FS

  • /Zi → Debugging symbols.

  • /FS → Multi-threaded compilation.

  • /Od → Disable optimizations for debugging.

Example:

Cross-Compiling with MSVC

Compiling for 32-bit Windows

Compiling for 64-bit Windows

Targeting ARM64 (Windows on ARM)

Automating Builds with a Batch Script**

Instead of manually compiling files, use a batch file (build.bat):

Example: build.bat

Run it:

When to Use a Build System (CMake/MSBuild)

While manual builds work for many projects, a build system becomes necessary for:

  • Cross-platform support.

  • Dependency management.

  • Automating complex builds.

However, understanding MSVC without CMake is crucial for:

  • Debugging build errors.

  • Creating simple builds.

  • Learning how compilers work internally.

Conclusion

MSVC provides powerful tools to build C++ programs without relying on CMake or MSBuild. By using cl.exe, you can:

  • Compile simple and large projects manually.

  • Optimize builds for performance.

  • Link external libraries (.lib and .dll).

  • Automate builds with batch scripts.

This method is useful for learning, debugging, and small projects, but for large applications, CMake or MSBuild is recommended.

Advertisements

Responsive Counter
General Counter
1272706
Daily Counter
1260