简体   繁体   中英

How to setup “include path” in vscode to compile c++

I am on Ubuntu 20.04 using Vscode.

I'm trying to compile files got here

Dialog Box

I type this in a terminal

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

and it compiles fine.

But doesn't work with Vscode.

This is my c_cpp_properties.json file

{
"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

}

and tasks.json file

{
// 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"
    },
]

}

Compiling with Vscode i got these errors终端错误

Any ideas what to?

I suspect that the problem is with pkg-config in compilerArgs. Try to separate each argument:

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

After many tries got solution from

Variables Reference

c_cpp_properties.json reference

and Using C++ on Linux in VS Code

First I put all files in same folder (in my case.cc and.h).

To compile in command line I use

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

You must reproduce this command in 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

}

and 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
        }
    }
]

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM