用于 GTK 开发的 VS Code C/C++ 配置 [英] VS Code C/C++ configuration for GTK development

查看:127
本文介绍了用于 GTK 开发的 VS Code C/C++ 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Linux Ubuntu 上的 VS Code 中的 C/C++ 编程设置一个友好的环境.多年来我一直在使用 Visual Studio 和 Code Blocks,但是 VS Code 设置太不清楚了,我几乎无法理解文档主题.目前我正在尝试编译几个hello world".程序,所以应该不难.

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.

假设我正在尝试构建和运行 GTK-3 hello世界计划.执行此操作的命令行说明是:

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`

在 bash 中使用时效果很好.所以首先我创建了一个名为 GTK-devel 的配置 c_cpp_properties.json 应该编译它.json文件内容为:

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
}

这是第一个问题:VS Code 中的配置是什么?在 Visual Studio 或 Code::Blocks 中,类似的事情在直观上是清晰且难以理解的——只需设置您需要的一切,然后在编译前在某处(菜单、对话框等)选择它.我想在 VS Code 中它有所不同,docs 根本不用解释——它只是说可以创建一个配置,但没有提到如何使用它.而且没有可以选择它的地方(终端、任务等),甚至在菜单等的任何地方都看不到.

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.

第二个问题涉及任务.因此,如果配置方法失败,我决定设置一个可以从主菜单清楚访问的任务.tasks.json 文件包含以下内容:

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"
        }
    ]
}

当我从菜单中选择时 Terminal ->运行构建任务 ->C/C++:gcc 编译 就被调用指令而言,一切似乎都很好,但缺少 libs 错误(包含并存在于系统中):

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

这里出现第三个问题:docs 并没有真正解释什么是终端.它说它默认为 $SHELL,但如果是这样,为什么编译器看不到这些库?

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?

所以最后一个问题是:VS Code 是一个真正的 IDE 还是它只是围绕它的一种营销方式,它可以用于在一个地方做许多不同的事情,但实际上它更好为每种语言使用专用工具等.因为设置问题会成功地分散您对您真正应该做的事情的注意力?

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:

此代码:

`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 不支持 `.所以我们所要做的就是复制并粘贴 task.json 中的每个参数:

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"
        ],

差点忘了.要智能感知文件 c_cpp_properties.json 只需在 includePath 处添加两个模式:

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/**"
        ]

就这样

这篇关于用于 GTK 开发的 VS Code C/C++ 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