Skip to content

Commit 2d34739

Browse files
author
Colin Robertson
committed
Merge branch 'cr-workitems' of https://github.com/MicrosoftDocs/cpp-docs-pr into cr-workitems
2 parents 594cb5a + 10276fe commit 2d34739

25 files changed

+458
-40
lines changed

docs/TOC.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# [Data Access](data/data-access-in-cpp.md)
1717
# [Text and Strings](text/text-and-strings-in-visual-cpp.md)
1818
# [vcpkg: A Package Manager for C++](vcpkg.md)
19-
# [Using the Visual Studio IDE for C++](ide/ide-and-tools-for-visual-cpp-development.md)
19+
# [IDE and Tools for Visual C++](ide/ide-and-tools-for-visual-cpp-development.md)
2020
# [Compilers and Build Tools](build/building-c-cpp-programs.md)
2121
# [Porting and Upgrading Visual C++ Code](porting/visual-cpp-porting-and-upgrading-guide.md)
2222
# [Security Best Practices for C++](security/security-best-practices-for-cpp.md)

docs/ide/TOC.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
## [Move Function Definition](refactoring/move-definition-location.md)
99
## [Rename](refactoring/rename.md)
1010
# [Visual C++ Tools and Features in Visual Studio Editions](visual-cpp-tools-and-features-in-visual-studio-editions.md)
11-
# [Creating and Managing Visual C++ Projects](creating-and-managing-visual-cpp-projects.md)
12-
## [Visual C++ Project Types](visual-cpp-project-types.md)
13-
## [Using Visual C++ Add New Item Templates](using-visual-cpp-add-new-item-templates.md)
11+
# [CMake and other non-MSBuild projects in Visual C++](non-msbuild-projects.md)
12+
## [CMake projects in Visual C++](cmake-tools-for-visual-cpp.md)
13+
# [MSBuild Projects in Visual C++](creating-and-managing-visual-cpp-projects.md)
14+
## [Project Types in Visual C++](visual-cpp-project-types.md)
15+
## [Add New Item Templates in Visual C++](using-visual-cpp-add-new-item-templates.md)
1416
## [File Types Created for Visual C++ Projects](file-types-created-for-visual-cpp-projects.md)
1517
### [Project and Solution Files](project-and-solution-files.md)
1618
### [Precompiled Header Files](precompiled-header-files.md)

docs/ide/building-cpp-projects-in-visual-studio.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ In the Visual Studio integrated development environment (IDE), there are several
7979
Presents the XML structure of a .vcxproj file.
8080

