00020-GCC on Windows-windows10
前言
在这篇教程中,你将要配置 Visual Studio Code 使用 mingw-w64 的 GCC C++ compiler (g++) 创建运行在 Windows 的程序。
操作系统:Windows 10 专业版
参考文档
Prerequisites
-
安装 C/C++ extension for VS Code,你能通过在扩展视图(Ctrl+Shift+X)搜索
c++
安装这个 C/C++ 扩展。
-
通过 MSYS2 获取最新版本的 Mingw-w64,MSYS2 提供 GCC、Mingw-w64 和其他有用的C++工具和库的最新原生版本。
-
按照 MSYS2 website 上的 Installation 的指导安装 Mingw-w64。
-
下载 installer:msys2-x86_64-20220603.exe
-
运行 installer,MSYS2 要求 64 位 Windows 7 或更新的操作系统。
-
选择安装目录,完成安装。
-
更新包数据库和基本包。除非您的安装文件是最新的,否则需要两个步骤。运行开始菜单的 “MSYS2 MSYS”。
1
pacman -Syu
- 再次运行开始菜单的 “MSYS2 MSYS”。用
pacman -Syu
更新剩余的基础包。
1
pacman -Syu
- 安装
Mingw-w64 toolset
。
1
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
- 关闭 “MSYS2 MSYS” 窗口,运行开始菜单的 “MSYS2 MinGW x64”。现在就可以使用 “make” 和 “gcc” 为 Windows 构建软件了。
-
-
将
Mingw-w64
的bin
目录的路径添加到 WindowsPATH
环境变量。-
In the Windows search bar, type ‘settings’ to open your Windows Settings.
-
Search for Edit environment variables for your account.
-
Choose the
Path
variable in your User variables and then select Edit. -
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:
C:\msys64\mingw64\bin
.
由于为了以后的方便,我还添加了其他的路径。
D:\lyf_computer_language\msys64\usr\bin
D:\lyf_computer_language\msys64\mingw64\bin
D:\lyf_computer_language\msys64\clang64\bin- Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.
-
Check your MinGW installation
为了检查 Mingw-w64 tools
是否安装正确,打开一个新的控制台。
1 | g++ --version |
Create Hello World
运行开始菜单的 “MSYS2 MinGW x64”,运行下面命令。
1 | mkdir projects |
The “code .” command opens VS Code in the current working folder, which becomes your “workspace”. Accept the Workspace Trust dialog by selecting Yes, I trust the authors since this is a folder you created.
Add a source code file
- 新建
helloworld.cpp
。
1 |
|
Run helloworld.cpp
-
Open
helloworld.cpp
so that it is the active file. -
Press the play button in the top right corner of the editor.
- Choose
C/C++: g++.exe build and debug active file
from the list of detected compilers on your system.
- After the build succeeds, your program’s output will appear in the integrated Terminal.
常见问题
- 如果出现下图错误,可以采用下面的方法或者参考下面的文章。
-
方法1
: 使用-static 编译[g++ -static file.cpp]。 -
方法2
: 将libstdc++-6.dll
文件放到C:\Windows\System32
或C:\Windows\SysWOW64
文件夹下。libstdc++-6.dll
文件可以从Mingw-w64
的bin
目录的路径(D:\lyf_computer_language\msys64\mingw64\bin
)得到。
结语
第二十篇博文写完,开心!!!!
今天,也是充满希望的一天。