1

目录结构

laolang@laolang-PC:~/code/vscode/cstudy$ tree -a
.
├── CMakeLists.txt
├── include
│   └── tool.h
├── src
│   ├── CMakeLists.txt
│   ├── main.c
│   └── tool.c
└── .vscode
    ├── c_cpp_properties.json
    ├── launch.json
    └── tasks.json

3 directories, 8 files
laolang@laolang-PC:~/code/vscode/cstudy$ 

源码

tool.h

#ifndef TOOL_H
#define TOOL_H

int sum( int a, int b );

#endif

tool.c

#include "../include/tool.h"

int sum( int a, int b ){
    return a + b;
}

main.c

#include <stdio.h>

#include <gtk/gtk.h>

#include "../include/tool.h"

static void
activate(GtkApplication *app,
    gpointer user_data) {
    GtkWidget *window;
    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Hello GNOME");
    gtk_widget_show_all(window);
}
int
main(int argc, char *argv[]) {

    int a = 1;
    int b = 2;
    a = 2;
    a = 3;
    a = 4;
    printf("%d + %d = %d\n",a,b,sum(a,b));



    GtkApplication *app;
    int status;
    app = gtk_application_new("org.gtk.example",
        G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate",
        G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return (status);
}

vscode 配置

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/bin/kmdatastruct", // 可执行文件
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "compile", // task.json 中的 label
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "type": "shell",
            "command": "if [ ! -d \"./build\" ]; then  mkdir build; fi && cd build && cmake .. && make",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

c_cpp_properties.json

此文件主要配置第三方库的提示 ,配置之后最好重启vscode

{
    "configurations": [
        {
            "name": "Linux",
            "browse": {
                "path": [
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "${workspaceFolder}",
                // 下面的目录来自:pkg-config --cflags gtk+-3.0
                "/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/pango-1.0",
                "/usr/include/harfbuzz",
                "/usr/include/pango-1.0",
                "/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/libpng16",
                "/usr/include/glib-2.0",
                "/usr/lib/x86_64-linux-gnu/glib-2.0/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

运行

F5

参考

VSCode中C/C++库文件的配置


khlbat
26 声望0 粉丝

khl