致命错误:glib.h:使用vscode和mingw时没有此类文件或目录

问题描述

在尝试将glib.h包含到项目中时,出现以下致命错误。 mingw gcc找不到glib.h。

严重错误:glib.h:没有这样的文件或目录

我已将问题隔离在一个简单的helloworld.c文件中。

enter image description here

在Ctrl-Shift-B建立时,我明白了。

enter image description here

我还按照我阅读here的说明,在c_cpp_properties.json文件中更新了我的includePath。

有人对我应该研究的内容解决此问题有任何想法吗?我花了很多时间,但是在这方面没有取得任何进展。 :(

helloworld.c是:

#include <stdio.h>

#include <glib.h>


void main() {
    
    printf("Hello World!");

    char c = getchar();

}

c_cpp_properties.json是:

{
    "configurations": [
        {
            "name": "MingGW","includePath": [
                "${workspaceFolder}/**","C:\\cryptolibs\\msys2-64\\mingw64\\include\\glib-2.0","C:\\cryptolibs\\msys2-64\\mingw64\\lib\\glib-2.0\\include","C:\\cryptolibs\\pbc-0.5.14\\include"
            ],"defines": [
                "_DEBUG","UNICODE","_UNICODE"
            ],"windowsSdkVersion": "10.0.17763.0","compilerPath": "C:\\MinGW\\bin\\g++.exe","cStandard": "c11","cppStandard": "c++17","intelliSenseMode": "clang-x64","browse": {
                "path": [
                    "${workspaceFolder}","C:\\cryptolibs\\pbc-0.5.14\\include"
                ],"limitSymbolsToIncludedHeaders": true,"databaseFilename": ""
            }
        }
    ],"version": 4
}

launch.json是:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more @R_498_4045@ion,visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0","configurations": [
        
        {
            "name": "gcc.exe - Build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": true,"MIMode": "gdb","miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe","setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true
                }
            ],"prelaunchTask": ""
        }
    ]
}

tasks.json是:

{
    "tasks": [
        {
            "type": "shell","label": "C/C++: gcc.exe build active file","command": "C:\\MinGW\\bin\\gcc.exe","args": ["-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {
                "cwd": "${workspaceFolder}"
            },"problemmatcher": ["$gcc"],"group": {
                "kind": "build","isDefault": true
            }
        }
    ]
}

解决方法

要使用glib.h,您需要在系统上安装glib2。 如果您有外壳程序(例如MSYS2),通常可以使用以下命令获取编译器标志:

pkg-config --cflags glib-2.0

以及带有以下内容的链接器标志:

pkg-config --cflags glib-2.0

例如,在我的MSYS2系统上,返回:

-LD:/Prog/winlibs64-10.2.0/custombuilt/lib -lglib-2.0 -lintl

-mms-bitfields -ID:/Prog/winlibs64-10.2.0/custombuilt/include/glib-2.0 -ID:/Prog/winlibs64-10.2.0/custombuilt/lib/glib-2.0/include -ID:/Prog/winlibs64-10.2.0/custombuilt/include

分别。