I am trying to set up a friendly environment for C/C++ programming in VS Code on Linux Ubuntu. For years I've been using Visual Studio and Code Blocks, however VS Code set up is so unclear that I can hardly understand the documentation topics. At the moment I am trying to compile few "hello world" programs so it shouldn't be hard.
Lets say I am trying to build and run GTK-3 hello world program. The command line instructions to do this is:
gcc hello_world.c `pkg-config --cflags --libs gtk+-3.0`
which works just perfectly when used from bash. So first I have created a configuration c_cpp_properties.json called GTK-devel which is supposed to compile this. The json file content is:
{
"configurations": [
{
"name": "GTK-devel",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
"`pkg-config --cflags --libs gtk+-3.0`"
]
}
],
"version": 4
}
And here comes the first question: What is a configuration in VS Code? In Visual Studio or Code::Blocks similar things were intuitively clear and undesrtandable - just set up everything you need and then choose it somewhere (menu, dialog,...) before compiling. I guess that in VS Code it is something different, the docs don't explain that at all - it just says one can create a configuration, but does not mention how to use it. Moreover there is no place in which one can select it (terminals, tasks, etc), nor where is is even visible anywhere in menu etc.
Second question concerns tasks. So if configuration approach failed I've decided to set up a task which I can clearly access from main menu. The tasks.json file has the following content:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc compilation",
"command": "/usr/bin/gcc `pkg-config --cflags --libs gtk+-3.0`",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "kompilator: /usr/bin/gcc GTK3"
}
]
}
When I choose from menu Terminal -> Run build task -> C/C++: gcc compilation everything seems to be fine in terms of called instructions but there are missing libs errors (which are included and present in the system):
"/usr/bin/gcc `pkg-config --cflags --libs gtk+-3.0`" -g /home/user/Programming/hello_world/hello_world.c -o /home/user/Programming/hello_world/hello_world
/bin/sh: 1: /usr/bin/gcc -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/pango-1.0 -I/usr/include/fribidi -I/usr/include/harfbuzz -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -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/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0: not found
Here the third question arises: Again the docs don't really explain what is a terminal. It says that it defaults to $SHELL, but if so, why the libs are not visible to the compiler?
So the final question is: Is VS Code a real IDE or it is just a marketing around it that it can be used to do many different things in one place, but in reality it is better to use dedicated tool for each language etc. as setup issues will succesfully distract you from what you are really supposed to do?
After spend four hours I finally found the solution. It's simple and half dirty. Here we go:
This code:
`pkg-config --cflags --libs gtk+-3.0`
Produces something like this (that's my environment, e.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/pango-1.0 -I/usr/include/fribidi -I/usr/include/harfbuzz -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -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/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0
VScode doesn't support arguments between `. So all we have to do is copy and paste every single argument in task.json:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-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/pango-1.0",
"-I/usr/include/fribidi",
"-I/usr/include/harfbuzz",
"-I/usr/include/atk-1.0",
"-I/usr/include/cairo",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-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/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-lgtk-3",
"-lgdk-3",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0"
],
Almost forgot. To intellisense file c_cpp_properties.json just simply add two patterns at includePath:
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**",
"/usr/lib/x86_64-linux-gnu/**"
]
That's it
Related
Im trying to set up vs code to work with gtk, I'm programming in c, I followed the instruction on how to install gtk using msys64, then I tried to run an hello world example from their page, but vs code seem to have problems finding the gtk header files.
error: gtk/gtk.h: No such file or directory
I beleive as well that gtk is installed as i run the command kg-config --cflags --libs gtk4
Output: -IC:/msys64/mingw64/bin/../include/gtk-4.0 -IC:/msys64/mingw64/bin/../include/pango-1.0 -IC:/msys64/mingw64/bin/../include -IC:/msys64/mingw64/bin/../include/glib-2.0 -IC:/msys64/mingw64/bin/../lib/glib-2.0/include -IC:/msys64/mingw64/bin/../include/harfbuzz -IC:/msys64/mingw64/bin/../include/freetype2 -IC:/msys64/mingw64/bin/../include/libpng16 -IC:/msys64/mingw64/bin/../include/fribidi -IC:/msys64/mingw64/bin/../include/cairo -IC:/msys64/mingw64/bin/../include/lzo -IC:/msys64/mingw64/bin/../include/pixman-1 -IC:/msys64/mingw64/bin/../include/gdk-pixbuf-2.0 -IC:/msys64/mingw64/bin/../include/graphene-1.0 -IC:/msys64/mingw64/bin/../lib/graphene-1.0/include -mfpmath=sse -msse -msse2 -LC:/msys64/mingw64/bin/../lib -lgtk-4 -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
c_cpp_properties:
"configurations": [
{
"name": "Gtk_dev",
"includePath": [
"${workspaceFolder}/**",
"C:/msys64/mingw64/include/**",
"C:/msys64/mingw64/lib/gtk-4.0",
"C:/msys64/mingw64/lib/glib-2.0/include",
"C:/msys64/mingw64/lib/graphene-1.0/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "gcc-x64",
"compilerArgs": [],
"browse": {
"limitSymbolsToIncludedHeaders": false,
"path": []
}
}
],
"version": 4
}
I've recently tried installing the gtk library and encountered many problems. Firstly i tried using it on VScode and yet it always shows the same error:
test.c:2:10: fatal error: gtk/gtk.h: No such file or directory
2 | #include <gtk/gtk.h>
| ^~~~~~~~~~~
I've tried configuring c_cpp_properties.json to manually add the paths:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}",
"C:\\msys64\\mingw64\\lib\\**",
"C:\\msys64\\mingw64\\include\\**",
"C:\\msys64\\mingw64\\include\\gtk-3.0",
"C:\\msys64\\mingw64\\include\\gtk-3.0\\gtk",
"C:\\msys64\\mingw64\\include\\glib-2.0",
"C:\\msys64\\mingw64\\include\\pango-1.0",
"C:\\msys64\\mingw64\\include\\harfbuzz",
"C:\\msys64\\mingw64\\include\\cairo",
"C:\\msys64\\mingw64\\include\\gdk-pixbuf-2.0",
"C:\\msys64\\mingw64\\include\\atk-1.0",
"C:\\Users\\Acer\\Code\\vcpkg\\packages\\gtk_x64-windows\\lib",
"C:\\Users\\Acer\\Code\\vcpkg\\packages\\gtk_x64-windows\\include\\gtk-4.0\\**",
"C:\\Users\\Acer\\Code\\vcpkg\\installed\\x64-windows\\lib"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30133\\bin\\Hostx64\\x64\\cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
Although the ridicule lenght (beware that i've tried every combination possible of these paths: different order, with more, with less, etc...), these still didn't work.
As you should have noticed i've downloaded gtk via msys64 and vcpkg, the reason being that i then tried running on the Visual Studio IDE, which also didn't work:
E1696: cannot open source file "gtk/gtk.h"
(I thought it might not be recognizing vcpkg path yet changing it still didn't work, neither trying to compile on x64 or x86).
Lastly i tried compiling it myself on gcc:
gcc test.c `pkg-config --cflags --libs gtk+-3.0`
And got the following errors:
gcc.exe: error: pkg-config: No such file or directory
gcc.exe: error: gtk+-3.0`: No such file or directory
gcc.exe: error: unrecognized command line option '--cflags'
gcc.exe: error: unrecognized command line option '--libs'
Changing the command to this variation made it at least give the same error as VSCode:
gcc $(pkg-config --cflags gtk+-3.0) test.c $(pkg-config --libs gtk+-3.0)
I then called pkg-config to see the path that it would show:
pkg-config --cflags --libs gtk+-3.0
-IC:/msys64/mingw64/bin/../include/gtk-3.0 -IC:/msys64/mingw64/bin/../include/pango-1.0 -IC:/msys64/mingw64/bin/../include -IC:/msys64/mingw64/bin/../include/glib-2.0 -IC:/msys64/mingw64/bin/../lib/glib-2.0/include -IC:/msys64/mingw64/bin/../include/harfbuzz -IC:/msys64/mingw64/bin/../include/freetype2 -IC:/msys64/mingw64/bin/../include/libpng16 -mms-bitfields -IC:/msys64/mingw64/bin/../include/fribidi -IC:/msys64/mingw64/bin/../include/cairo -IC:/msys64/mingw64/bin/../include/lzo -IC:/msys64/mingw64/bin/../include/pixman-1 -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -IC:/msys64/mingw64/bin/../include/gdk-pixbuf-2.0 -mms-bitfields -mms-bitfields -mms-bitfields -IC:/msys64/mingw64/bin/../include/atk-1.0 -mms-bitfields -mms-bitfields -mms-bitfields -pthread -mms-bitfields -LC:/msys64/mingw64/bin/../lib -lgtk-3 -lgdk-3 -lz -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lwinmm -ldwmapi -lsetupapi -lcfgmgr32 -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
Then tried passing that directly to gcc and got the following error:
On line:1 character:942
+ ... ib -lgtk-3 -lgdk-3 -lz -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid - ...
+ ~
Argument missing from list of parameters.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
Wonderful. I then tried adding that to my Path on the system variables. It did not work. Also made sure that all previous paths mentioned before were also there. Nothing works.(Should also mention than other than having multiple gtk+-3.0 installations on my computer one of which is found by pkg-config, i've also installed gtk+-4.0 via vcpkg, this one is not found by pkg-config).
Other than finding out how to finally use gtk on my pc, i would like, if possible, to known what did i do horribly wrong in each of the tries above so that none of them worked.
Thank You.
I'm building a program which uses GTK+3 and pango. Most of it compiles fine apart from the last bit which builds the executable. The final command is:
gcc -o z80sim main.c -Wall -Iz80 -Igui obj/disas.o obj/iosim.o obj/sim0.o obj/sim1.o obj/sim2.o obj/sim3.o obj/sim4.o obj/sim5.o obj/sim6.o obj/sim7.o obj/simctl.o obj/simfun.o obj/simglb.o obj/simint.o obj/callbacks.o obj/code.o obj/guiglb.o obj/memory.o obj/flags.o obj/log.o obj/ports.o obj/registers.o `pkg-config --cflags --libs gtk+-3.0`
But I get this error message:
obj/sim1.o: In function `cpu':
sim1.c:(.text+0x2cb): undefined reference to `check_gui_break'
obj/callbacks.o: In function `on_open1_activate':
callbacks.c:(.text+0x20): undefined reference to `Get_File'
:
:
Where check_gui_break is defined and called in sim1.c etc.
This has to be something to do with the ordering of object files and libraries on the ld call, but I have tried putting the pkg-config part before and after the individual .o files and it still gives the same errors.
I've read the very informative https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking but, although I know a lot more now, I still can't crack this one.
BTW.
pkg-config --cflags --libs gtk+-3.0
gives
-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/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -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/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -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
Please will some kind soul take pity on me and enlighten me?
It appears that most of the errors I've encountered have been a result of the transition from Glade2 to Glade3. In 2, Glade generates some C code which is what's missing from my code. Back to the reference manual to see how to modernise the code.
I'm new at C and started to learn how to create GUI.
For some reason the only way program run is
gcc simple.c -o simple -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0 -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12
If i just run gcc simple.cit gives me
In file included from /usr/include/glib-2.0/glib/galloca.h:32:0,
from /usr/include/glib-2.0/glib.h:30,
from /usr/include/glib-2.0/gobject/gbinding.h:28,
from /usr/include/glib-2.0/glib-object.h:23,
from /usr/include/glib-2.0/gio/gioenums.h:28,
from /usr/include/glib-2.0/gio/giotypes.h:28,
from /usr/include/glib-2.0/gio/gio.h:26,
from /usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30,
from /usr/include/gtk-2.0/gdk/gdk.h:32,
from /usr/include/gtk-2.0/gtk/gtk.h:32,
from graphical.c:1:/usr/include/glib-2.0/glib/gtypes.h:32:24: fatal error: glibconfig.h: No such file or catalog compilation terminated.
If I do other 'traditional' ways to include lib:
gcc `pkg-config gtk+-2.0 --cflags` example.c -o example `pkg-config gtk+-2.0 --libs`
It gives me:
Package gtk+-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-2.0.pc' to the PKG_CONFIG_PATH environment variable
No package 'gtk+-2.0' found
graphical.c:1:21: fatal error: gtk/gtk.h: No such file or catalog
What is the right way to include GTK?
I just figured out what was the problem.
I had linuxbrew installed and for some reason pkg-config took path from one of it's repos even if I force it to export.
I completely removed linuxbrew and now it's finally working!
I am trying to compile a gtk program using the tutorial here. When I issue the command
gcc -o tut tut.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
I get the following error:
gcc: error: unrecognized command line option ‘-pthread -I/usr/include/gtk-2.0
-I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0
-I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0
-I/usr/include/gio-unix-2.0/ -I/usr/include/freetype2 -I/usr/include/glib-2.0
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1
-I/usr/include/libpng12 -I/usr/include/harfbuzz -Wl,--export-dynamic -pthread
-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0
-lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lgobject-2.0 -lfreetype
-lgmodule-2.0 -lglib-2.0 ’
gcc is version 4.8.2. pkg-config is version 0.26. i have libgtk2.0-dev installed.
I can compile simple c programs fine.
How do I resolve the "unrecognized command" problem?
[update from comment]
I am using zsh.
This looks like a shell issue.
What shell are you using?
In case it's not bash, give bash a try.