如何在vscode中设置“包含路径”以编译C ++

问题描述

我在使用Vscode的Ubuntu 20.04。

我正在尝试编译此处的文件

Dialog Box

我在终端上输入

g++ main.cpp examplewindow.cpp -o WindowBox11 -v -std=c++0x `pkg-config gtkmm-3.0 --cflags --libs`

它可以正常编译。

但不适用于Vscode。

这是我的c_cpp_properties.json文件

{
"configurations": [
    {
        "name": "Linux","includePath": [
            "${workspaceFolder}/**","/usr/include/gtkmm-3.0","/usr/include/**"
        ],"defines": [],"compilerPath": "/usr/bin/g++","cStandard": "gnu18","cppStandard": "gnu++14","intelliSenseMode": "gcc-x64","compilerArgs": [
            "-std=c++0x","`pkg-config gtkmm-3.0 --cflags --libs`","-v"
        ]
    }
],"version": 4

}

和tasks.json文件

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0","tasks": [
    {
        "label": "build & run",//It's name of the task,you can have several tasks 
        "type": "shell",//type can be either 'shell' or 'process',more details will be given below
        "command": "g++","args": [
            "-g",//gnu debugging flag,only necessary if you want to perform debugging on file  
            "${file}",//${file} gives full path of the file
            "-o","${workspaceFolder}\\build\\${fileBasenameNoExtension}",//output file name
            "&&",//to join building and running of the file
            "${workspaceFolder}\\build\\${fileBasenameNoExtension}",],"group": {
            "kind": "build",//defines to which group the task belongs
            "isDefault": true
        },"presentation": {   //Explained in detail below
            "echo": false,"reveal": "always","focus": true,"panel": "shared","clear": false,"showReuseMessage": false
        },"problemMatcher": "$gcc"
    },]

}

使用Vscode编译时出现这些错误

Error in Terminal

有什么想法吗?

解决方法

我怀疑问题出在编译器Args中的pkg-config。 尝试分隔每个参数:

"`pkg-config","gtkmm-3.0","--cflags","--libs`"
,

经过多次尝试 从

那里得到了解决方案

Variables Reference

c_cpp_properties.json reference

Using C++ on Linux in VS Code

首先,我将所有文件放在同一文件夹中(在我的情况下是.cc和.h)。

要在命令行中进行编译,请使用

g++ main.cc  examplewindow.cc -o Dialogue_windo11 -std=c++0x `pkg-config gtkmm-3.0 --cflags --libs`

您必须在vscode中重现此命令

c_cpp_properties.json

{
"env": {
  "myDefaultIncludePath": ["${workspaceFolder}","${workspaceFolder}/include"],"myCompilerPath": "/usr/bin/g++"
},"configurations": [
    {
      "name": "Linux","intelliSenseMode": "gcc-x64","includePath": ["${myDefaultIncludePath}","/usr/include/**"],"compilerPath": "/usr/bin","cStandard": "gnu18","cppStandard": "gnu++14","compilerArgs": [
        "-v"
      ]
    }
],"version" : 4

}

和tasks.json

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0","tasks": [
    {
        "label": "g++ build active file","type": "shell","command": "/usr/bin/g++","args": [
            "${fileDirname}/*.cc",//to compile all .cc files
            "-o","${fileDirname}/MessageBox","-std=c++0x","`pkg-config","--libs`"
            
        ],"problemMatcher": [],"group": {
            "kind": "build","isDefault": true
        }
    }
]

}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...