跳转到内容
彼岸论坛
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

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


小天管理

已推荐帖子

背景是这样的:最近闲着报了个深圳人社的 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"
        }
    ],
    "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;
}

意见的链接
分享到其他网站

加入讨论

您现在可以发表并稍后注册. 如果您是会员,请现在登录来参与讨论.

游客
回复主题...

×   粘贴为富文本.   粘贴为纯文本来代替

  只允许使用75个表情符号.

×   您的链接已自动嵌入.   显示为链接来代替

×   您之前的内容已恢复.   清除编辑器

×   您无法直接粘贴图片.要从网址上传或插入图片.

  • 游客注册

    游客注册

  • 会员

    没有会员可显示

  • 最新的状态更新

    没有最新的状态更新
  • 最近查看

    • 没有会员查看此页面.
×
×
  • 创建新的...