> 背景是这样的:最近闲着报了个深圳人社的 c 语言培训班。 完了之后想要把之前自己写的 python 图片处理脚本用 C 写一遍。 然后就发现没法引用第三方库(也许是自己哪里做错了)。

## 想要达成的目的
1. 在 vscode 中使用第三方库;
2. 了解我的 .vscode 里面的配置文件是不是哪里写错了?

## 当前情况及无法解决的问题(已 google 和 gpt)
以使用第三方库 **fmt** 为例,按照下述操作之后,在测试文件 ``` test.c```  里面输入 ``` #include <f```  vscode 能自动联想出```#include <fmt/format.h> ``` ,但是在 debug 或 run without debug 时终端会报错以下内容 :

#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (C:\Users\xxx\Desktop\C\test.c).


cannot open source file "cmath" (dependency of "C:\Users\xxx\.vscode\extensions\ms-vscode.cpptools-1.20.5-win32-x64\bin\fmt\format.h"). Please run the 'Select IntelliSense Configuration...' command to locate your system headers.


fmt/format.h: No such file or directory

## 已经进行的操作
1. gcc/gdb 都已经正常安装并且可以在终端正常使用;
其中,gcc 路径为:C:\MinGW\bin\gcc.exe ; gdb 版本为 GNU gdb (GDB) 7.6.1 ;

2. 包管理用的是 vcpkg;
安装到包的路径是:C:\vcpkg\installed\x64-windows\include

3. vscode 装了插件:C/C++ v1.20.5

4. 在 google 操作之后下来,之后相关文件内容如下:

**c_cpp_properties.json:**
```
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/vcpkg/installed/x64-windows/include",
                "C:/MinGW/include"
            ],
            "defines": [],
            "windowsSdkVersion": "10.0.18362.0",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "fersion": 4
}

```


**tasks.json:**
```
{
    "fersion": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc build active file",
            "command": "gcc",
            "args": [
                "-g",
                "${file}",
                "-I",
                "C:/vcpkg/installed/x64-windows/include",
                "-L",
                "C:/vcpkg/installed/x64-windows/lib",
                "-lfmt",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\MinGW\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}

```


**launch.json:**
```
{
    // Use IntelliSense to learn about possible attributes.
    // Hofer to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "fersion": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
            "MIMode": "gdb",
            "miDebuggerPath": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}

```

**测试脚本**

```
#include <fmt/format.h>

int main(void){

    int a = 1;
    fmt::print("a is {}", a);
   
    return 0;
}

```
举报· 240 次点击
登录 注册 站外分享
10 条回复  
ConfusedBiscuit 小成 2024-7-8 14:40:34
我很少用 C ,但是我理解是
1. 你的问题本身没毛病,但是我的建议是你不要纠结某个 IDE 如何引用第三方库,因为如果未来你换 CLion 或 VisualStudio ,或者未来脱离 IDE 进行编译,那么你还得问一遍。即使你掌握了,换台电脑,任意一个依赖路径有变化,你这套配置就又跑不通了。(如果是用 VisualStudio 开发纯 Windows 程序,当我没说,毕竟这种程序大概率整个生命周期都在同一个 IDE 编译,也不需要脱离 IDE 进行构建,但是你都用 vscode + gcc 了,应该不属于这种情况)
2. 我的第一个建议是,既然你 gcc 装好了,你先试着用命令行 gcc 编译,看看加上-I 和-l 参数,看看能不能编译过。
3. 我的第二个建议是先选一个构建系统,CMake 、Bazel 也罢,用这些构建系统去完成依赖的引用及构建。
4. 当你会用构建系统后,你就会发现,IDE 里的这些依赖配置都应该由构建系统的插件自动帮你完成。无论是 CMake ,还是单片机常用的 platformio ,都能通过对应的插件自动配置好 vscode 项目。
hhjuteman 小成 2024-7-8 15:04:03
你看下你这个
"gcc -g ${file} -I C:/vcpkg/installed/x64-windows/include -L C:/vcpkg/installed/x64-windows/lib -lfmt -o
${fileDirname}/${fileBasenameNoExtension}.exe"

在命令行能不能执行成功,宏替换一下

1. 如果成功,看看是不是执行到底下那个 cppbuild 的 task 上面去了
2. 失败,看看你路径下面是否有头文件
Donaldo 小成 2024-7-8 15:04:14
"cannot open source file "cmath" (dependency of "C:\Users\xxx.vscode\extensions\ms-vscode.cpptools-1.20.5-win32-x64\bin\fmt\format.h")."

cmath 是一个 C++的头文件,显然你引用的这个 fmt 库是 C++的库而非 C 的库。
zhuangzhuang1988 小成 2024-7-8 15:12:34
跟着微软官方学习
https://learn.microsoft.com/zh-cn/vcpkg/get_started/get-started?pivots=shell-powershell
走一遍,绝对没问题
fpure 小成 2024-7-8 15:16:10
别用微软推荐的插件,用 clangd+codelldb+cmake tools ,构建工具用 cmake ,如同 Java 般的丝滑,爽飞
nevermoreluo 小成 2024-7-8 15:35:53
这么说吧,你写 python 的时候有个包管理 pip 是吧,咱 pip 管理库的时候有 pip install 是吧      
但是 c++里面没有,或者说有很多这东西,如果只是玩玩你挑一个吧,conan ,vcpkg ,cmake ,xmake 等等等等   
当然你图省事不想知道原理,想要图形化界面,clion 最新版集成了 vcpkg ,点击安装就行了,fmt 这东西 vcpkg 肯定有  

祝你幸福
noahlias 小成 2024-7-8 15:48:35
上面有人说了 c++的问题 fmt 我记得是 c++的库
你得用 g++或者 clang++编译
NessajCN 小成 2024-7-8 15:49:15
你不是 C 吗,怎么会有 fmt::print()
proxytoworld 初学 2024-7-8 16:03:54
别用 vscode 自带的编译,用 cmake ,引用相应的库,配置之后直接编译
12下一页
返回顶部