微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我在 google 上搜索了 2 天,但没有成功找到一个清晰的段落来描述如何包含动态库(Mac iOS 上带有 .dylib 扩展名的文件)以便在有人时由 clang++ 编译设置 task.json 和/或 c_cpp_properties.json 文件 -(在按 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 标头,按照以下代码段:

// 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
        }
      }
      
      
    ]
  }

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。