windows下使用vscode开发C++程序遇到的一些问题

博客讲述了在Windows环境下,使用VSCode开发C++程序时遇到的库链接问题。作者配置了tasks.json以支持cl.exe和g++两种编译器,并详细展示了配置内容。尽管官网推荐使用cl.exe,但作者发现只能成功运行一个例程。通过设置预构建任务,可以在VSCode侧边栏中切换编译器。问题在于使用cl.exe时必须通过命令行以管理员模式启动VSCode。文章还提供了工程目录结构。

windows下使用vscode开发C++程序遇到的一些问题

  1. 连接库问题

    参考:https://blog.csdn.net/chaoren00001/article/details/104108133

    • 问题简述

      vscode下使用g++编译一个tobii眼动仪的例程,tasks.json配置无误,但是只能正常运行第一个例程,第二个例程报错undefined reference to ‘CLASS::METHOD’,无法解决

    • 解决方法

      • 按官网的方法,https://tobiitech.github.io/interaction-library-docs/cpp/html/gettingstarted.html,比较麻烦,需要命令行输入参数

        官网使用了cl.exe而不是g++来编译

      • 参考的方法,在tasks里设置了g++和cl.exe两个编译方式,可以在vscode的侧边栏进行选择

        • 我的tasks

          {
              "tasks": [
                  {	// 对应cl.exe
                      "type": "shell",
                      "label": "cl.exe build active file",
                      "command": "cl.exe",
                      "args": [	// cl的编译选项,自行设置
                          "/Zi",
                          "/EHsc",
                          "/MD",
                          "/I",
                          "include",
                          "/Fe:",
                          "${fileDirname}\\${fileBasenameNoExtension}.exe",
                          "${file}",
                          "/link",
                          "/libpath:lib\\x64",
                          "tobii_interaction_lib.lib",
                          "tobii_stream_engine.lib"
                      ],
                      "group": {
                          "kind": "build",
                          "isDefault": true
                      },
                      "presentation": {
                          "reveal": "always"
                      },
                      "problemMatcher": "$msCompile"
                  },
                  {	// 对应g++.exe
                      "type": "shell",
                      "label": "g++.exe build active file",
                      "command": "C:\\gytx_x86_64-10.2.0-posix-seh\\mingw64\\bin\\g++.exe",
                      "args": [	// g++的编译选项,自行设置
                          "-g",
                          "${file}",
                          "-o",
                          "${fileDirname}\\${fileBasenameNoExtension}.exe",
                          "-std=c++17",
                          "-Wall",
                          "-Weffc++",
                          "-Wextra",
                          "-pedantic",
                          "-I${workspaceFolder}\\include",
                          "-L${workspaceFolder}\\lib\\x64",
                          "-ltobii_interaction_lib",
                          "-ltobii_stream_engine"
                          
                      ],
                      "options": {
                          "cwd": "C:\\gytx_x86_64-10.2.0-posix-seh\\mingw64\\bin"		// 换成你自己的相应路径
                      }
                  }
              ],
              "version": "2.0.0"
          }
          
        • 我的launch.json

          {
              // 使用 IntelliSense 了解相关属性。 
              // 悬停以查看现有属性的描述。
              // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
              "version": "0.2.0",
              "configurations": [
                  {   // 对应cl.exe
                      "name": "cl.exe build and debug active file",
                      "type": "cppvsdbg",
                      "request": "launch",
                      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                      "args": [],
                      "stopAtEntry": false,
                      "cwd": "${workspaceFolder}",
                      "environment": [],
                      "externalConsole": true,
                      "preLaunchTask": "cl.exe build active file",
                      "logging": {	// 用于消除PDB文件找不到打不开问题,来自于https://none53.hatenablog.com/entry/2019/11/28/vsCode_Cannot_find_or_open_the_PDB_file.
                          "moduleLoad": false
                      }
                  },
                  {   // 对应g++.exe
                      "name": "g++.exe build and debug active file",
                      "type": "cppdbg",
                      "request": "launch",
                      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                      "args": [],
                      "stopAtEntry": false,
                      "cwd": "${workspaceFolder}",
                      "environment": [],
                      "externalConsole": true,
                      "MIMode": "gdb",
                      "miDebuggerPath": "C:\\gytx_x86_64-10.2.0-posix-seh\\mingw64\\bin\\gdb.exe",	// 换成你自己的gdb.exe的路径
                      "setupCommands": [
                          {
                              "description": "为 gdb 启用整齐打印",
                              "text": "-enable-pretty-printing",
                              "ignoreFailures": true
                          }
                      ],
                      "preLaunchTask": "g++.exe build active file"
                  }
              ]
          }
          
        • 工程结构

          ├─.vscode
          ├─bin
          │ └─x64
          ├─doc
          │ ├─c
          │ │ └─html
          │ ├─coordinate_systems
          │ │ └─html
          │ ├─cpp
          │ │ └─html
          │ ├─cs
          │ │ └─html
          │ ├─sdk
          │ │ └─html
          │ └─wpf
          │ └─html
          ├─include
          │ ├─DisplayEnumerationHelper
          │ ├─interaction_lib
          │ │ ├─api
          │ │ ├─datatypes
          │ │ ├─math
          │ │ └─misc
          │ └─tobii
          ├─lib
          │ └─x64
          └─samples
          ├─c
          ├─cpp
          ├─cs
          │ └─Properties
          └─wpf
          └─Tobii.InteractionLib.Wpf.SampleApp
          └─Properties

    • 未解决的问题

      • 要使用cl.exe,必须通过命令行打开vscode: 管理员模式打开x64 Native Tools Command Prompt for VS 2019, 输入code D:codes/test(这是我的工程路径), 打开了vscode后正常运行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值