c++gtk3visual-studio-codevscode-debugger

How to configure VSCode for GTK3 for intellisense / build / debug and g++


I'm using

How do I get the following to work:

Problem:

VSCode does not find includes - especially #include <gtk/gtk.h> is red in source.


Solution

  • The important thing to note is, that you need to tell VSCode the include paths and compiler flags to work properly.

    Make intellisense / code completion work

    Make building work

    You want to create a new task inside .vscode/tasks.json with the following content:

        {
          "type": "shell",
          "label": "gcc debug build active file - with GTK",
          "command": "/usr/bin/gcc",
          "args": [          
              "-g",
    
                    "-pthread",
                    "-I/usr/include/gtk-3.0",
                    "-I/usr/include/at-spi2-atk/2.0",
                    "-I/usr/include/at-spi-2.0",
                    "-I/usr/include/dbus-1.0",
                    "-I/usr/lib/x86_64-linux-gnu/dbus-1.0/include",
                    "-I/usr/include/gtk-3.0",
                    "-I/usr/include/gio-unix-2.0",
                    "-I/usr/include/cairo",
                    "-I/usr/include/libdrm",
                    "-I/usr/include/pango-1.0",
                    "-I/usr/include/harfbuzz",
                    "-I/usr/include/pango-1.0",
                    "-I/usr/include/fribidi",
                    "-I/usr/include/atk-1.0",
                    "-I/usr/include/cairo",
                    "-I/usr/include/pixman-1",
                    "-I/usr/include/freetype2",
                    "-I/usr/include/libpng16",
                    "-I/usr/include/gdk-pixbuf-2.0",
                    "-I/usr/include/libmount",
                    "-I/usr/include/blkid",
                    "-I/usr/include/uuid",
                    "-I/usr/include/glib-2.0",
                    "-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
    
              "${file}",
    
                    "-lgtk-3",
                    "-lgdk-3",
                    "-lpangocairo-1.0",
                    "-lpango-1.0",
                    "-latk-1.0",
                    "-lcairo-gobject",
                    "-lcairo",
                    "-lgdk_pixbuf-2.0",
                    "-lgio-2.0",
                    "-lgobject-2.0",
                    "-lglib-2.0",
    
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "options": {
              "cwd": "/usr/bin"
          },
          "problemMatcher": [
              "$gcc"
          ],
          "group": {
              "kind": "build",
              "isDefault": true
          }
        } 
    

    Make debugging work

    You want to create a new configuration inside the .vscode/launch.json file. On my setup vscode kept using the wrong configuration, so I deleted the others. Below is the full content of the file with only one configuration.

        {
          // Use IntelliSense to learn about possible attributes.
          // Hover to view descriptions of existing attributes.
          // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
          "version": "0.2.0",
          "configurations": [
    
              {
                  "name": "debug with gdb (no build)",
                  "type": "cppdbg",
                  "request": "launch",
                  "program": "${fileDirname}/${fileBasenameNoExtension}",
                  "args": [],
                  "stopAtEntry": false,
                  "cwd": "${workspaceFolder}",
                  "environment": [],
                  "externalConsole": false,
                  "MIMode": "gdb",
                  "setupCommands": [
                      {
                          "description": "Enable pretty-printing for gdb",
                          "text": "-enable-pretty-printing",
                          "ignoreFailures": true
                      }
                  ],
                  "preLaunchTask": "",
                  "miDebuggerPath": "/usr/bin/gdb"
              }
          ]
        }