Visual studio how to build

Visual studio how to build

Walkthrough: Working with Projects and Solutions (C++)

Here’s how to create a C++ project in Visual Studio, add code, and then build and run the project. The project in this walkthrough is a program that tracks how many players are playing different card games.

In Visual Studio, work is organized in projects and solutions. A solution can have more than one project—for example, a DLL and an executable that references that DLL. For more information, see Solutions and Projects.

Before you start

To complete this walkthrough, you need Visual Studio 2017 or later. If you need a copy, here’s a short guide: Install C++ support in Visual Studio. If you haven’t done it yet, follow the next steps after installation through the «Hello, World» tutorial to make sure the C++ components are installed correctly and it all works.

It helps if you understand the fundamentals of the C++ language, and know what a compiler, linker, and debugger are used for. The tutorial also assumes that you’re familiar with Windows and how to use menus, dialogs,

Create a project

To create a project, first choose a project-type template. For each project type, Visual Studio sets compiler settings and—depending on the type—generates starter code that you can modify later. The following steps vary depending on which version of Visual Studio you are using. To see the documentation for your preferred version of Visual Studio, use the Version selector control. It’s found at the top of the table of contents on this page.

To create a project in Visual Studio

From the main menu, choose File > New > Project to open the Create a New Project dialog box.

At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Console.

From the filtered list of project types, choose Console App then choose Next. In the next page, enter Game as the name for the project.

You can accept the default location in the Location drop-down list, enter a different location, or choose the Browse button to browse to a directory where you want to save the project.

When you create a project, Visual Studio puts the project in a solution. By default, the solution has the same name as the project. You can change the name in the Solution name box, but for this example, keep the default name.

Choose the Create button to create the project.

Visual Studio creates your new solution and project files, and opens the editor for the Game.cpp source code file it generated.

To create a project in Visual Studio 2017

On the menu bar, choose File > New > Project.

In the left pane of the New Project dialog box, expand Installed and select Visual C++, if it isn’t open already.

In the list of installed templates in the center pane, select Windows Console Application.

Enter a name for the project in the Name box. For this example, enter Game.

You can accept the default location in the Location drop-down list, enter a different location, or choose the Browse button to browse to a directory where you want to save the project.

When you create a project, Visual Studio puts the project in a solution. By default, the solution has the same name as the project. You can change the name in the Solution name box, but for this example, keep the default name.

Choose the OK button to create the project.

Visual Studio creates your new solution and project files, and opens the editor for the Game.cpp source code file it generated.

To create a project in Visual Studio 2015

On the menu bar, choose File > New > Project.

In the left pane of the New Project dialog box, expand Installed and select Visual C++, if it isn’t open already.

In the list of installed templates in the center pane, select Win32 Console Application.

Enter a name for the project in the Name box. For this example, enter Game.

You can accept the default location in the Location drop-down list, enter a different location, or choose the Browse button to browse to a directory where you want to save the project.

When you create a project, Visual Studio puts the project in a solution. By default, the solution has the same name as the project. You can change the name in the Solution name box, but for this example, keep the default name.

Choose the OK button to create the project.

Visual Studio creates your new solution and project files, and opens the editor for the Game.cpp source code file it generated.

Organize projects and files

You can use Solution Explorer to organize and manage the projects, files, and other resources in your solution.

To add a class to a project

If the Solution Explorer window isn’t displayed in Visual Studio, on the menu bar, choose View > Solution Explorer.

In Solution Explorer, select the Game project. On the menu bar, choose Project > Add Class.

In the Add Class dialog, enter Cardgame in the Class Name box. Don’t modify the default file names and settings. Choose the OK button.

Visual Studio creates new files and adds them to your project. You can see them in the Solution Explorer window. The Cardgame.h and Cardgame.cpp files are opened in the editor.

Edit the Cardgame.h file, and make these changes:

Add two private data members after the opening brace of the class definition.

Modify the default constructor that Visual Studio generated. After the public: access specifier, find the line that looks like:

After the default destructor, add an inline declaration for a static int member function named GetParticipants that takes no parameters and returns the totalParticipants value.

