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