vscode配置C++以及boost库开发环境

下载vscode

去官网https://code.visualstudio.com/下载最新版本就可以了
https://cdn.llfc.club/1675217312867.jpg
下载后打开vscode,安装C++扩展
https://cdn.llfc.club/1675217797696.jpg
目前就先配置到这里,以后的配置留给下面介绍。

下载mingw

去mingw官网下载压缩包
https://sourceforge.net/projects/mingw-w64/files/
下载速度慢的话可以去我的网盘下载,整理了mingw和boost的安装包

链接: https://pan.baidu.com/s/1IJSyQqdf-lbuFG12FhDt2A?pwd=2dhi
提取码: 2dhi

选择x86_64-win32-seh,当然选择x86_64-posix-seh也可以。一个是windows线程,一个是posix线程。
https://cdn.llfc.club/1675218594895.jpg
下载后解压缩到指定文件夹,我的目录为 D:\cppsoft\mingw64
https://cdn.llfc.club/1675218863338.jpg
配置环境变量,选择用户的环境变量path, 添加D:\cppsoft\mingw64\bin,具体路径看你自己的配置
https://cdn.llfc.club/1675219007194.jpg
测试是否配置成功,打开cmd,在里面输入g++或者gcc,如果输出no input files 则说明配置成功
https://cdn.llfc.club/1675219207819.jpg

配置vscode C++开发环境

主要分为配置编译器信息,配置构建任务task.json以及配置调试设置lauch.json文件三个步骤。

配置编译器信息

在vscode界面按住Ctrl + Shift + P 唤出配置界面
https://cdn.llfc.club/1675219377959.jpg
在输入栏输入C/C+, 选择“Edit Configurations(UI)”进入配置
https://cdn.llfc.club/1675219489602.jpg
配置编译器路径和IntelliSense模式,我的路径是D:/mingw64/bin/g++.exe
https://cdn.llfc.club/1675219605854.jpg
另外要注意配置Include path,其他人的博客很少提及这点,这里着重说一下
https://cdn.llfc.club/1675821919856.jpg
上述情况配置好include路径后,包含第三方库才不会报错,否则会有错误提示。
ctrl+s保存后会生成一个.vscode文件夹,.vscode文件夹里有一个c_cpp_properties.json文件,记录了我们配置的编译器信息。
https://cdn.llfc.club/1675219958396.jpg
json文件信息如下

  1. {
  2. "configurations": [
  3. {
  4. "name": "Win32",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "defines": [
  9. "_DEBUG",
  10. "UNICODE",
  11. "_UNICODE"
  12. ],
  13. "windowsSdkVersion": "10.0.22000.0",
  14. "compilerPath": "D:/mingw64/bin/g++.exe",
  15. "cStandard": "c17",
  16. "cppStandard": "c++17",
  17. "intelliSenseMode": "gcc-x64"
  18. }
  19. ],
  20. "version": 4
  21. }

配置构建任务

配置任务之前我们先写一个.cpp和.h文件用来测试,组织形式如下
https://cdn.llfc.club/1675221432283.jpg
然后选中左侧目录树的cpp文件,保证我们所有的配置都是基于cpp构建的。
接下来,创建一个tasks.json文件来告诉VS Code如何构建(编译)程序。该任务将调用g++编译器基于源代码创建可执行文件。 按快捷键Ctrl+Shift+P调出命令面板,输入tasks,选择“Tasks:Configure Default Build Task”:
https://cdn.llfc.club/1675220317789.jpg
可以看到vscode列举出我的电脑已经安装的所有C++编译器,这里选择
再选择“C/C++: g++.exe build active file”,这是我们刚刚安装的mingw编译器
https://cdn.llfc.club/1675220601273.jpg
点击后vscode为我们自动生成了task.json文件
https://cdn.llfc.club/1675221243876.jpg
详细的task.json配置信息如下

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "cppbuild",
  6. "label": "C/C++: g++.exe build active file",
  7. "command": "D:/mingw64/bin/g++.exe",
  8. "args": [
  9. "-fdiagnostics-color=always",
  10. "-g",
  11. "${file}",
  12. "-o",
  13. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  14. ],
  15. "options": {
  16. "cwd": "D:/mingw64/bin"
  17. },
  18. "problemMatcher": [
  19. "$gcc"
  20. ],
  21. "group": {
  22. "kind": "build",
  23. "isDefault": true
  24. },
  25. "detail": "compiler: D:/mingw64/bin/g++.exe"
  26. }
  27. ]
  28. }

配置调试设置

同样选中左侧目录树的cpp文件,保证我们所有的配置都是基于cpp构建的。
这里主要是为了在.vscode文件夹中产生一个launch.json文件,用来配置调试的相关信息。点击菜单栏的Debug—>Start Debugging:
https://cdn.llfc.club/vsluanch.png
接下来选择C++(GDB/LLDB), 当然你选择windows的也可以。
https://cdn.llfc.club/1675221733059.jpg
紧接着会产生一个launch.json的文件。
这里有的人没有生成launch.json文件,而是直接运行成功了,可以不配置launch.json文件了。
如果生成launch.json文件,则如下

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": []
  7. }

