Win 7 下 C++ 编译环境 MSYS2 MinGW 64-bit + Visual Studio Code

叁歲伎倆 2022-03-07 06:14 434阅读 0赞

1. MSYS2

国内使用中科大的源,详见:https://lug.ustc.edu.cn/wiki/…

pacman -S mingw-w64-x86_64-toolchain

1.1 踩过的坑

初始直接安装

pacman -S gcc

而gcc -version 后发现是 7.4 而非 8.2 的。
后使用指令后版本变成 8.2 了。

pacman -S mingw-w64-x86_64-gcc

但后来 gdb 时还需要手动安装一下。若空间足够,可以手动安装。

pacman -S mingw-w64-x86_64-gdb

2. 使用 make 跑一下

test.cpp

  1. #include<iostream>
  2. int main(void) {
  3. long int tag = __cplusplus;
  4. if(tag == 201703L) std::cout << "C++17\n";
  5. else if(tag == 201402L) std::cout << "C++14\n";
  6. else if(tag == 201103L) std::cout << "C++11\n";
  7. else if(tag == 199711L) std::cout << "C++98\n";
  8. else std::cout << "pre-standard C++\n";
  9. return 0;
  10. }

makefile

  1. CPPFLAGS=-Wall -std=c++17
  2. all:test
  3. clean:
  4. rm test.exe

结果:

  1. $ ./test.exe
  2. C++17

3. 使用 Visual Studio Code

3.1 安装扩展:C/C++

3.2 新建文件夹

File -> Open Folder, Create a new Folder ‘test’
create a new file test.cpp, the same as 2.

3.3 配置文件

create a dir .vscode with 3 files.
c_cpp_properties.json

  1. {
  2. "configurations": [
  3. {
  4. "name": "Win32",
  5. "includePath": [
  6. "c:\\msys64\\mingw64\\include",
  7. "c:\\msys64\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.2.1\\include",
  8. "${workspaceFolder}/**"
  9. ],
  10. "defines": [
  11. "_DEBUG",
  12. "UNICODE",
  13. "_UNICODE"
  14. ],
  15. "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
  16. "cStandard": "c11",
  17. "cppStandard": "c++17",
  18. "intelliSenseMode": "clang-x64"
  19. }
  20. ],
  21. "version": 4
  22. }

用于编译的 tasks
tasks.json

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "g++",
  6. "type": "shell",
  7. "command": "g++",
  8. "args": [
  9. "${file}", "-g", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe", "-std=c++17"
  10. ],
  11. "group": {
  12. "kind": "build",
  13. "isDefault": true
  14. }
  15. }
  16. ]
  17. }

用于运行加载的launch.json

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "(gdb) Launch",
  6. "type": "cppdbg",
  7. "request": "launch",
  8. "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
  9. "args": [],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceFolder}",
  12. "environment": [],
  13. "externalConsole": false,
  14. "MIMode": "gdb",
  15. "miDebuggerPath": "c:\\msys64\\mingw64\\bin\\gdb.exe",
  16. "setupCommands": [
  17. {
  18. "description": "Enable pretty-printing for gdb",
  19. "text": "-enable-pretty-printing",
  20. "ignoreFailures": true
  21. }
  22. ],
  23. "preLaunchTask": "g++"
  24. }
  25. ]
  26. }

4. 编译

快捷键:ctrl+shift+B

鼠标的用法:
先选中 test.cpp
再Terminal — Run Task — g++ — Continue…
Terminal 中会有编译成功的消息。

5. 运行 F5

会出现输出结果
C++17

发表评论

表情:
评论列表 (有 0 条评论,434人围观)

还没有评论,来说两句吧...

相关阅读