Skip to content

Commit 54229e9

Browse files
author
mikeblome
committed
updates for open folder
1 parent c789136 commit 54229e9

6 files changed

Lines changed: 135 additions & 28 deletions
18.7 KB
Loading
10.4 KB
Loading
27 KB
Loading
12.4 KB
Loading
4.43 KB
Loading

docs/build/open-folder-projects-cpp.md

Lines changed: 135 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,139 @@ ms.assetid: abd1985e-3717-4338-9e80-869db5435175
66
---
77
# Open Folder projects for C++
88

9-
In Visual Studio 2017 and later, the "Open Folder" feature enables you to open a folder of source files and immediately start coding with support for IntelliSense, browsing, refactoring, debugging, and so on. No .sln or .vcxproj files are loaded; if needed, you can specify custom tasks as well as build and launch parameters through simple .json files. For general information about Open Folder, see [Develop code in Visual Studio without projects or solutions](/visualstudio/ide/develop-code-in-visual-studio-without-projects-or-solutions).
9+
In Visual Studio 2017 and later, the "Open Folder" feature enables you to open a folder of source files and immediately start coding with support for IntelliSense, browsing, refactoring, debugging, and so on. As you edit, create, move, or delete files, Visual Studio tracks the changes automatically and continuously updates its IntelliSense index. No .sln or .vcxproj files are loaded; if needed, you can specify custom tasks as well as build and launch parameters through simple .json files. For general information about Open Folder, see [Develop code in Visual Studio without projects or solutions](/visualstudio/ide/develop-code-in-visual-studio-without-projects-or-solutions).
1010

11-
CMake is integrated in the Visual Studio IDE as a component of the C++ desktop workload. For more information, see [CMake projects in Visual Studio](cmake-projects-in-visual-studio.md). For any other build system, you can use the Open Folder feature. Open Folder effectively decouples the code editor, debugger and analyzers from the build system and the compiler toolset. You can use the C++ code editor with its rich IntelliSense features, the code analyzers, and the Visual Studio debugger with virtually any build system, including CMake, Ninja, QMake (for Qt projects), gyp, SCons, Gradle, Buck, make and more. It even works with a single file or a loose collection of files with no build system.
11+
## CMake and Qt
1212

