当前位置: 首页 > news >正文

[VisualStudioCode]_[VSCODE]_[C/C++开发环境配置-问题解决和补充]

场景

  1. 在使用VSCode配置C++的开发环境时《VisualStudioCode_C/C++开发环境配置[1]》,编译时会发现找不到标准库的std::mutexstd::thread的声明,而这两个的头文件已经引入,什么情况?

  2. 无论如何MinGW都是Windows上验证开发C++特性比较新的工具,就是配置麻烦点。其实免费的话配置麻烦是可以接受的。想避免麻烦的话可以直接使用vsstudio的社区版。

说明

  1. MingGW的预编译版本官方推荐的是在niXman mingw-builds-binaries下载。这个版本我们发现有posixwin32版本,如果下载使用的是win32版本,那么如果要使用标准库thread,必须安装了gthread第三方库。而这个库一般是和gtk一起安装的,很少会用到,就会导致用不了<thread>。解决办法是使用posix线程的版本。如:
https://github.com/niXman/mingw-builds-binaries/releases/download/13.2.0-rt_v11-rev0/x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0.7z
  1. 如果配置了MinGWbin目录在环境变量path,就不会出现链接问题。如果像我那样不想配置环境变量,需要模拟普通用户一样的环境,集成的终端就会出现以下错误。
Build finished with error(s).
E:\software\MinGW-w64\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\bin\g++.exe -std=c++11 -IE:\software\MinGW-w64\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\lib\gcc\x86_64-w64-mingw32\13.2.0\include\c++ -g -o E:\Project\Sample\08.cpp-test\test-synchronized\Debug-MinGW\test-synchronized.exe E:\Project\Sample\08.cpp-test\test-synchronized\test-synchronized\test-synchronized.cpp E:\Project\Sample\08.cpp-test\test-synchronized\test-synchronized\bas_lock.cpp

如果想看具体的信息,那么在外部终端运行,会弹出以下错误。在mingw安装目录下搜索这个cc1plus.exe命令,之后用Dependencies发现它缺少libwinpthread-1.dll文件,而这个文件是在mingw的安装目录bin下。因此编译链接需要有这个bin在搜索路径里,可以在tasks.json里设置当前编译目录是..\bin目录。 方法是在options->cwd里设置。

在这里插入图片描述

{"version": "2.0.0","tasks": [{..."options": {"cwd": "E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\bin"},...}]
}
  1. Debug运行的时候如果报错无法调试,也是因为没有配置gdb的搜索路径,gdb也是需要依赖bin目录下的其他文件。所以需要在launch.json配置environment属性值。
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". During startup program exited with code 0xc000007b.
"environment": [{"name": "path","value": ";%path%;E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\bin"}],
  1. tasks.json里配置的编译参数,如果要编译多个文件,可以使用gcc的参数。 如果要从任意文件都可以按F5编译运行,那么就需要使用${workspaceFolder}变量[3]
"args": ["-std=c++11","-IE:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\lib\\gcc\\x86_64-w64-mingw32\\13.2.0\\include\\c++","-g","-o","${workspaceFolder}\\Debug-MinGW\\test-synchronized.exe","${workspaceFolder}\\test-synchronized\\test-synchronized.cpp","${workspaceFolder}\\test-synchronized\\bas_lock.cpp"],

例子

  1. 这里是优化后的配置, 关键的3个文件,都需要放在.vscode目录下。 当然也可以自己配置执行Makefile的编译。

tasks.json 编译链接配置

{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "test-synchronized","command": "E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\bin\\g++.exe","args": ["-std=c++11","-IE:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\lib\\gcc\\x86_64-w64-mingw32\\13.2.0\\include\\c++","-g","-o","${workspaceFolder}\\Debug-MinGW\\test-synchronized.exe","${workspaceFolder}\\test-synchronized\\test-synchronized.cpp","${workspaceFolder}\\test-synchronized\\bas_lock.cpp"],"options": {"cwd": "E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\bin"},"problemMatcher": ["$gcc"],"group": "build","detail": "compiler: g++.exe"}]
}

