Mihail HRS
Mihail HRS

Reputation: 115

How to add gtk lib in Visual studio code?

I try to add gtk library to my file in Visual Studio code on my Arch Linux ,but it underlines "#include line and writes:

#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/home/mikhailkhr/My projects/C projects/Test/Test.c).
cannot open source file "glibconfig.h" (dependency of "gtk/gtk.h")

It does compile this file with:

 gcc `pkg-config gtk+-3.0 --cflags` ProgName.c `pkg-config gtk+-3.0 --libs`

But why does it underline this?
And how to fix this?

Thanks.

Sorse code:

#define _PROGRAM_NAME "whoami"
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
#include <gtk/gtk.h>

const char *getUserName()
{
  uid_t uid = geteuid();
  struct passwd *pw = getpwuid(uid);
  if (pw)
  {
    return pw->pw_name;
  }

  return "";
}

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello %s\n", getUserName());
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button_box;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_container_add (GTK_CONTAINER (window), button_box);

  button = gtk_button_new_with_label (getUserName());
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (button_box), button);

  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);



    return status;

}

Upvotes: 5

Views: 20080

Answers (4)

Gunty
Gunty

Reputation: 2479

add this line to your include path:

C:/msys64/mingw64/**
  1. open vs code and do CTRL + SHIFT + P to open command palette

  2. type C/C++: Edit configurations (UI) and open

  3. add C:/msys64/mingw64/** to the include path

This makes the IDE search the entire mingw filesystem for your relevant dependancies.

This solution is for mingw and windows

Upvotes: 0

Near Future
Near Future

Reputation: 185

If you open gtk/gtk.h (F12 go to definition):

enter image description here

You'll see a bunch of other red lines:

enter image description here

Which, generally saying, is your error message.

In most cases, if you open 1 of those header files , you'll see that some header path is missing (non related to gtk/ core , pango in this case):

enter image description here

Simply search the name (pango as example here) under your include directory (mine one is C:\msys64\mingw64\include under the Windows):

enter image description here

Now, when you're sure it's installed (install it in case of opposite answer) you need to create .vscode folder under your working-project directory and create a file c_cpp_properties.json

enter image description here

In this file you will tell to VSCode, where the required header files are stored:

enter image description here

{
    "configurations": [
        {
               "name": "Win32",
            "includePath": [ "C:\\msys64\\mingw64\\include\\pango-1.0"         , "C:\\msys64\\mingw64\\include\\gtk-3.0" ,"C:\\msys64\\mingw64\\include", "C:\\msys64\\mingw64\\include\\glib-2.0"],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14"
        }
    ],
    "version": 4
}

(Full documentation about properties of the file you can find here)

Which solves one of the previous errors:

enter image description here

You should repeat and add those pathes into the includePath property untill it will be fully solved.

Upvotes: 0

user12581835
user12581835

Reputation:

I added this to the include path inside .vscode/c_cpp_properties.json:

        "includePath": [
            "${workspaceFolder}/**",
            "/usr/include/gtk-3.0",
            "/usr/include/glib-2.0",
            "/usr/lib/x86_64-linux-gnu/glib-2.0/include",
            "/usr/include/pango-1.0",
            "/usr/include/harfbuzz",
            "/usr/include/cairo",
            "/usr/include/gdk-pixbuf-2.0",
            "/usr/include/atk-1.0"
        ],

If you need gtkmm:

"/usr/include/gtkmm-3.0",
"/usr/include/glibmm-2.4",
"/usr/include/sigc++-2.0",
"/usr/lib/x86_64-linux-gnu/sigc++-2.0/include",
"/usr/lib/x86_64-linux-gnu/glibmm-2.4/include",
"/usr/include/giomm-2.4/giomm",
"/usr/include/giomm-2.4",
"/usr/include/glibmm-2.4",
"/usr/include/glibmm-2.4/glibmm",
"/usr/lib/x86_64-linux-gnu/giomm-2.4/include",
"/usr/include/gdkmm-3.0",
"/usr/lib/x86_64-linux-gnu/gdkmm-3.0/include",
"/usr/include/pangomm-1.4",
"/usr/lib/x86_64-linux-gnu/pangomm-1.4/include",
"/usr/include/cairomm-1.0",
"/usr/include/freetype2",
"/usr/lib/x86_64-linux-gnu/gtkmm-3.0/include",
"/usr/lib/x86_64-linux-gnu/atkmm-1.6/include",
"/usr/include/atkmm-1.6"

Upvotes: 5

Carsten H
Carsten H

Reputation: 119

Add /usr/lib/glib-2.0/include/ to the include paths.

Basically one wants to add the output from pkg-config to the VSCode includes.
To dump the output on a shell, use:
echo $(pkg-config --libs --cflags gtk+-3.0)

If you want to provide pkg-config as a VSCode task argument see this answer. I am happily using this VSCode extension with GCC and Clang. Here is my gist to kick off coding GTK 3 in C with VSCode.

Happy hacking!

Upvotes: 7

Related Questions