static int GetParticipants()

The Cardgame.h file should resemble the code below after you change it:

The line #pragma once tells the compiler to include the header file only one time. For more information, see once. For information about other C++ keywords in the header file above, see class, int, static, and public.

Choose the Cardgame.cpp tab at the top of the editing pane to open it for editing.

Delete everything in the file and replace it with the code:

You can use auto-completion when you are entering code. For example, if you enter this code at the keyboard, you can enter pl or tot and then press Ctrl+Spacebar. Auto-completion enters players or totalParticipants for you.

Add test code to your main function

Add some code to your app that tests the new functions.

To add test code to the project

In the Game.cpp editor window, replace the existing code with:

Build and run your app project

Next, build the project and run the app.

To build and run the project

On the menu bar, choose Build > Build Solution.

Output from a build is displayed in the Output window. If your build is successful, the output should resemble:

The Output window can show different steps, depending on the build configuration, but if the project build succeeds, the last line should resemble the output shown.

If your build didn’t succeed, compare your code to the code that is shown in the earlier steps.

To run the project, on the menu bar, choose Debug > Start Without Debugging. A console window should appear, and the output should resemble:

Press a key to dismiss the console window.

Congratulations, you’ve successfully built an app project and solution. Continue the walkthrough to learn more about how to build C++ code projects in Visual Studio.

Walkthrough: Create an MSBuild project file from scratch

For information about creating a project file for a C++ project, see MSBuild (C++).

This walkthrough shows how to create a basic project file incrementally, by using only a text editor. The walkthrough follows these steps:

Extend the PATH environment variable.

Create a minimal application source file.

Create a minimal MSBuild project file.

Build the application by using the project file.

Add properties to control the build.

Control the build by changing property values.

Add targets to the build.

Control the build by specifying targets.

This walkthrough shows how to build the project at the command prompt and examine the results. For more information about MSBuild and how to run MSBuild at the command prompt, see Walkthrough: Use MSBuild.

To complete the walkthrough, you must have Visual Studio installed because it includes MSBuild and the Visual C# compiler, which are required for the walkthrough.

Extend the path

Before you can use MSBuild, you must extend the PATH environment variable to include all the required tools. You can use the Developer Command Prompt for Visual Studio. Search for it on Windows 10 in the search box in the Windows task bar. To set up the environment in an ordinary command prompt or in a scripting environment, run VSDevCmd.bat in the Common7/Tools subfolder of a Visual Studio installation.

Create a minimal application

This section shows how to create a minimal C# application source file by using a text editor.

At the command prompt, browse to the folder where you want to create the application, for example, \My Documents\ or \Desktop\.

Type md HelloWorld to create a subfolder named \HelloWorld\.

Type cd HelloWorld to change to the new folder.

Start Notepad or another text editor, and then type the following code.

Save this source code file and name it Helloworld.cs.

Build the application by typing csc helloworld.cs at the command prompt.

Test the application by typing helloworld at the command prompt.

The Hello, world! message should be displayed.

Delete the application by typing del helloworld.exe at the command prompt.

Create a minimal MSBuild project file

Now that you have a minimal application source file, you can create a minimal project file to build the application. This project file contains the following elements:

The required root Project node.

An ItemGroup node to contain item elements.

An item element that refers to the application source file.

A Target node to contain tasks that are required to build the application.

A Task element to start the Visual C# compiler to build the application.

To create a minimal MSBuild project file

In the text editor, create a new file and enter these two lines:

Insert this ItemGroup node as a child element of the Project node:

Notice that this ItemGroup already contains an item element.

Insert this task element as a child element of the Target node:

Save this project file and name it Helloworld.csproj.

Your minimal project file should resemble the following code:

Tasks in the Build target are executed sequentially. In this case, the Visual C# compiler Csc task is the only task. It expects a list of source files to compile, and this is given by the value of the Compile item. The Compile item references just one source file, Helloworld.cs.

In the item element, you can use the asterisk wildcard character (*) to reference all files that have the .cs file name extension, as follows:

Build the application

Now, to build the application, use the project file that you just created.