如果想生成launch.json还有一个办法,就是点击Run选择Add Configuration, 就可以生成launch.json文件了。
https://cdn.llfc.club/vsluanch2.png
关于launch.json文件的配置可以参考官方文档,大家可以直接用我的配置如下

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "g++.exe build and debug active file",
  6. "type": "cppdbg",
  7. "request": "launch",
  8. "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
  9. "args": [],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceFolder}",
  12. "environment": [],
  13. "externalConsole": true, //修改此项,让其弹出终端
  14. "MIMode": "gdb",
  15. "miDebuggerPath": "D:\\cppsoft\\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": "task g++" //修改此项
  24. }
  25. ]
  26. }

miDebuggerPath 改为你自己的mingw路径
preLaunchTask 为任务名字,你可以自己定义一个任务名字,任务名字要和task.json中的label相匹配。我的名字为”task g++”,那么我会将task.json中的label也改为”task g++”。我的task.json文件如下

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "cppbuild",
  6. "label": "task g++",
  7. "command": "D:/mingw64/bin/g++.exe",
  8. "args": [
  9. "-fdiagnostics-color=always",
  10. "-g",
  11. "${cwd}//src//*.cpp",
  12. "-o",
  13. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  14. ],
  15. "options": {
  16. "cwd": "D:/mingw64/bin"
  17. },
  18. "problemMatcher": [
  19. "$gcc"
  20. ],
  21. "group": {
  22. "kind": "build",
  23. "isDefault": true
  24. },
  25. "detail": "compiler: D:/mingw64/bin/g++.exe"
  26. }
  27. ]
  28. }
  • 我们修改了label为”task g++”
  • 我们修改了args中的-g选项为”${cwd}//src//*.cpp”
    选择test.cpp再次运行,程序通过。

    安装boost库

    先去官网下载boost库最新版本
    https://www.boost.org/users/download/
    选择windows版本下载,zip和7z格式的都可以
    https://cdn.llfc.club/1675230057233.jpg
    解压后文件夹下有个一个bootstrap.bat文件,双击运行会生成b2.exe
    https://cdn.llfc.club/1675231068369.jpg
    然后在boost文件夹下启动cmd,执行 “.\b2.exe toolset=gcc”
    https://cdn.llfc.club/1675231439687.jpg
    编译时间和机器性能有关,执行编译过后,会在stage文件夹下生成lib文件夹,里面就是我们要用到的lib库。
    https://cdn.llfc.club/1675232553339.jpg
    接下来需要将boost文件夹下的staget文件夹下的lib文件夹下的库放到mingw的文件夹下x86_64-w64-mingw32的文件夹下的lib文件夹内。
    我的目录就是从”D:\TSBrowserDownloads\boost_1_81_0\stage\lib”复制到”D:\mingw64\x86_64-w64-mingw32\lib”。
    https://cdn.llfc.club/1675232714373.jpg
    同时也要把boost的头文件从boost文件夹下拷贝到x86_64-w64-mingw32的include文件夹下
    也就是从”D:\TSBrowserDownloads\boost_1_81_0\boost”拷贝到D:\mingw64\x86_64-w64-mingw32\include
    https://cdn.llfc.club/1675233325569.jpg

    测试boost库

    需要对已经配置完成的boost库进行测试,我们写一个程序, 替换原来的test.cpp的main函数
  1. #include <iostream>
  2. #include <string>
  3. #include "boost/lexical_cast.hpp"
  4. int main()
  5. {
  6. using namespace std;
  7. cout << "Enter your weight: ";
  8. float weight;
  9. cin >> weight;
  10. string gain = "A 10% increase raises ";
  11. string wt = boost::lexical_cast<string> (weight);
  12. gain = gain + wt + " to "; // string operator()
  13. weight = 1.1 * weight;
  14. gain = gain + boost::lexical_cast<string>(weight) + ".";
  15. cout << gain << endl;
  16. system("pause");
  17. return 0;
  18. }

我们点击调试的三角符号或者菜单里的run命令,可以看到运行成功了,并且弹出了窗口
https://cdn.llfc.club/1675233803676.jpg

总结

我将配置好的项目demo提交到gitee了,感兴趣的可以看一看
项目地址
https://gitee.com/secondtonone1/testvscpp

热门评论

热门文章

  1. C++ 类的继承封装和多态

    喜欢(588) 浏览(4926)
  2. windows环境搭建和vscode配置

    喜欢(587) 浏览(2790)
  3. Linux环境搭建和编码

    喜欢(594) 浏览(12093)
  4. 解密定时器的实现细节

    喜欢(566) 浏览(3456)
  5. slice介绍和使用

    喜欢(521) 浏览(2476)

最新评论

  1. C++ 并发三剑客future, promise和async Yunfei:大佬您好,如果这个线程池中加入的异步任务的形参如果有右值引用,这个commit中的返回类型推导和bind绑定就会出现问题,请问实际工程中,是不是不会用到这种任务,如果用到了,应该怎么解决?
  2. Qt MVC结构之QItemDelegate介绍 胡歌-此生不换:gpt, google
  3. 聊天项目(9) redis服务搭建 pro_lin:redis线程池的析构函数,除了pop出队列,还要free掉redis连接把
  4. 答疑汇总(thread,async源码分析) Yagus:如果引用计数为0,则会执行 future 的析构进而等待任务执行完成,那么看到的输出将是 这边应该不对吧,std::future析构只在这三种情况都满足的时候才回block: 1.共享状态是std::async 创造的(类型是_Task_async_state) 2.共享状态没有ready 3.这个future是共享状态的最后一个引用 这边共享状态类型是“_Package_state”,引用计数即使为0也不应该block啊

个人公众号

个人微信