VisualStudioCode配置mingw-w64的GCCc++编译器(g++)和
基于html的网页设计毕业论文GD。。。
机器翻译
Using GCC with MinGW
使⽤ GCC 和 MinGW
In this tutorial, you configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger from to create programs that run on Windows.
在本教程中,您将 Visual Studio Code 配置为使⽤来⾃ mingw-w64的 GCC c + + 编译器(g + +)和 GDB 调试器来创建在 Windows 上运⾏的程序。
After configuring VS Code, you will compile and debug a simple Hello World program in VS Code. This tutorial does not teach you about GCC, GDB, Mingw-w64, or the C++ language. For those subjects, there are many good resources available on the Web.
在配置了 VS 代码之后,您将⽤ VS 代码编译和调试⼀个简单的 Hello World 程序。本教程不教授 GCC、 GDB、 mingw-w64或 c + + 语⾔。对于这些主题,⽹上有许多很好的资源。
If you have any problems, feel free to file an issue for this tutorial in the .
如果您有任何问题,请随意将本教程的问题归档到 VS 代码⽂档存储库中。
Prerequisites 先决条件
To successfully complete this tutorial, you must do the following steps:
要成功完成本教程,您必须执⾏以下步骤:
1. Install .
安装 Visual Studio 代码。
2. Install the . You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X).
为 VS 代码安装 c/c + + 扩展。您可以通过在 Extensions 视图中搜索“ c + +”(Ctrl + Shift + x)来安装 c/c
+ + 扩展。
3. Get the latest version of Mingw-w64 via , which provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools
and libraries. to download the MSYS2 installer. Then follow the instructions on the to install Mingw-w64.
通过 msys2获得最新版本的 Mingw-w64,它提供了最新的 GCC、 mingw-w64和其他有⽤的 c + + ⼯具和库。点击这⾥下载 msys2安装程序。然后按照 msys2⽹站上的说明安装 Mingw-w64。
4. Add the path to your Mingw-w64 bin folder to the Windows PATH environment variable by using the following steps:
使⽤以下步骤将 Mingw-w64 bin ⽂件夹的路径添加到 Windows PATH 环境变量⽂件夹中:
1. In the Windows search bar, type 'settings' to open your Windows Settings. 在 Windows 搜索栏中,键⼊“设置”以打开 Windows
设置
2. Search for 搜寻Edit environment variables for your account 为您的帐户编辑环境变量.
3. Choose the 选择Path variable and then select 变量,然后选择Edit 编辑.
4. Select 选择New 新 and add the Mingw-w64 destination folder path to the system path. The exact path depends on which
version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the path: 并将 mingw-w64⽬标⽂件夹路径添加到系统路径。确切的路径取决于您安装了哪个版本的 Mingw-w64,以及安装它的位置。如果你使⽤上⾯的设置来安装 Mingw-w64,那么把它添加到路径:C:\msys64\mingw64\bin.
5. Select 选择OK 好的 to save the updated PATH. You will need to reopen any console windows for the new PATH location to be
available. 保存更新后的 PATH。您需要重新打开任何控制台窗⼝,以便新的 PATH 位置可⽤
Check your MinGW installation 检查您的 MinGW 安装
To check that your Mingw-w64 tools are correctly installed and available, open a new Command Prompt and type:
要检查您的 mingw-w64⼯具是否正确安装和可⽤,请打开⼀个新的命令提⽰符并键⼊:
g++ --version
gdb --version
If you don't see the expected output or g++ or gdb is not a recognized command, make sure your PATH entry matches the Mingw-w64 binary location where the compilers are located. If the compilers do not exist at that PATH entry, make sure you followed the instructions on the to install Mingw-w64.
如果您没有看到预期的输出,或者 g + + 或 gdb 不是⼀个可识别的命令,请确保您的 PATH 条⽬与编译器所在的 mingw-w64⼆进制位置匹配。如果 PATH 条⽬中不存在编译器,请确保按照 msys2⽹站上的说明安装 Mingw-w64。
Create Hello World 创建 Hello World
From a Windows command prompt, create an empty folder called projects where you can place all y
our VS Code projects. Then create a sub-folder called helloworld, navigate into it, and open VS Code in that folder by entering the following commands:
在⼀个命令提⽰符⽂件夹中,创建⼀个名为项⽬的空⽂件夹,在这个⽂件夹中你可以放置所有的 VS 代码项⽬。然后创建⼀个名为helloworld 的⼦⽂件夹,导航到该⽂件夹,并通过输⼊以下命令打开该⽂件夹中的 VS Code:
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .
The "code ." command opens VS Code in the current working folder, which becomes your "workspace". As you go through the tutorial, you will see three files created in a .vscode folder in the workspace:
代码。”命令在当前⼯作⽂件夹中打开 VS 代码,该⽂件夹成为您的“⼯作区”。在阅读本教程时,您将看到在。⼯作区中的 vscode ⽂件夹:
tasks.json (build instructions) (构建说明)
launch.json (debugger settings) (调试器设置)
c_cpp_properties.json (compiler path and IntelliSense settings) (编译器路径和 IntelliSense 设置)
Add a source code file 添加源代码⽂件
In the File Explorer title bar, select the New File button and name the file helloworld.cpp.
在 File Explorer 标题栏中,选择 New File 按钮并将⽂件命名为 helloworld.cpp。
Add hello world source code 添加 hello world 源代码
Now paste in this source code:
linux删除已经创建的用户现在粘贴下⾯的源代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
Now press Ctrl+S to save the file. Notice how the file you just added appears in the File Explorer view (Ctrl+Shift+E) in the side bar of VS Code:
现在按 Ctrl + s 来保存⽂件。注意你刚刚添加的⽂件是如何出现在 VS Code 边栏的 File Explorer 视图(Ctrl + Shift + e)中的:
You can also enable to automatically save your file changes, by checking Auto Save in the main File menu.
您也可以启⽤⾃动保存,以⾃动保存您的⽂件更改,通过检查⾃动保存在主⽂件菜单。
The Activity Bar on the far left lets you open different views such as Search, Source Control, and Run. You'll look at the Run view later in this tutorial. You can find out more about the other views in the VS Code .
最左边的活动栏允许您打开不同的视图,如搜索、源代码管理和运⾏。您将在本教程的后⾯看到 Run 视图。您可以在 VS 代码⽤户界⾯⽂档中到关于其他视图的更多信息。
Note: When you save or open a C++ file, you may see a notification from the C/C++ extension about the availability of an
Insiders version, which lets you test new features and fixes. You can ignore this notification by selecting the X (Clear
Notification).
注意: 当您保存或打开⼀个 c + + ⽂件时,您可能会看到来⾃ c/c + + 扩展的关于内部版本可⽤性的通知,它允许您测试新的特性和修复。可以通过选择 x (清除通知)来忽略此通知。
Explore IntelliSense 探索智能感知
In your new helloworld.cpp file, hover over vector or string to see type information. After the declaration of the msg variable, start typing msg. as you would when calling a member function. You should immediately see a completion list that shows all the member functions, and a window that shows the type information for the msg object:
在新的 helloworld.cpp ⽂件中,将⿏标悬停在 vector 或 string 上以查看类型信息。在声明 msg 变量之后,开始键⼊ msg。就像调⽤成员函数⼀样。您应该⽴即看到⼀个完成列表,显⽰所有成员函数,以及⼀个窗⼝,显⽰ msg 对象的类型信息:
You can press the Tab key to insert the selected member; then, when you add the opening parenthesis, you will see information about any arguments that the function requires.
您可以按 Tab 键插⼊选定的成员; 然后,当您添加开始括号时,您将看到有关函数所需的任何参数的信息。
Build helloworld.cpp
Next, you'll create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the g++ compiler to create an executable file based on the source code.
接下来,您将创建⼀个 tasks.json ⽂件来告诉 VS 代码如何构建(编译)程序。此任务将调⽤ g + + 编译器,以基于源代码创建可执⾏⽂件。
oracle查看dmp文件内容
From the main menu, choose Terminal > Configure Default Build Task. In the dropdown, which will display a tasks dropdown listing various predefined build tasks for C++ compilers. Choose g++.exe build active file, which will build the file that is currently displayed (active) in the editor.
从主菜单中,选择终端 > 配置默认⽣成任务。在下拉列表中,它将显⽰⼀个任务下拉列表,列出 c + + 编译器的各种预定义的构建任务。选择 g + + 。Exe ⽣成活动⽂件,该⽂件将⽣成当前在编辑器中显⽰(活动)的⽂件。
This will create a tasks.json file in a .vscode folder and open it in the editor.
这将在.vscode ⽂件夹中创建 tasks.json ⽂件,并在编辑器中打开它。
Your new tasks.json file should look similar to the JSON below:
您的新 tasks.JSON ⽂件看起来应该类似于下⾯的 JSON:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
replace怎么读英语"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:/msys64/mingw64/bin/g++.exe"
}
],
"version": "2.0.0"
}
The command setting specifies the program to run; in this case that is g++. The args array specifies the command-line arguments that will be
passed to g++. These arguments must be specified in the order expected by the compiler. This task tells g++ to take the active file (${file}), compile it, and create an executable file in the current directory (${fileDirname}) with the same name as the active file but with the .exe extension (${fileBasenameNoExtension}.exe), resulting for our example.
命令设置指定要运⾏的程序; 在本例中是 g + + 。Args 数组指定将传递给 g + + 的命令⾏参数。这些参数必须按编译器期望的顺序指定。这个任务告诉 g + + 获取活动⽂件({ file }) ,对其进⾏编译,并在⼯作⽬录⽂件中创建⼀个与活动⽂件同名的可执⾏⽂件({ fileDirname })。Exe 扩展(${ fileBasenameNoExtension })。Exe) ,导致 为我们的⽰例。
Note: You can learn more about tasks.json variables in the .
注意: 您可以在变量引⽤中了解更多关于 tasks.json 变量的信息。
The label value is what you will see in the tasks list; you can name this whatever you like.
标签值就是您将在任务列表中看到的内容; 您可以随⼼所欲地命名它。
The "isDefault": true value in the group object specifies that this task will be run when you press Ctrl+Shift+B. This property is for convenience only; if you set it to false, you can still run it from the Terminal menu with Tasks: Run Build Task.
Group 对象中的“ isDefault”: true value 指定当您按下 Ctrl + Shift + b 时将运⾏此任务。此属性仅为⽅便起见; 如果将其设置为 false,仍然可以在终端菜单中使⽤ Tasks: Run Build Task 运⾏它。
Running the build 运⾏构建
1. Go back to helloworld.cpp. Your task builds the active file and you want to build helloworld.cpp.
返回到 helloworld.cpp,您的任务构建活动⽂件,并希望构建 helloworld.cpp。
2. To run the build task defined in tasks.json, press Ctrl+Shift+B or from the Terminal main menu choose Run Build Task.
要运⾏ tasks. json 中定义的构建任务,请按 Ctrl + Shift + b 或从终端主菜单中选择 Run Build Task。
3. When the task starts, you should see the Integrated Terminal panel appear below the source code editor. After the task completes, the
next studioterminal shows output from the compiler that indicates whether the build succeeded or failed. For a successful g++ build, the output looks something like this:
当任务启动时,您应该会看到集成终端⾯板出现在源代码编辑器下⽅。任务完成后,终端显⽰编译器的输出,指⽰⽣成是成功还是失败。对于⼀个成功的 g + + 构建,输出如下:
4. Create a new terminal using the + button and you'll have a new terminal with the helloworld folder as the working directory. Run dir and
you should now see the
创建⼀个新的终端使⽤ + 按钮,你将有⼀个新的终端与 helloworld ⽂件夹作为⼯作⽬录。运⾏ dir,现在应该会看到可执⾏的
<。
5. You can run helloworld in the terminal by (or .\ if you use a PowerShell terminal).
可以通过键⼊ (如果使⽤ PowerShell 终端,则可以键⼊.)在终端中运⾏ helloworld。
Note: You might need to press Enter a couple of times initially to see the PowerShell prompt in the terminal. This issue should be fixed in a future release of Windows.
注意: ⼀开始您可能需要按回车⼏次,才能在终端中看到 PowerShell 提⽰符。这个问题应该在未来的 Windows 版本中得到解
决。
Modifying tasks.json
You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}\\*.cpp" instead of ${file}. This will build all .cpp files in your current folder. You can also modify the output filename by replacing "${fileDirname}\\${fileBasenameNoExtension}.exe" with a hard-coded filename (for example "${workspaceFolder}\\").
您可以使⽤“ { worksomatoolder } *”这样的参数修改 tasks.json 以构建多个 c + + ⽂件。⽽不是{ file }。这将建⽴所有。当前⽂件夹中的 cpp ⽂件。还可以通过替换“ { fileDirname } { fileBasenameNoExtension }来修改输出⽂件名。Exe ⽂件名(例如“ ${ worksomatoolder } ”)。
Debug helloworld.cpp
Next, you'll create a launch.json file to configure VS Code to launch the GDB debugger when you pre
ss F5 to debug the program.
接下来,您将创建⼀个 launch.json ⽂件来配置 VS Code,以便在按 f5调试程序时启动 GDB 调试器。
1. From the main menu, choose 从主菜单中选择Run 快跑 > 添加配置.. and then choose 然后选择C++ (GDB/LLDB)
C + + (GDB/ldb).
2. You'll then see a dropdown for various predefined debugging configurations. Choose 然后您将看到各种预定义调试配置的下拉列表
g++.exe build and debug active file.
VS Code creates a launch.json file, opens it in the editor, and builds and runs 'helloworld'.
VS Code 创建⼀个 launch.json ⽂件,在编辑器中打开它,构建并运⾏“ helloworld”。
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\",
伦勃朗作品特"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
The program setting specifies the program you want to debug. Here it is set to the active file folder ${fileDirname} and active filename with the .exe extension ${fileBasenameNoExtension}.exe, which if helloworld.cpp is the active file will
程序设置指定要调试的程序。在这⾥,它被设置为活动⽂件夹{ fileDirname }和带有。扩展名{ fileBasenameNoExtension }。Exe,如果helloworld.cpp 是活动⽂件,则该⽂件将为 。
By default, the C++ extension won't add any breakpoints to your source code and the stopAtEntry value is set to false.
默认情况下,c + + 扩展不会向源代码添加任何断点,⽽ stopAtEntry 值设置为 false。
Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging.
将 stopAtEntry 值更改为 true,以便在开始调试时使调试器在 main ⽅法上停⽌。
Note: The preLaunchTask setting is used to specify task to be executed before launch. Make sure it is consistent with the tasks.json file label setting.
注意: preLaunchTask 设置⽤于指定启动前要执⾏的任务。确保它与 tasks.json ⽂件标签设置⼀致。
Start a debugging session 启动调试会话
1. Go back to 回到helloworld.cpp so that it is the active file. 所以它就是活动⽂件
2. Press 新闻稿F5 or from the main menu choose 或者从主菜单中选择Run > Start Debugging 运⾏ > 开始调试. Before you start
stepping through the source code, let's take a moment to notice several changes in the user interface: .在你开始介绍源代码之前,让我们花点时间注意⼀下⽤户界⾯的⼏个变化:
The Integrated Terminal appears at the bottom of the source code editor. In the Debug Output tab, you see output that indicates the debugger is up and running.
集成终端显⽰在源代码编辑器的底部。在“调试输出”选项卡中,可以看到指⽰调试器已启动并正在运⾏的输出。
The editor highlights the first statement in the main method. This is a breakpoint that the C++ extension automatically sets for you:编辑器突出显⽰ main ⽅法中的第⼀条语句。这个断点是 c + + 扩展⾃动为你设置的:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论