我正在努力为Linux Ubuntu上的VS Code中的C/C++编程建立一个友好的环境。多年来,我一直在使用Visual Studio和代码块,但VS代码设置非常不清楚,以至于我几乎无法理解文档主题。目前我正试图编译一些";你好世界";程序,所以不应该很难。
比方说,我正在尝试建立和运行GTK-3你好世界程序。执行此操作的命令行指令是:
gcc hello_world.c `pkg-config --cflags --libs gtk+-3.0`
在bash中使用时效果非常好。因此,首先我创建了一个名为GTK-devel
的配置c_cpp_properties.json
,它应该编译这个。json文件内容为:
{
"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
}
第一个问题来了:VS代码中的配置是什么?在Visual Studio或Code::Blocks中,类似的东西直观上很清楚,也不需要说明——只需设置所需的一切,然后在编译前在某个地方(菜单、对话框…)进行选择。我想在VS Code中它是不同的,文档根本没有解释-它只是说可以创建一个配置,但没有提到如何使用它。此外,没有一个可以选择它的地方(终端、任务等),也没有菜单中任何地方都可见的地方。
第二个问题涉及任务。所以,如果配置方法失败了,我决定设置一个任务,我可以从主菜单中清楚地访问它。tasks.json
文件具有以下内容:
{
"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"
}
]
}
当我从菜单Terminal -> Run build task -> C/C++: gcc compilation
中选择时,就调用的指令而言,一切似乎都很好,但有一些缺失的libs错误(包括在系统中):
"/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
这里出现了第三个问题:同样,文档并没有真正解释什么是终端。它说它默认为$SHELL
,但如果是这样,为什么编译器看不到库?
所以最后一个问题是:VS Code是一个真正的IDE,还是它只是一个围绕它的营销,它可以用来在一个地方做很多不同的事情,但实际上最好为每种语言使用专用工具等,因为设置问题会成功地分散你对真正应该做的事情的注意力?
花了四个小时,我终于找到了解决方案。它既简单又有一半脏。我们开始了:
此代码:
`pkg-config --cflags --libs gtk+-3.0`
产生这样的东西(例如,这是我的环境):
-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不支持"之间的参数。因此,我们所要做的就是复制并粘贴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"
],
差点忘了。要智能感知文件c_cpp_properties.json
,只需在includePath
添加两个模式:
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**",
"/usr/lib/x86_64-linux-gnu/**"
]
就是