8181
## Related Sections
82-
[VC++ Directories, Projects, Options Dialog Box](http://msdn.microsoft.com/en-us/e027448b-c811-4c3d-8531-4325ad3f6e02)
83-
Discusses how to modify the search path for executable files, include files, library files, and source code files during a build
82+
[VC++ Directories, Projects, Options Dialog Box](vcpp-directories-property-page.md)
83+
(MSBuild projects only) Discusses how to modify the search path for executable files, include files, library files, and source code files during a build.
8484

8585
[Compiling and Building](/visualstudio/ide/compiling-and-building-in-visual-studio)
8686
Provides information on building within Visual Studio.
@@ -94,8 +94,8 @@ In the Visual Studio integrated development environment (IDE), there are several
9494
[Upgrading Projects from Earlier Versions of Visual C++](../porting/upgrading-projects-from-earlier-versions-of-visual-cpp.md)
9595
Provides links to topics covering issues on upgrading Visual C++ 6.0 and later projects to Visual C++ .NET.
9696

97-
[Porting and Upgrading Programs](http://msdn.microsoft.com/en-us/c36c44b3-5a9b-4bb4-9b7a-469aa770ed00)
98-
Provides details about porting applications and discusses makefiles.
97+
[Visual C++ Porting and Upgrading Guide](../porting/visual-cpp-porting-and-upgrading-guide.md)
98+
Detailed information about how to upgrade C++ applications that were created in earlier versions of Visual Studio, and also how to migrate applications that were created by using tools other than Visual Studio.
9999

100100
## See Also
101101
[Roadmap for Windows Store apps using C++](http://msdn.microsoft.com/en-us/0b71e4a4-5d8a-4a20-b2ec-e40062675ec1)
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
---
2+
title: "CMake Projects in Visual C++ | Microsoft Docs"
3+
ms.custom: ""
4+
ms.date: "08/08/2017"
5+
ms.reviewer: ""
6+
ms.suite: ""
7+
ms.technology:
8+
- "cpp-ide"
9+
ms.tgt_pltfrm: ""
10+
ms.topic: "article"
11+
dev_langs:
12+
- "C++"
13+
helpviewer_keywords:
14+
- "CMake in Visual C++"
15+
ms.assetid: 444d50df-215e-4d31-933a-b41841f186f8
16+
author: "mikeblome"
17+
ms.author: "mblome"
18+
manager: "ghogen"
19+
translation.priority.ht:
20+
- "de-de"
21+
- "es-es"
22+
- "fr-fr"
23+
- "it-it"
24+
- "ja-jp"
25+
- "ko-kr"
26+
- "ru-ru"
27+
- "zh-cn"
28+
- "zh-tw"
29+
translation.priority.mt:
30+
- "cs-cz"
31+
- "pl-pl"
32+
- "pt-br"
33+
- "tr-tr"
34+
---
35+
# CMake projects in Visual C++
36+
CMake is a cross-platform, open-source tool for defining build processes that run on multiple platforms. Until recently, Visual Studio users could use CMake to generate MSBuild project files, which the IDE then consumed for IntelliSense, browsing, and compilation. Starting in Visual Studio 2017, the **Visual C++ Tools for CMake** component uses the "Open Folder" feature to enable the IDE to consume CMake project files (such as CMakeLists.txt) directly for the purposes of IntelliSense and browsing. If you use a Visual Studio generator, a temporary project file is generated and passed to msbuild.exe, but is never loaded for IntelliSense or browsing purposes.
37+
38+
**Visual Studio 2017 version 15.3 and later**: Support is provided for both Ninja and Visual Studio generators.
39+
40+
## Installing
41+
Visual C++ Tools for CMake is installed by default as part of the Desktop development with C++ workload.
42+
43+
![CMake component in C++ Desktop workload](media/cmake-install.png)
44+
45+
## IDE Integration
46+
When you choose **File | Open | Folder** to open a folder containing a CMakeLists.txt file, the following things happen:
47+
- Visual Studio adds a **CMake** menu item to the main menu, with commands for viewing and editing CMake scripts.
48+
- **Solution Explorer** displays the folder structure and files.
49+
- Visual Studio runs CMake.exe and generates the CMake cache for the default x86 Debug configuration. The CMake command line is displayed in the **Output Window**, along with additional output from CMake.
50+
- In the background, Visual Studio starts to index the source files to enable IntelliSense, browsing information, refactoring, and so on. As you work, Visual Studio monitors changes in the editor and also on disk to keep its index in sync with the sources.
51+
You can open folders containing any number of CMake projects. Visual Studio detects and configures all the "root" CMakeLists.txt files in your workspace. CMake operations (configure, build, debug) as well as C++ IntelliSense and browsing are available to all CMake projects in your workspace.
52+
53+
![CMake project with multiple roots](media/cmake-multiple-roots.png)
54+
55+
## Building CMake projects
56+
To build a CMake project, you have these choices:
57+
1. Select the target in the Debug dropdown and press **F5** or click the "Run" button. The project will automatically build first, just like with Visual Studio solutions.
58+
2. Right click on the CMakeLists.txt and select Build from the context menu. If you have multiple targets in your folder structure, you can choose to build all or only one specific target, or
59+
60+
3. From the main menu, select **Build | Build Solution** (**F7** or **Ctrl+Shift+B**) (make sure that a CMake target is already selected in the **Startup Item** dropdown in the **General** toolbar).
61+
62+
![CMake build menu command](media/cmake-build-menu.png)
63+
64+
When a Visual Studio generator is selected for the active configuration, MSBuild.exe is invoked with `-m -v:minimal` arguments. To customize the build, inside the CMakeSettings.json file, you can specify additional command line arguments to be passed to the build system via the `buildCommandArgs` property:
65+
66+
```json
67+
"buildCommandArgs": "-m:8 -v:minimal -p:PreferredToolArchitecture=x64"
68+
```
69+
As you would expect, build results are shown in the **Output Window** and **Error List**.
70+
71+
![CMake build errors](media/cmake-build-errors.png)
72+
73+
In a folder with multiple build targets, by default all are built when you press **F5** or **Ctrl+Shift+B**. You can choose the Build item on the CMakeLists.txt context menu to specify which CMake target to build.
74+
75+
## Debugging the project
76+
To debug a CMake project, choose the desired configuration and press **F5**, or press the Run button in the toolbar. If the Run button says "Select Startup Item", click on the down arrow and choose the target that you want to run. (In a CMake project, the "Current document" option isn't valid.)
77+
78+
![CMake run button](media/cmake-run-button.png)
79+
80+
Pressing **F5** will first build the project if changes have been made since the previous build.
81+
82+
## Configuring CMake debugging sessions
83+
All executable CMake targets are shown in the Startup Item dropdown in the General toolbar. To start a debugging session, just select one and launch the debugger.
84+
85+
![CMake startup item dropdown](media/cmake-startup-item-dropdown.png)
86+
87+
To customize the debugger settings for any executable CMake target in your project, right-click on the specific CMakeLists.txt file and select **Debug and Launch Settings**, When you select a CMake target in the sub-menu, a file called launch.vs.json is created. This file is pre-populated with information about the CMake target you have selected and allows you to specify additional parameters such as program arguments or debugger type.
88+
89+
A simple launch.vs.json example:
90+
```json
91+
{
92+
"version": "0.2.1",
93+
"defaults": {},
94+
"configurations": [
95+
{
96+
"type": "default",
97+
"project": "CMakeLists.txt",
98+
"projectTarget": "tests\\hellotest",
99+
"name": "tests\\hellotest with args",
100+
"args": ["argument after argument"]
101+
}
102+
]
103+
}
104+
```
105+
As soon as you save the launch.vs.json file, an entry is created in the Startup Item dropdown with the new name. By editing the launch.vs.json file, you can create as many debug configurations as you like for any number of CMake targets.
106+
107+
108+
## Editing CMakeLists.txt files
109+
To edit a CMakeLists.txt file, right click on the file in **Solution Explorer** and choose **Open**. If you make changes to the file, a yellow status bar appears and informs you that IntelliSense will update, and gives you an opportunity to cancel the update operation. For information about CMakeLists.txt, see the [CMake documentation](https://cmake.org/documentation/).
110+
111+
![CMakeLists.txt file editing](media/cmake-cmakelists.png)
112+
113+
As soon as you save the file, the configuration step will automatically run again and display information in the Output Window. Errors and warnings are shown in the Error List or Output Window. Double-click on an error in the Error List to navigate to the offending line in CMakeLists.txt.
114+
115+
![CMakeLists.txt file errors](media/cmake-cmakelists-error.png)
116+
117+
## <a name="cmake_settings"></a> CMake settings and custom configurations
118+
By default, Visual Studio provides four default CMake configurations ("x86-Debug", "x86-Release", "x64-Debug" and "x64-Release"). These configurations define how CMake.exe is invoked to create the CMake cache for a given project. To modify these configurations, or create a new custom configuration, choose **CMake | Change CMake Setting**, and then choose the CMakeLists.txt file that the settings will apply to. The Change CMake Settings is also available on the file's context menu in **Solution Explorer**. This command creates a CMakeSettings.json file in the project folder. This file is used to re-create the CMake cache file, for example after a "Clean" operation.
119+
120+
![CMake main menu command for change settings](media/cmake-change-settings.png)
121+
122+
JSON IntelliSense helps you edit the CMakeSettings.json file:
123+
124+
![CMake JSON IntelliSense](media/cmake-json-intellisense.png)
125+
126+
The following example shows a sample configuration, which you can use as the starting point to create your own in CMakeSettings.json:
127+
```json
128+
{
129+
"configurations": [
130+
{
131+
"name": "my-config",
132+
"generator": "Visual Studio 15 2017",
133+
"buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
134+
"cmakeCommandArgs": "",
135+
"variables": [
136+
{
137+
"name": "VARIABLE",
138+
"value": "value"
139+
}
140+
]
141+
}
142+
]
143+
}
144+
```
145+
1. name: the name that will appear in the C++ configuration dropdown. This property value can also be used as a macro `${name}` to specify other property values. For an example, see the "buildRoot" definition in cmakesettings.json.
146+
2. generator: maps to the -G switch and specifies the generator to be used. This property can also be used as a macro `${generator}` to help specify other property values. Visual Studio currently supports the following CMake generators:
147+
- "Visual Studio 14 2015"
148+
- "Visual Studio 14 2015 ARM"
149+
- "Visual Studio 14 2015 Win64"
150+
- "Visual Studio 15 2017"
151+
- "Visual Studio 15 2017 ARM"
152+
- "Visual Studio 15 2017 Win64"
153+
- "Ninja"
154+
3. buildRoot: maps to -DCMAKE_BINARY_DIR switch and specifies where the CMake cache will be created. If the folder does not exist, it will be created.
155+
4. variables: contains a name+value pair of CMake variables that will get passed as -Dname=value to CMake. If your CMake project build instructions specify adding any variables directly to the CMake cache file, it is recommended that you add them here instead.
156+
5. cmakeCommandArgs: specifies any additional switches you want to pass to CMake.exe.
157+
6. configurationType: defines the build configuration type for the selected generator. Currently supported values are "Debug", "MinSizeRel", "Release", and "RelWithDebInfo".
158+
159+
CMakeSettings.json also supports consuming environment variables in any of the properties mentioned above. The syntax to use is `${env.FOO}` to expand the environment variable %FOO%.
160+
You also have access to built-in macros inside this file:
161+
- `${workspaceRoot}` – provides the full path of the workspace folder
162+
- `${workspaceHash}` – hash of workspace location; useful for creating a unique identifier for the current workspace (for example, to use in folder paths)
163+
- `${projectFile}` – the full path of the root CMakeLists.txt file
164+
- `${projectDir}` – the full path of the folder of the root CMakeLists.txt file
165+
- `${thisFile}` – the full path of the CMakeSettings.json file
166+
- `${name}` – the name of the configuration
167+
- `${generator}` – the name of the CMake generator used in this configuration
168+
169+
As significant changes are made to the CMakeSettings.json or to CMakeLists.txt files, Visual Studio will automatically re-run the CMake configure step. If the configure step finishes without errors, the information that is collected will be available in C++ IntelliSense and language services and also in build and debug operations.
170+
171+
When multiple CMake projects use the same CMake configuration name (for example, x86-Debug), all of them are configured and built (in their own build root folder) when that configuration is selected. You can debug the targets from all of the CMake projects that participate in that CMake configuration.
172+
173+
![CMake Build Only menu item](media/cmake-build-only.png)
174+
175+
To limit builds and debug sessions to a subset of the projects in the workspace, create a new configuration with a unique name in the CMakeSettings.json file and apply it to those projects only. When that configuration is selected, IntelliSense, building, and debugging will be enabled only for those specified projects.
176+
177+
## Troubleshooting CMake cache errors
178+
If you need more information about the state of the CMake cache to diagnose a problem, open the CMake main menu or the CMakeLists.txt context menu in **Solution Explorer** to run one of these commands:
179+
- **View Cache** opens the CMakeCache.txt file from the build root folder in the editor. (Any edits you make here to CMakeCache.txt are wiped out if you clean the cache. To make changes that persist after the cache is cleaned, see [CMake settings and custom configurations](#cmake_settings) earlier in this article.)
180+
- **Open Cache Folder** opens an Explorer window to the build root folder.
181+
- **Clean Cache** deletes the build root folder so that the next CMake configure step starts from a clean cache.
182+
- **Generate Cache** forces the generate step to run even if Visual Studio considers the environment up-to-date.
183+

docs/ide/creating-and-managing-visual-cpp-projects.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,10 @@ translation.priority.mt:
4040
- "pt-br"
4141
- "tr-tr"
4242
---
43-
# Creating and Managing Visual C++ Projects
44-
Visual Studio 2017 offers three ways to organize your C++ source code and related files to produce an executable (such as an .exe, .dll or .lib):
45-
- As an MSBuild-based project
46-
- As a CMake project. For more information, see [CMake support in Visual Studio](https://blogs.msdn.microsoft.com/vcblog/2017/04/17/cmake-support-in-visual-studio-whats-new-in-2017-15-2-update/).
47-
- As a loose collection of files in a folder with an optional JSON configuration file. For more information, see [Bring your C++ code to Visual Studio](https://blogs.msdn.microsoft.com/vcblog/2017/04/14/bring-your-cpp-code-to-visual-studio/).
43+
# Creating and Managing MSBuild-based Visual C++ Projects
44+
MSBuild is the native build system for Visual C++ and is generally the best build system to use for UWP apps as well as Desktop applications that use MFC or ATL libraries. MSBuild is tightly integrated with the Visual Studio IDE and project system, but you can also use it from the command line. Starting in Visual Studio 2017, Visual C++ supports [CMake and other non-MSBuild systems through the Open Folder feature](non-msbuild-projects.md).
4845

49-
> [!NOTE]
50-
> This section describes MSBuild projects.
46+
An MSBuild-based project has a project file in XML format (.vcxproj) that specifies all the files and resources needed to compile the program, as well as other configuration settings, for example the target platform (x86, x64 or ARM) and whether you are building a release version or debug version of the program. A project (or many projects) are contained in a *Solution*; for example, a solution might contain several Win32 DLL projects, and a single Win32 console application that uses those DLLs. When you build the project, the MSBuild engine consumes the project file and produces the executable file and/or any other custom output you have specified.
5147

5248
You can create Visual C++ projects by choosing **File &#124; New &#124; Project**, ensuring that Visual C++ is selected in the left pane, and then choosing from the list of project templates in the middle pane. When you click on a template, in many cases a wizard will appear that enables you to set various project properties before the project is created. You can view and modify those properties later by using the project's property pages (**Project &#124; Properties**).
5349

@@ -63,10 +59,10 @@ You can create Visual C++ projects by choosing **File &#124; New &#124; Project*
6359

6460
## In This Section
6561
[Visual C++ Project Types](../ide/visual-cpp-project-types.md)
66-
Describes the project types that are available in Visual C++ and how to create a C-language project.
62+
Describes the MSBuild-based project types that are available in Visual C++.
6763

6864
[File Types Created for Visual C++ Projects](../ide/file-types-created-for-visual-cpp-projects.md)
69-
Describes the kinds of files that are used with various project types.
65+
Describes the kinds of files that are used with various MSBuild project types.
7066

7167
[Creating Desktop Projects By Using Application Wizards](../ide/creating-desktop-projects-by-using-application-wizards.md)
7268
How to use the wizards to create projects with Visual C++.

0 commit comments

Comments
 (0)