This builds the Build target of the Helloworld project file by invoking the Visual C# compiler to create the Helloworld application.

Test the application by typing helloworld.

The Hello, world! message should be displayed.

You can see more details about the build by increasing the verbosity level. To set the verbosity level to «detailed», type this command at the command prompt:

Add build properties

You can add build properties to the project file to further control the build. Now add these properties:

An AssemblyName property to specify the name of the application.

An OutputPath property to specify a folder to contain the application.

To add build properties

Delete the existing application by typing del helloworld.exe at the command prompt.

In the project file, insert this PropertyGroup element just after the opening Project element:

Add this task to the Build target, just before the Csc task:

The MakeDir task creates a folder that is named by the OutputPath property, provided that no folder by that name currently exists.

Add this OutputAssembly attribute to the Csc task:

This instructs the Visual C# compiler to produce an assembly that is named by the AssemblyName property and to put it in the folder that is named by the OutputPath property.

Save your changes.

Your project file should now resemble the following code:

We recommend that you add the backslash (\) path delimiter at the end of the folder name when you specify it in the OutputPath element, instead of adding it in the OutputAssembly attribute of the Csc task. Therefore,

Test the build properties

Now you can build the application by using the project file in which you used build properties to specify the output folder and application name.

This creates the \Bin\ folder and then invokes the Visual C# compiler to create the MSBuildSample application and puts it in the \Bin\ folder.

To verify that the \Bin\ folder has been created, and that it contains the MSBuildSample application, type dir Bin.

Test the application by typing Bin\MSBuildSample.

The Hello, world! message should be displayed.

Add build targets

Next, add two more targets to the project file, as follows:

A Clean target that deletes old files.

A Rebuild target that uses the DependsOnTargets attribute to force the Clean task to run before the Build task.

Now that you have multiple targets, you can set the Build target as the default target.

To add build targets

In the project file, add these two targets just after the Build target:

The Clean target invokes the Delete task to delete the application. The Rebuild target does not run until both the Clean target and the Build target have run. Although the Rebuild target has no tasks, it causes the Clean target to run before the Build target.

Add this DefaultTargets attribute to the opening Project element:

This sets the Build target as the default target.

Your project file should now resemble the following code:

Test the build targets

You can exercise the new build targets to test these features of the project file:

Building the default build.

Setting the application name at the command prompt.

Deleting the application before another application is built.

Deleting the application without building another application.

To test the build targets

To verify that the \Bin\ folder contains both the MSBuildSample application and the new Greetings application, type dir Bin.

Test the Greetings application by typing Bin\Greetings.

The Hello, world! message should be displayed.

To verify that the \Bin\ folder is now empty, type dir Bin.

Type msbuild.

Although a project file is not specified, MSBuild builds the helloworld.csproj file because there is only one project file in the current folder. This causes the MSBuildSample application to be created in the \Bin\ folder.

To verify that the \Bin\ folder contains the MSBuildSample application, type dir Bin.

Build incrementally

You can tell MSBuild to build a target only if the source files or target files that the target depends on have changed. MSBuild uses the time stamp of a file to determine whether it has changed.

To build incrementally

In the project file, add these attributes to the opening Build target:

This specifies that the Build target depends on the input files that are specified in the Compile item group, and that the output target is the application file.

The resulting Build target should resemble the following code:

Remember that helloworld.csproj is the default project file, and that Build is the default target.

The -v:d switch specifies a verbose description for the build process.

These lines should be displayed:

Skipping target «Build» because all output files are up-to-date with respect to the input files.

Input files: HelloWorld.cs

Output files: BinMSBuildSample.exe

MSBuild skips the Build target because none of the source files have changed since the application was last built.

C# example

The following example shows a project file that compiles a C# application and logs a message that contains the output file name.

Visual Basic example

The following example shows a project file that compiles a Visual Basic application and logs a message that contains the output file name.

What’s next?

Visual Studio can automatically do much of the work that is shown in this walkthrough. To learn how to use Visual Studio to create, edit, build, and test MSBuild project files, see Walkthrough: Use MSBuild.

Understand build configurations

