如何为 GTK3 配置 VSCode 以进行智能感知/构建/调试和 g++
Posted
技术标签:
【中文标题】如何为 GTK3 配置 VSCode 以进行智能感知/构建/调试和 g++【英文标题】:How to configure VSCode for GTK3 for intellisense / build / debug and g++ 【发布时间】:2020-03-04 05:11:35 【问题描述】:我正在使用
g++ GTK3 VSCode我如何使以下工作:
gtk 的智能感知/代码完成 在 VSCode 中构建 使用 VSCode 进行调试问题:
VSCode 找不到包含 - 特别是 #include <gtk/gtk.h>
在源代码中是红色的。
【问题讨论】:
【参考方案1】:需要注意的重要一点是,您需要告诉 VSCode 包含路径和编译器标志才能正常工作。
第一步:在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
s 并且带有双引号和逗号。 您可能需要根据机器的输出调整值
让建筑工作
您想在.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
s。)
底部是pkg-config --libs gtk+-3.0
的输出(引用和commated)
您可能还需要根据计算机上命令的实际输出来调整这些值
进行调试
您想在.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"
]
【讨论】:
我想知道你是否可以直接从tasks.json
调用pkg-config
。如果可以,您应该这样做,而不是手动指定标志。
或许,以某种方式有可能。如果有人知道如何更新答案,我很高兴:)
我试过了,但它包含了从 pkgconfig
输出的整个参数,单引号,因此编译器将它作为一个完整的选项,而不是一堆选项。以上是关于如何为 GTK3 配置 VSCode 以进行智能感知/构建/调试和 g++的主要内容,如果未能解决你的问题,请参考以下文章