Редагувати

Поділитися через


C/C++ projects and build systems in Visual Studio

You can use Visual Studio to edit, compile, and build any C++ code base with full IntelliSense support without having to convert that code into a Visual Studio project or compile with the MSVC toolset. For example, you can edit a cross-platform CMake project in Visual Studio on a Windows machine, then compile it for Linux using g++ on a remote Linux machine.

C++ compilation

To build a C++ program means to compile source code from one or more files and then link those files into an executable file (.exe), a dynamic-load library (.dll) or a static library (.lib).

Basic C++ compilation involves three main steps:

  • The C++ preprocessor transforms all the #directives and macro definitions in each source file. This creates a translation unit.
  • The C++ compiler compiles each translation unit into object files (.obj), applying whatever compiler options have been set.
  • The linker merges the object files into a single executable, applying the linker options that have been set.

The MSVC toolset

The Microsoft C++ compiler, linker, standard libraries, and related utilities make up the MSVC compiler toolset (also called a toolchain or "build tools"). These are included in Visual Studio. You can also download and use the command-line toolset as a free standalone package. For more information, see Build Tools for Visual Studio on the Visual Studio Downloads page.

You can build simple programs by invoking the MSVC compiler (cl.exe) directly from the command line. The following command accepts a single source code file, and invokes cl.exe to build an executable called hello.exe:

cl /EHsc hello.cpp

Here the compiler (cl.exe) automatically invokes the C++ preprocessor and the linker to produce the final output file. For more information, see Building on the command line.

Build systems and projects

Most real-world programs use some kind of build system to manage complexities of compiling multiple source files for multiple configurations (debug vs. release), multiple platforms (x86, x64, ARM, and so on), custom build steps, and even multiple executables that must be compiled in a certain order. You make settings in a build configuration file(s), and the build system accepts that file as input before it invokes the compiler. The set of source code files and build configuration files needed to build an executable file is called a project.

The following list shows various options for Visual Studio Projects - C++:

  • create a Visual Studio project by using the Visual Studio IDE and configure it by using property pages. Visual Studio projects produce programs that run on Windows. For an overview, see Compiling and Building in the Visual Studio documentation.

  • open a folder that contains a CMakeLists.txt file. CMake support is integrated into Visual Studio. You can use the IDE to edit, test, and debug without modifying the CMake files in any way. This enables you to work in the same CMake project as others who might be using different editors. CMake is the recommended approach for cross-platform development. For more information, see CMake projects.

  • open a loose folder of source files with no project file. Visual Studio will use heuristics to build the files. This is an easy way to compile and run small console applications. For more information, see Open Folder projects.

  • open a folder that contains a makefile, or any other build system configuration file. You can configure Visual Studio to invoke any arbitrary build commands by adding JSON files to the folder. For more information, see Open Folder projects.

  • Open a Windows makefile in Visual Studio. For more information, see NMAKE Reference.

MSBuild from the command line

You can invoke MSBuild from the command line by passing it a .vcxproj file along with command-line options. This approach requires a good understanding of MSBuild, and is recommended only when necessary. For more information, see MSBuild.

In This Section

Visual Studio projects
How to create, configure, and build C++ projects in Visual Studio using its native build system (MSBuild).

CMake projects
How to code, build, and deploy CMake projects in Visual Studio.

Open Folder projects
How to use Visual Studio to code, build, and deploy C++ projects based on any arbitrary build system, or no build system at all.

Release builds
How to create and troubleshoot optimized release builds for deployment to end users.

Use the MSVC toolset from the command line
Discusses how to use the C/C++ compiler and build tools directly from the command line rather than using the Visual Studio IDE.

Building DLLs in Visual Studio
How to create, debug, and deploy C/C++ DLLs (shared libraries) in Visual Studio.

Walkthrough: Creating and Using a Static Library
How to create a .lib binary file.

Building C/C++ Isolated Applications and Side-by-side Assemblies
Describes the deployment model for Windows Desktop applications, based on the idea of isolated applications and side-by-side assemblies.

Configure C++ projects for 64-bit, x64 targets
How to target 64-bit x64 hardware with the MSVC build tools.

Configure C++ projects for ARM processors
How to use the MSVC build tools to target ARM hardware.

Optimizing Your Code
How to optimize your code in various ways including program guided optimizations.

Configuring Programs for Windows XP
How to target Windows XP with the MSVC build tools.

C/C++ Building Reference
Provides links to reference articles about program building in C++, compiler and linker options, and various build tools.