You need build configurations when you need to build your projects with different settings. For example, Debug and Release are configurations and different compiler options are used accordingly when building them. One configuration is active and is indicated in the command bar at the top of the IDE.

Visual studio how to build. Смотреть фото Visual studio how to build. Смотреть картинку Visual studio how to build. Картинка про Visual studio how to build. Фото Visual studio how to build

Visual studio how to build. Смотреть фото Visual studio how to build. Смотреть картинку Visual studio how to build. Картинка про Visual studio how to build. Фото Visual studio how to build

This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see Build configurations in Visual Studio for Mac.

The configuration and the platform control where built output files are stored. Normally, when Visual Studio builds your project, the output is placed in a project subfolder named with the active configuration (for example, bin/Debug/x86), but you can change that.

You can create your own build configurations at the solution and project level. The solution configuration determines which projects are included in the build when that configuration is active. Only the projects that are specified in the active solution configuration will be built. If multiple target platforms are selected in Configuration Manager, all projects that apply to that platform are built. The project configuration determines what build settings and compiler options are used when you build the project.

To create, select, modify, or delete a configuration, you can use the Configuration Manager. To open it, on the menu bar, choose Build > Configuration Manager, or just type Configuration in the search box. You can also use the Solution Configurations list on the Standard toolbar to select a configuration or open the Configuration Manager.

Visual studio how to build. Смотреть фото Visual studio how to build. Смотреть картинку Visual studio how to build. Картинка про Visual studio how to build. Фото Visual studio how to build

If you can’t find solution configuration settings on the toolbar and can’t access the Configuration Manager, it might be because you’re using Visual Basic development settings. For more information, see How to: Manage configurations with Visual Basic developer settings applied.

By default, Debug and Release configurations are included in projects that are created by using Visual Studio templates. A Debug configuration supports the debugging of an app, and a Release configuration builds a version of the app that can be deployed. For more information, see How to: Set debug and release configurations. You can also create custom solution configurations and project configurations. For more information, see How to: Create and edit configurations.

Solution configurations

A solution configuration specifies how projects in the solution are to be built and deployed. To modify a solution configuration or define a new one, in the Configuration Manager, under Active solution configuration, choose Edit or New.

Each entry in the Project contexts box in a solution configuration represents a project in the solution. For every combination of Active solution configuration and Active solution platform, you can set how each project is used. (For more information about solution platforms, see Understand build platforms.)

When you define a new solution configuration and select the Create new project configurations check box, Visual Studio automatically assigns the new configuration to all of the projects. Likewise, when you define a new solution platform and select the Create new project platforms check box, Visual Studio automatically assigns the new platform to all of the projects. Also, if you add a project that targets a new platform, Visual Studio adds that platform to the list of solution platforms and assigns it to all of the projects. You can still modify the settings for each project.

The active solution configuration also provides context to the IDE. For example, if you’re working on a project and the configuration specifies that it will be built for a mobile device, the Toolbox displays only items that can be used in a mobile device project.

Project configurations

The configuration and platform that a project targets are used together to specify the build settings and compiler options to use when it’s built. A project can have different settings for each combination of configuration and platform. To modify the properties of a project, open the shortcut menu for the project in Solution Explorer, and then choose Properties. At the top of the Build tab of the project designer, choose an active configuration to edit its build settings.

Visual studio how to build. Смотреть фото Visual studio how to build. Смотреть картинку Visual studio how to build. Картинка про Visual studio how to build. Фото Visual studio how to build

Visual studio how to build. Смотреть фото Visual studio how to build. Смотреть картинку Visual studio how to build. Картинка про Visual studio how to build. Фото Visual studio how to build

Building multiple configurations

When you build a solution using the Build > Build Solution command, Visual Studio only builds the active configuration. All projects that are specified in that solution configuration are built, and the only project configuration that’s built is that one specified in the active solution configuration and active solution platform, which is shown in the toolbar in Visual Studio. For example, Debug and x86. Other defined configurations and platforms are not built.

How Visual Studio assigns project configurations

When you define a new solution configuration and don’t copy settings from an existing one, Visual Studio uses the following criteria to assign default project configurations. The criteria are evaluated in the order shown.

