如何在“.json”配置文件中包含 .dylib 文件? - Mac iOS 上的 VS 代码 - Clang++ 编译器 - 语言:c++

How can I include .dylib files in ".json" configuration files? - VS Code on Mac iOS - Clang++ compiler - Language: c++

我在 google 上花了 2 天时间寻找一个清晰的段落来描述如何包含动态库(在 Mac iOS 上扩展名为 .dylib 的文件),但没有成功为了在有人设置 task.json and/or c_cpp_properties.json[=53 时被 clang++ 编译=] 文件 - (在按 F5 以启动任务和执行源代码之前)

特别是我想包含接下来的两个 .dylib 文件:

  1. /usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib;
  2. /usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib;

不得不提的是,我成功地使 clang++OpenGL main.cpp 文件中编译 glew.hglfw3.h headers 根据以下片段:

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
...

此任务已完成写入下一个 c_cpp_properties.json 文件:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
              ], 
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "macos-gcc-x64"
        }
    ],
    "version": 4
}

魔术是由 "macFrameworkPath" 指令完成的。

但这还不够。 clang++ 编译时我仍然有这个错误:

clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

正如我在谷歌搜索解决方案后了解到的那样,发生这种情况是因为有必要包含我之前提到的那两个动态库。 但正如我一开始所说,我没有找到如何去做。 也许有人可以想出一个明确而适当的解决方案。 我最好的猜测是在 task.json 脚本中添加一个新参数,顺便说一下,它看起来像这样:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "-g",
          "${file}",
          "-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
          "-o",
          "${fileDirname}/src/${fileBasenameNoExtension}",
          "-Wno-deprecated",
          "-Wno-pragma-once-outside-header"
        ],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
      
      
    ]
  }

向所有读者致以最诚挚的问候。

看来您确实需要将库传递给 clang++ 的参数。

尝试使用 -l 标志来执行此操作,指向目标库。

添加 "-l /usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib"

"-l /usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib" 到 args 字符串列表。

这个特定问题的解决方案是在 task.json 中添加下一个命令参数:

task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "-g",
          "${file}",
          "-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
          "/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib",
          "/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib",
          "-o",
          "${fileDirname}/src/${fileBasenameNoExtension}",
          "-Wno-deprecated",
          "-Wno-pragma-once-outside-header"
        ],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
      
      
    ]
  }

如大家所见,只需将文件的完整路径写为附加参数即可被编译器接受