13-
To use Open Folder, from the main menu select **File | Open | Folder** or press **Ctrl + Shift + Alt + O**.
14-
Solution Explorer immediately displays all the files in the folder. You can click on any file to begin editing it. In the background, Visual Studio starts indexing the files to enable IntelliSense, navigation, and refactoring features. As you edit, create, move, or delete files, Visual Studio tracks the changes automatically and continuously updates its IntelliSense index.
13+
CMake is integrated in the Visual Studio IDE as a component of the C++ desktop workload. The workflow for CMake is not identical to the workflow described in this article. If you are using CMake, see [CMake projects in Visual Studio](cmake-projects-in-visual-studio.md). You can also use CMake to build Qt projects, or you can use the [Qt Visual Studio Extension](https://download.qt.io/development_releases/vsaddin/) for either Visual Studio 2015 or Visual Studio 2017.
1514

16-
## QMake projects that target the Qt framework
15+
## Other build systems
1716

18-
You can use CMake to build Qt projects, or you can use the [Qt Visual Studio Extension](https://download.qt.io/development_releases/vsaddin/) for either Visual Studio 2015 or Visual Studio 2017.
19-
20-
## gyp, Cons, SCons, Buck, etc
21-
22-
You can use any build system in Visual Studio and still enjoy the advantages of the C++ IDE and debugger. When you open the root folder of your project, the C++ code editor uses heuristics to index the source files for IntelliSense and browsing. You can provide hints about the structure of your code by editing the CppProperties.json file. In a similar way, you can configure and invoke your build program by editing the launch.vs.json file.
23-
24-
## Configuring Open Folder projects
25-
26-
You can customize an Open Folder project through three JSON files:
17+
To use the Visual Studio IDE with a build system or compiler toolset that is not directly supported (for example, make, gyp, SCons, Gradle, Buck, and so on), from the main menu select **File | Open | Folder** or press **Ctrl + Shift + Alt + O**. Navigate to the folder that contains your source code files. To build the project, define custom tasks, or configure IntelliSense for system headers, you add three JSON files:
2718

2819
| | |
2920
|-|-|
3021
|CppProperties.json|Specify custom configuration information for browsing. Create this file, if needed, in your root project folder. (Not used in CMake projects.)|
31-
|tasks.vs.json|Specify custom build commands and compiler switches. Accessed via the **Solution Explorer** context menu item **Configure Tasks**.|
32-
|launch.vs.json|Specify command line arguments for the debugger. Accessed via the **Solution Explorer** context menu item **Debug and Launch Settings**.|
22+
|tasks.vs.json|Specify custom build commands. Accessed via the **Solution Explorer** context menu item **Configure Tasks**.|
23+
|launch.vs.json|Specify command line arguments for the debugger. Accessed via the **Solution Explorer** context menu item **Debug and Launch Settings**.|
3324

3425
### Configure IntelliSense and browsing hints with CppProperties.json
3526

36-
IntelliSense and browsing behavior partly depends on the active build configuration, which defines #include paths, compiler switches, and other parameters. By default, Visual Studio provides Debug and Release configurations. CMake projects use the CMakeSettings.json file and CMakeLists.txt files for this purpose. For other kinds of Open Folder projects, you may need to create a custom configuration in order for IntelliSense and browsing features to fully comprehend your code. To define a new configuration, create a file called CppProperties.json in the root folder. Here is an example:
27+
For IntelliSense and browsing behavior such as **Go to Definition** to work correctly, Visual Studio needs to know which compiler you are using, where the system headers are, and where any additional include files are located if they are not directly in the folder you have opened (the workspace folder). To specify a configuration, you can choose **Manage Congfigurations** from the dropdown in the main toolbar:
28+
29+
![Manage configurations dropdown](media/manage-configurations-dropdown.png)
30+
31+
Currently, Visual Studio offers four default configurations, all for the Microsoft C++ compiler:
32+
33+
![Default configurations](media/default-configurations.png)
34+
35+
If, for example, you choose **x64-Debug**, Visual Studio creates a file called `CppProperties.json` in your root project folder and populates it like so:
3736

3837
```json
3938
{
4039
"configurations": [
4140
{
42-
"name": "Windows x64",
43-
"includePath": [ "include" ],
44-
"defines": [ "_DEBUG" ],
45-
"compilerSwitches": "/std:c++17",
46-
"intelliSenseMode": "windows-msvc-x64",
47-
"forcedInclude": [ "pch.h" ],
48-
"undefines": []
41+
"inheritEnvironments": [
42+
"msvc_x64"
43+
],
44+
"name": "x64-Debug",
45+
"includePath": [
46+
"${env.INCLUDE}",
47+
"${workspaceRoot}\\**"
48+
],
49+
"defines": [
50+
"WIN32",
51+
"_DEBUG",
52+
"UNICODE",
53+
"_UNICODE"
54+
],
55+
"intelliSenseMode": "windows-msvc-x64"
4956
}
5057
]
5158
}
5259
```
53-
For more information, see [CppProperties schema reference](cppproperties-schema-reference.md).
60+
61+
This configuration "inherits" the environment variables of the Visual Studio [x64 Developer Command Prompt](building-on-the-command-line.md). One of those variables is `INCLUDE` and you can refer to it here by using the `${env.INCLUDE}` macro. The `includePath` property tells Visual Studio where to look for all the sources that it needs for IntelliSense. In this case, it says "look in the all the directories specified by the INCLUDE environment variable, and also all the directories in the current working folder tree. The `name` property is the name that will appear in the dropdown, and can be anything you like. The `defines` property provides hints to IntelliSense when it encounters conditional compilation blocks. The `intelliSenseMode` property provides some additional hints based on the compiler type. Several options are available for MSVC, GCC, and Clang.
62+
63+
If you are using a different compiler, you have to create a custom configuration and environment in *cppproperties.json*. The following example shows a complete *cppproperties.json* file with a single custom configuration for using GCC in an MSYS2 installation:
64+
65+
```json
66+
{
67+
"configurations": [
68+
{
69+
"inheritEnvironments": [
70+
"mingw_64"
71+
],
72+
"name": "Mingw64",
73+
"includePath": [
74+
"${env.INCLUDE}",
75+
"${workspaceRoot}\\**"
76+
],
77+
"intelliSenseMode": "linux-gcc-x64",
78+
"environments": [
79+
{
80+
"MINGW64_ROOT": "C:\\msys64\\mingw64",
81+
"BIN_ROOT": "${env.MINGW64_ROOT}\\bin",
82+
"FLAVOR": "x86_64-w64-mingw32",
83+
"TOOLSET_VERSION": "8.3.0",
84+
"PATH": "${env.MINGW64_ROOT}\\bin;${env.MINGW64_ROOT}\\..\\usr\\local\\bin;${env.MINGW64_ROOT}\\..\\usr\\bin;${env.MINGW64_ROOT}\\..\\bin;${env.PATH}",
85+
"INCLUDE": "${env.MINGW64_ROOT}\\include\\c++\\${env.TOOLSET_VERSION};${env.MINGW64_ROOT}\\include\\c++\\${env.TOOLSET_VERSION}\\tr1;${env.MINGW64_ROOT}\\include\\c++\\${env.TOOLSET_VERSION}\\${env.FLAVOR}",
86+
"environment": "mingw_64"
87+
}
88+
]
89+
}
90+
}
91+
```
92+
93+
Note the `environments` block. It defines properties that behave like environment variables and are available not only in the *cppproperties.json* file, but also in the other configuration files *task.vs.json* and *launch.vs.json*. The `Mingw64` configuration inherits the `mingw_w64` environment, and uses its INCLUDE property to specify the value for `includePath`. You can add other paths to this array property as needed.
94+
95+
The `intelliSenseMode` property is set to a value appropriate for GCC. For more information on all these properties, see [CppProperties schema reference](cppproperties-schema-reference.md).
96+
97+
When everything is working correctly, you will see IntelliSense from the GCC headers when you hover over a type:
98+
99+
![GCC IntelliSense](media/gcc-intellisense.png)
100+
101+
If you are not seeing the IntelliSense that you expect, you can troubleshoot by going to **Tools** > **Options** > **Text Editor** > **C/C++** > **Advanced** and setting **Enable Logging** to **true**. To start with, try setting **Logging Level** to 5, and **Logging Filters** to 8.
102+
103+
![Diagnostic logging](media/diagnostic-logging.png)
104+
105+
Output is piped to the **Output Window** and is visible when you choose **Show Output From: Visual C++ Log*. The output contains, among other things, the list of actual include paths that IntelliSense is trying to use.
106+
107+
> [!WARNING]
108+
> There is currently a known issue in which the `INCLUDE` value specified in `environments` is not correctly passed to the `includePath` property. You can work around the issue by adding the complete literal include paths to the `includePath` array.
54109

55110
### Define build tasks with tasks.vs.json
56111

57112
You can automate build scripts or any other external operations on the files you have in your current workspace by running them as tasks directly in the IDE. You can configure a new task by right-clicking on a file or folder and selecting **Configure Tasks**.
58113

59114
![Open Folder Configure Tasks](media/open-folder-config-tasks.png)
60115

61-
This creates (or opens) the **tasks.vs.json** file in the .vs folder which Visual Studio creates in your root project folder. You can define any arbitrary task in this file and then invoke it from the **Solution Explorer** context menu. The following example shows a tasks.vs.json file that defines a single task. `taskName` defines the name that appears in the context menu. `appliesTo` defines which files the command can be performed on. The `command` property refers to the COMSPEC environment variable, which identifies the path for the console (cmd.exe on Windows). You can also reference environment variables that are declared in CppProperties.json or CMakeSettings.json. The `args` property specifies the command line to be invoked. The `${file}` macro retrieves the selected file in **Solution Explorer**. The following example will display the filename of the currently selected .cpp file.
116+
This creates (or opens) the *tasks.vs.json* file in the .vs folder which Visual Studio creates in your root project folder. You can define any arbitrary task in this file and then invoke it from the **Solution Explorer** context menu. To continue with the GCC example, the following example shows a complete *tasks.vs.json* file with as single task that invokes *g++.exe* to build a project. Assume the project contains a single file called *hello.cpp*.
117+
118+
```json
119+
{
120+
"version": "0.2.1",
121+
"tasks": [
122+
{
123+
"taskLabel": "build hello",
124+
"appliesTo": "/",
125+
"type": "default",
126+
"command": "g++",
127+
"args": [
128+
"-g",
129+
"-o",
130+
"hello",
131+
"hello.cpp"
132+
]
133+
}
134+
]
135+
}
136+
137+
```
138+
139+
You can now run this task by right-clicking on the project node in **Solution Explorer** and choosing **build hello**. When the task completes you should see a new file, *hello.exe* in **Solution Explorer**.
140+
141+
You can define many kinds of tasks. The following example shows a tasks.vs.json file that defines a single task. `taskName` defines the name that appears in the context menu. `appliesTo` defines which files the command can be performed on. The `command` property refers to the COMSPEC environment variable, which identifies the path for the console (*cmd.exe* on Windows). You can also reference environment variables that are declared in CppProperties.json or CMakeSettings.json. The `args` property specifies the command line to be invoked. The `${file}` macro retrieves the selected file in **Solution Explorer**. The following example will display the filename of the currently selected .cpp file.
62142

63143
```json
64144
{
@@ -81,9 +161,36 @@ For more information, see [Tasks.vs.json schema reference](tasks-vs-json-schema-
81161

82162
### Configure debugging parameters with launch.vs.json
83163

84-
To customize your program’s command line arguments, right-click on the executable in **Solution Explorer** and select **Debug and Launch Settings**. This will open an existing **launch.vs.json** file, or if none exists, it will create a new file prepopulated with the information about the program you have selected.
164+
To customize your program’s command line arguments, right-click on the executable in **Solution Explorer** and select **Debug and Launch Settings**. This will open an existing **launch.vs.json** file, or if none exists, it will create a new file that is pre-populated with the information about the program you have selected. First you are given a choice of what kind of debug session you want to configure. For debugging a MinGw-w64 project, we choose **C/C++ Launch for MinGGW/Cygwin (gdb)**. This creates a launch configuration for using *gdb.exe* with some educated guesses about default values. Since we did not define a variable called `MINGW_PREFIX` we can just substitute the literal path:
165+
166+
```json
167+
{
168+
"version": "0.2.1",
169+
"defaults": {},
170+
"configurations": [
171+
{
172+
"type": "cppdbg",
173+
"name": "hello.exe",
174+
"project": "hello.exe",
175+
"cwd": "${workspaceRoot}",
176+
"program": "${debugInfo.target}",
177+
"MIMode": "gdb",
178+
"miDebuggerPath": "c:\\msys64\\usr\\bin\\gdb.exe",
179+
"externalConsole": true
180+
}
181+
]
182+
}
183+
184+
```
185+
186+
To start debugging, choose the executable in the debug dropdown, then click the green arrow:
187+
188+
![Launch debugger](media/launch-debugger-gdb.png)
189+
190+
You should see the **Initializing Debugger** dialog and then an external console window that is running your program.
191+
85192

86-
To specify additional arguments, just add them in the `args` JSON array as shown in the following example:
193+
You can define launch settings for any executable on your computer. The following example launches *7za* and specifies additional arguments, by adding them to the `args` JSON array:
87194

88195
```json
89196
{

0 commit comments

Comments
 (0)