【问题标题】:How do I get Visual Studio Code to include non-standard libraries with angle brackets <>?如何让 Visual Studio Code 包含带尖括号 <> 的非标准库?
【发布时间】:2021-09-11 17:51:59
【问题描述】:

我正在尝试使用带有 VSC 扩展名 Code RunnerArduinoJson 库运行程序,但我无法编译它。

VSC 中没有标记错误或警告,但是当我尝试运行这个 sn-p 时:

#include "../External_Libraries/ArduinoJson/ArduinoJson.h"
#include <iostream>
int main(){
    std::cout << "Done.\n";
    return 0;}

我得到以下错误输出:

In file included from ../External_Libraries/ArduinoJson/src/ArduinoJson.hpp:17,\
                 from ../External_Libraries/ArduinoJson/src/ArduinoJson.h:9,\
                 from ../External_Libraries/ArduinoJson/ArduinoJson.h:5,\
                 from localtest.cpp:17:\ ../External_Libraries/ArduinoJson/src/ArduinoJson/Array/ArrayRef.hpp:7:10: fatal error:\ ArduinoJson/Array/ArrayFunctions.hpp: No such file or directory\
#include <ArduinoJson/Array/ArrayFunctions.hpp>\
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ compilation terminated.

在 ArduinoJson 库中,有一些使用双引号的包含命令和一些使用尖括号的命令:

#include "src/ArduinoJson.h"
//...   
#include <ArduinoJson/Array/ArrayFunctions.hpp>

似乎只有带尖括号的包含语句有问题。我已经尝试在 settings.json 以及 c_cpp_properties.json 中更新我的包含路径以涵盖这一点,但它没有奏效:

在 settings.json 中:

"C_Cpp.default.includePath": [
    "C:\\...\\project",
    "C:\\...\\project\\External_Libraries\\ArduinoJson\\src",
    "C:\\...\\project\\External_Libraries\\ArduinoJson\\src\\ArduinoJson\\Array"],
"C_Cpp.default.compilerPath": "C:\\MinGW\\bin\\gcc.exe"

在 c_cpp_properties.json 中:

"name": "Win32",
"includePath":[
    "${default}",
    "C:/.../project",
    "C:/.../project/External_Libraries/ArduinoJson/src/ArduinoJson/Array"],
"defines":[
    "_DEBUG",
    "UNICODE",
    "_UNICODE"],
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "windows-gcc-x86",
"compilerPath": "C:/MinGW/bin/gcc.exe",
"compilerArgs": ["-I C:\\...\\project\\External_Libraries"]

有人知道我可能做错了什么吗?

我的文件夹结构是

项目/
--src/
----localtest.cpp
--External_Libraries/
----ArduinoJson/

【问题讨论】:

    标签: c++ visual-studio-code include-path vscode-code-runner arduinojson


    【解决方案1】:

    我认为您应该将最后一个 includePath 更改为“C:/.../project/External_Libraries/ArduinoJson/src”。那是因为实际包含已经将它作为相对路径#include &lt;ArduinoJson/Array/ArrayFunctions.hpp&gt;

          "name": "Win32",
      "includePath":[
          "${default}",
          "C:/Users/pohl/Documents/Git/IDEAL_AgentsOnHardware",
          "C:/.../project/External_Libraries/ArduinoJson/src"],
      "defines":[
          "_DEBUG",
          "UNICODE",
          "_UNICODE"],
      "cStandard": "gnu17",
      "cppStandard": "gnu++14",
      "intelliSenseMode": "windows-gcc-x86",
      "compilerPath": "C:/MinGW/bin/gcc.exe",
      "compilerArgs": ["-I C:\\...\\project\\External_Libraries"]
    

    【讨论】:

    • 感谢您的回复。你有一个很好的观点,但不幸的是它没有奏效。我收到相同的错误消息。 VSC 中的不同事物可能有不同的包含路径吗?例如,Code Runner 扩展是否有特定的包含路径?
    • 您是否已经尝试为C_Cpp.default.includePath 进行相同的路径更改?如果没有,你能试试吗?
    • 我也尝试过 - 但无济于事。我仍然收到相同的错误消息。然而,当我在 Linux 机器上编译它时,我有一个名为“ArduinoJson”的符号链接到与 localtest.cpp 位于同一目录中的 ArduinoJson/src 目录,并且只需使用“-I ArduinoJson”来包含它 - 这有效!不幸的是,我不能在我的 Windows 机器上使用符号链接。
    • 我看了github.com/bblanchon/ArduinoJson。文件 ArduinoJson/src/ArduinoJson.hpp 没有那个包含。也许你已经改变了来源?你能检查一下吗。另外,尝试将 \\ 替换为 /。最后看看这个:stackoverflow.com/questions/37522462/…
    • 问题出现在 ArduinoJson.hpp 的第 17 行,其中包括“ArduinoJson/Array/ArrayRef.hpp”,在第 7 行的 ArrayRef.hpp 中我们发现#include 导致问题。我替换了所有 \\ 但它仍然不起作用。不过,您的链接问题给了我一个想法。我没有安装“Code Runner”,而是安装了“Compile Run”扩展并将其配置为使用 g++ 而不是带有编译器参数“-I C:/.../project/External_Libraries/ArduinoJson/src”的 gcc -> 这有效!我猜 Code Runner 扩展似乎使用了不同的编译器和路径。
    【解决方案2】:

    我找到了一种解决方法:我安装了 Compile Run 扩展而不是 Code Runner,并将其配置为使用 g++ 而不是 gcc,并带有编译器参数“-IC:/.../project/External_Libraries/ArduinoJson/src” - > 这行得通!

    在 settings.json 中:

    "c-cpp-compile-run.cpp-compiler": "C:/MinGW/bin/g++.exe",
    "c-cpp-compile-run.cpp-flags": "-Wall -Wextra -Wa,-mbig-obj -I C:/.../project/External_Libraries/ArduinoJson/src",
    

    Code Runner 扩展似乎使用了不同的编译器和路径,我无法正确更新。 Compile Run 允许为这个扩展设置特定的编译器和路径,所以在我看来这更容易处理。

    我仍然非常想知道如何正确更新编译器并包含 Code Runner 的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-04
      • 2012-01-06
      • 2016-08-15
      • 1970-01-01
      • 2018-04-07
      • 2019-12-02
      • 1970-01-01
      • 2014-12-14
      相关资源
      最近更新 更多