c_cpp_properties.json 智能感知声明跳转配置

{"configurations": [{"name": "MinGW-G++","includePath": ["${workspaceFolder}/**","E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\include","E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\x86_64-w64-mingw32\\include","E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\lib\\gcc\\x86_64-w64-mingw32\\13.2.0\\include\\c++","E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\lib\\gcc\\x86_64-w64-mingw32\\13.2.0\\include","E:/software/MinGW-w64/x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0/lib/gcc/x86_64-w64-mingw32/13.2.0/include/c++/x86_64-w64-mingw32"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"windowsSdkVersion": "10.0.22621.0","compilerPath": "E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\bin\\g++.exe","cStandard": "c17","cppStandard": "c++11","intelliSenseMode": "windows-gcc-x64","compilerArgs": []}],"version": 4
}

launch.json 运行配置

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{  "name": "MinGW gdb launch","type": "cppdbg","request": "launch","program": "${workspaceFolder}\\Debug-MinGW\\test-synchronized.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","externalConsole": false,"environment": [{"name": "path","value": ";%path%;E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\bin"}],"preLaunchTask": "test-synchronized","MIMode": "gdb","logging": {"moduleLoad": true,"trace": true},// "windows": {"options": {"env": {"path":"C:\\Windows;C:\\Windows\\System32;E:\\software\\cygwin64\\bin"}}},"miDebuggerPath": "E:\\software\\MinGW-w64\\x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0\\bin\\gdb.exe","setupCommands": [{"description": "asdfas","text": "-enable-pretty-printing","ignoreFailures": true}] } ]
}

参考

  1. VisualStudioCode_C/C++开发环境配置

  2. niXman mingw-builds-binaries

  3. Visual Studio Code Variables Reference

相关文章:

  • 怎么让视频进行加速处理并保存
  • Qt QWidget窗口基类
  • Python AttributeError: ‘NoneType‘ object has no attribute ‘shape‘如何解决
  • 分布式引擎Elasticsearch本地部署并结合内网穿透远程访问
  • C#,字符串匹配算法(模式搜索)Z算法的源代码与数据可视化
  • 适配 IOS 安全区域
  • Java线程学习笔记
  • 定时音频数据采集并发送websocket实时播放
  • 深度学习入门到发表顶级会议论文需要多久
  • google drive api
  • MATLAB中padarray函数用法
  • Android 实现集合去重的方法
  • 解锁前端新潜能:如何使用 Rust 锈化前端工具链
  • Windows启动MongoDB服务报错(错误 1053:服务没有及时响应启动或控制请求)
  • 【Unity美术】如何用3DsMax做一个水桶模型
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【译】理解JavaScript:new 关键字
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • AWS实战 - 利用IAM对S3做访问控制
  • Babel配置的不完全指南
  • docker-consul
  • GitUp, 你不可错过的秀外慧中的git工具
  • IDEA 插件开发入门教程
  • JavaScript DOM 10 - 滚动
  • javascript 哈希表
  • Java程序员幽默爆笑锦集
  • Redis中的lru算法实现
  • select2 取值 遍历 设置默认值
  • 机器学习学习笔记一
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 如何学习JavaEE,项目又该如何做?
  • 腾讯大梁:DevOps最后一棒,有效构建海量运营的持续反馈能力
  • elasticsearch-head插件安装
  • ​卜东波研究员:高观点下的少儿计算思维
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (16)Reactor的测试——响应式Spring的道法术器
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (LeetCode) T14. Longest Common Prefix
  • (poj1.3.2)1791(构造法模拟)
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (顺序)容器的好伴侣 --- 容器适配器
  • (算法)Travel Information Center
  • (一)Neo4j下载安装以及初次使用
  • (转载)从 Java 代码到 Java 堆
  • .net 获取url的方法
  • .NET中GET与SET的用法
  • .sys文件乱码_python vscode输出乱码
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @PreAuthorize注解
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • []error LNK2001: unresolved external symbol _m
  • [20160807][系统设计的三次迭代]