欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-22 18:20:38
...

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后正常运行
相关标签: c++ vscode