If a project has a configuration name (

) that exactly matches the name of the new solution configuration, that configuration is assigned. Configuration names are not case-sensitive.

If the project has a configuration name in which the configuration-name part matches the new solution configuration, that configuration is assigned, whether the platform portion matches or not.

If there is still no match, the first configuration that’s listed in the project is assigned.

How Visual Studio assigns solution configurations

When you create a project configuration (in the Configuration Manager, by choosing New on the drop-down menu in the Configuration column for that project) and select the Create new solution configurations check box, Visual Studio looks for a like-named solution configuration to build the project on each platform it supports. In some cases, Visual Studio renames existing solution configurations or defines new ones.

Visual Studio uses the following criteria to assign solution configurations.

If a project configuration doesn’t specify a platform or specifies just one platform, then a solution configuration whose name matches that of the new project configuration is either found or added. The default name of this solution configuration does not include a platform name; it takes the form

If a project supports multiple platforms, a solution configuration is either found or added for each supported platform. The name of each solution configuration includes both the project configuration name and the platform name, and has the form

Compile and build in Visual Studio

For a first introduction to building within the IDE, see Walkthrough: Building an application.

You can use any of the following methods to build an application: the Visual Studio IDE, the MSBuild command-line tools, and Azure Pipelines:

Build MethodBenefits
IDE— Create builds immediately and test them in a debugger.
— Run multi-processor builds for C++ and C# projects.
— Customize different aspects of the build system.
CMake— Build projects using the CMake tool
— Use the same build system across Linux and Windows platforms.
MSBuild command line— Build projects without installing Visual Studio.
— Run multi-processor builds for all project types.
— Customize most areas of the build system.
Azure Pipelines— Automate your build process as part of a continuous integration/continuous delivery pipeline.
— Apply automated tests with every build.
— Employ virtually unlimited cloud-based resources for build processes.
— Modify the build workflow and create build activities to perform deeply customized tasks.

The documentation in this section goes into further details of the IDE-based build process. For more information on the other methods, see CMake, MSBuild and Azure Pipelines, respectively.

This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see Compile and build in Visual Studio for Mac.

Overview of building from the IDE

When you create a project, Visual Studio created default build configurations for the project and the solution that contains the project. These configurations define how the solutions and projects are built and deployed. Project configurations in particular are unique for a target platform (such as Windows or Linux) and build type (such as debug or release). You can edit these configurations however you like, and can also create your own configurations as needed.

For a first introduction to building within the IDE, see Walkthrough: Building an application.

From there, you can explore a variety of other tasks:

Walkthrough: Using MSBuild to Create a Visual C++ Project

This walkthrough demonstrates how to use MSBuild in a command prompt to build a Visual Studio C++ project. You’ll learn how to create an XML-based .vcxproj project file for a Visual C++ console application. After building the project, you’ll learn how to customize the build process.

This walkthrough illustrates these tasks:

Creating the C++ source files for your project.

Creating the XML MSBuild project file.

Using MSBuild to build your project.

Using MSBuild to customize your project.

Prerequisites

You need these prerequisites to complete this walkthrough:

A copy of Visual Studio with the Desktop development with C++ workload installed.

A general understanding of the MSBuild system.

Create the C++ source files

In this walkthrough, you’ll create a project that has a source file and a header file. The source file main.cpp contains the main function for the console application. The header file main.h contains code to include the header file. You can create these C++ files by using Visual Studio or a text editor such as Visual Studio Code.

To create the C++ source files for your project

Create a folder for your project.

Create a file named main.cpp and add this code to the file:

Create a file named main.h and add this code to the file:

Creating the XML MSBuild Project File

An MSBuild project file is an XML file that contains a project root element (

). In the example project you’ll build, the

element contains seven child elements:

Three item group tags ( ) that specify project configuration and platform, source file name, and header file name.

Three import tags ( ) that specify the location of Microsoft Visual C++ settings.

A property group tag (

) that specifies project settings.

To create the MSBuild project file

element shown here. (Use ToolsVersion=»14.0″ if you’re using Visual Studio 2015, ToolsVersion=»15.0″ if you’re using Visual Studio 2017, or ToolsVersion=»16.0″ if you’re using Visual Studio 2019.)

