我正在使用
- g++
- GTK3
- VSCode
如何使以下内容正常工作:
- gtk的智能感知/代码补全
- 在VSCode内构建
- 使用VSCode调试
问题:
VSCode无法找到包含文件-特别是源代码中的#include <gtk/gtk.h>
是红色的。
我正在使用
如何使以下内容正常工作:
问题:
VSCode无法找到包含文件-特别是源代码中的#include <gtk/gtk.h>
是红色的。
需要注意的是,您需要告诉VSCode包含路径和编译器标志才能正常工作。
.vscode
的新隐藏文件夹。打开它。pkg-config --cflags gtk+-3.0
和pkg-config --libs gtk+-3.0
的输出分别应用于它们各自的配置中。.vscode/c_cpp_properties.json
。添加以下内容。
{
"env": {
"myDefaultIncludePath": [
"${workspaceFolder}",
"${workspaceFolder}/include"
],
"myCompilerPath": "/usr/local/bin/g++"
},
"configurations": [
{
"name": "include paths",
"intelliSenseMode": "g++-8",
"includePath": [
"/usr/include/gtk-3.0",
"/usr/include/at-spi2-atk/2.0",
"/usr/include/at-spi-2.0",
"/usr/include/dbus-1.0",
"/usr/lib/x86_64-linux-gnu/dbus-1.0/include",
"/usr/include/gtk-3.0",
"/usr/include/gio-unix-2.0",
"/usr/include/cairo",
"/usr/include/libdrm",
"/usr/include/pango-1.0",
"/usr/include/harfbuzz",
"/usr/include/pango-1.0",
"/usr/include/fribidi",
"/usr/include/atk-1.0",
"/usr/include/cairo",
"/usr/include/pixman-1",
"/usr/include/freetype2",
"/usr/include/libpng16",
"/usr/include/gdk-pixbuf-2.0",
"/usr/include/libmount",
"/usr/include/blkid",
"/usr/include/uuid",
"/usr/include/glib-2.0",
"/usr/lib/x86_64-linux-gnu/glib-2.0/include"
],
"compilerPath": "/usr/local/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
请注意,“includePath”的内容是pkg-config --cflags gtk+-3.0
的输出,但不包括前面的-I
并且带有双引号和逗号。您可能需要根据您机器的输出来调整这些值。
您想在.vscode/tasks.json
中创建一个新任务,其内容如下:
{
"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
}
}
args
中的两个更缩进的部分。pkg-config --cflags gtk+-3.0
的输出。(这次包括-I
。)pkg-config --libs gtk+-3.0
的输出(带引号和逗号)。你想在.vscode/launch.json
文件中创建一个新的配置。在我的设置中,vscode一直使用错误的配置,因此我删除了其他配置。下面是只有一个配置的文件的完整内容。
{
// 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"
}
]
}
使用怎么样?
"`pkg-config --cflags --libs gtk4`"
请注意
`pkg-config`
在 pkg-config 之间,它与其他不同。
'pkg-config'
要查看完整文件,这里是我上传的tasks.json和c_cpp_properties.json文件的要点 https://gist.github.com/KesunyianAyam/48810a1f4339f496e192f4e94adc6e3b
我的是gtk4,但可以通过使用来用于gtk3
`pkg-config --cflags --libs gtk+-3.0`
并使用与gtk3相关的包含文件
tasks.json
中手动指定标志,就像顶部答案中所做的那样。这个问题在2021年得到了修复。请参阅https://github.com/microsoft/vscode-cpptools/pull/7895
您可以在args
中使用反引号调用pkg-config。 "args": [
"-g",
"${file}",
"`pkg-config --libs --cflags gtk4`",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
$(...)
语法用于命令替换,但仍然无法正常工作。
tasks.json
调用pkg-config
。如果可以的话,你应该这样做,而不是手动指定标志。 - HolyBlackCatpkgconfig
输出的整个参数放在单引号中,因此编译器将其视为一个整体选项,而不是一堆选项。 - ADBeveridge