【问题标题】:How to setup "include path" in vscode to compile c++如何在 vscode 中设置“包含路径”来编译 c++
【发布时间】:2020-11-30 08:06:06
【问题描述】:

我在 Ubuntu 20.04 上使用 Vscode。

我正在尝试编译这里的文件

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 编译时出现这些错误

有什么想法吗?

【问题讨论】:

    标签: c++ ubuntu visual-studio-code include-path


    【解决方案1】:

    经过多次尝试 得到解决方案

    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", "gtkmm-3.0", "--cflags", "--libs`"
                
            ],
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
    

    }

    【讨论】:

      【解决方案2】:

      我怀疑问题出在 compilerArgs 中的 pkg-config 上。 尝试将每个参数分开:

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

      【讨论】:

        猜你喜欢
        • 2019-04-21
        • 1970-01-01
        • 2012-07-15
        • 1970-01-01
        • 2022-01-15
        相关资源
        最近更新 更多