Insert the elements in the next procedure steps between the root

child elements in an element. The child element specifies debug and release configurations for a 32-bit Windows operating system:

Add an element that specifies the path of the default C++ settings for this project:

Add a property group element (

) that specifies two project properties, and

. (Use v140 as the

value if you’re using Visual Studio 2015, v141 if you’re using Visual Studio 2017, or v142 if you’re using Visual Studio 2019.)

Add an element that specifies the path of the current C++ settings for this project:

Add a child element in an element. The child element specifies the name of the C/C++ source file to compile:

is a build target and is defined in the default targets folder.

Add a child element in an element. The child element specifies the name of the header file for the C/C++ source file:

Add an element that specifies the path of the file that defines the target for this project:

Complete Project File

This code shows the complete project file that you created in the previous procedure. (Use ToolsVersion=»15.0″ for Visual Studio 2017, or ToolsVersion=»14.0″ for Visual Studio 2015.)

Using MSBuild to Build Your Project

Enter this command at the command prompt to build your console application:

msbuild myproject.vcxproj /p:configuration=debug

MSBuild creates a folder for the output files, and then compiles and links your project to generate the Myproject.exe program. After the build process finishes, use this command to run the application from the debug folder:

The application should display «Hello, from MSBuild!» in the console window.

Customizing Your Project

MSBuild enables you to execute predefined build targets, apply user-defined properties, and use custom tools, events, and build steps. This section illustrates these tasks:

Using MSBuild with build targets.

Using MSBuild with build properties.

Using MSBuild with the 64-bit compiler and tools.

Using MSBuild with different toolsets.

Adding MSBuild customizations.

Using MSBuild with Build Targets

A build target is a named set of predefined or user-defined commands that can be executed during the build. Use the target command-line option ( /t ) to specify a build target. For the myproject example project, the predefined clean target deletes all files in the debug folder and creates a new log file.

At the command prompt, enter this command to clean myproject :

Using MSBuild with Build Properties

The property command-line option ( /p ) enables you to override a property in your project build file. In the myproject example project, the release or debug build configuration is specified by the Configuration property. The operating system that you’ll use to run the built application is specified by the Platform property.

At the command prompt, enter this command to create a debug build of the myproject application to run on 32-bit Windows:

msbuild myproject.vcxproj /p:configuration=debug /p:platform=win32

At the command prompt, enter this command to create a release build that runs on 64-bit Windows:

msbuild myproject.vcxproj /p:configuration=release /p:platform=x64

At the command prompt, enter this command to create a release build for myplatform :

msbuild myproject.vcxproj /p:configuration=release /p:platform=myplatform

Using MSBuild with the 64-bit Compiler and Tools

If you have installed Visual Studio on 64-bit Windows, the 64-bit x64 native and cross tools are installed by default. You can configure MSBuild to use the 64-bit compiler and tools to build your application by setting the PreferredToolArchitecture property. This property doesn’t affect the project configuration or platform properties. By default, the 32-bit version of the tools is used. To specify the 64-bit version of the compiler and tools, add this property group element to the Myproject.vcxproj project file after the Microsoft.Cpp.default.props file element:

At the command prompt, enter this command to use the 64-bit tools to build your application:

msbuild myproject.vcxproj /p:PreferredToolArchitecture=x64

Using MSBuild with a different toolset

If you have the toolsets and libraries for other versions of Visual C++ installed, MSBuild can build applications for either the current Visual C++ version or for the other installed versions. For example, if you have installed Visual Studio 2012, to specify the Visual C++ 11.0 toolset for Windows XP, add this property group element to the Myproject.vcxproj project file after the Microsoft.Cpp.props file element:

To rebuild your project with the Visual C++ 11.0 Windows XP toolset, enter this command:

msbuild myproject.vcxproj /p:PlatformToolset=v110_xp /t:rebuild

Adding MSBuild customizations

MSBuild provides various ways to customize your build process. These articles show how to add custom build steps, tools, and events to your MSBuild project:

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *