C 语言新手求助:如何在 vscode 中使用第三方库? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
craiiz
V2EX    C

C 语言新手求助:如何在 vscode 中使用第三方库?

  •  
  •   craiiz 2024-07-08 14:18:43 +08:00 3698 次点击
    这是一个创建于 460 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    想要达成的目的

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

    当前情况及无法解决的问题(已 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" } ], "version": 4 } 

    tasks.json:

    { "version": "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. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "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; } 
    13 条回复    2024-07-10 15:31:30 +08:00
    ConfusedBiscuit
        1
    ConfusedBiscuit  
       2024-07-08 14:40:34 +08:00
    我很少用 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
        2
    hhjuteman  
       2024-07-08 15:04:03 +08:00
    你看下你这个
    "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
        3
    Donaldo  
       2024-07-08 15:04:14 +08:00
    "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 的库。
    hhjuteman
        4
    hhjuteman  
       2024-07-08 15:05:05 +08:00
    仔细看了一眼,应该是执行到底下那个配置去了。
    里面有个
    "group": {
    "kind": "build",
    "isDefault": true
    },

    "isDefault": true 有点明显
    zhuangzhuang1988
        5
    zhuangzhuang1988  
       2024-07-08 15:12:34 +08:00
    fpure
        6
    fpure  
       2024-07-08 15:16:10 +08:00   1
    别用微软推荐的插件,用 clangd+codelldb+cmake tools ,构建工具用 cmake ,如同 Java 般的丝滑,爽飞
    nevermoreluo
        7
    nevermoreluo  
       2024-07-08 15:35:53 +08:00
    这么说吧,你写 python 的时候有个包管理 pip 是吧,咱 pip 管理库的时候有 pip install 是吧
    但是 c++里面没有,或者说有很多这东西,如果只是玩玩你挑一个吧,conan ,vcpkg ,cmake ,xmake 等等等等
    当然你图省事不想知道原理,想要图形化界面,clion 最新版集成了 vcpkg ,点击安装就行了,fmt 这东西 vcpkg 肯定有

    祝你幸福
    nevermoreluo
        8
    nevermoreluo  
       2024-07-08 15:37:01 +08:00
    哦哦 注意到你的 includePath 里面有 vcpkg ,如果你知道你在干什么的话,你看看 fmt 里面关于 vcpkg 的安装文档吧,https://fmt.dev/11.0/get-started/
    如果写成这样教程里面应该有教你怎么用 vcpkg 了,你再看看吧相关内容吧
    noahlias
        9
    noahlias  
       2024-07-08 15:48:35 +08:00
    上面有人说了 c++的问题 fmt 我记得是 c++的库
    你得用 g++或者 clang++编译
    NessajCN
        10
    NessajCN  
       2024-07-08 15:49:15 +08:00
    你不是 C 吗,怎么会有 fmt::print()
    proxytoworld
        11
    proxytoworld  
       2024-07-08 16:03:54 +08:00
    别用 vscode 自带的编译,用 cmake ,引用相应的库,配置之后直接编译
    echoechoin
        12
    echoechoin  
       2024-07-09 18:23:29 +08:00
    clangd 插件很好用,只要你的代码能编译过,就可以找到任意的符号
    craiiz
        13
    craiiz  
    OP
       2024-07-10 15:31:30 +08:00
    @all
    感谢各位!!我觉得 @ConfusedBiscuit @proxytoworld 说的很对。目前还是自己用 g++命令编译解决了。

    @noahlias
    @NessajCN
    对,确实是 c++库,一开始是问 gpt c 语言怎么用第三方库,然后就给我推了这个; vcpkg 安装后怎么都编译不过,然后 google 加上 -v 参数后的报错信息,才发现是 c++库。最后是 github 下载后编译,再在脚本(改成了 c++)里面使用。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2686 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 13:49 PVG 21:49 LAX 06:49 JFK 09:49
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86