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

问题描述

我在谷歌上找了 2 天没有成功地找到一个清晰的段落来描述如何包含动态库(.dylibMac iOS 上具有扩展名的文件)以便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;

不得不提的是,我成功地按照以下代码片段在OpenGL main.cpp文件和头文件中进行了clang++编译: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
        }
      }
      
      
    ]
  }

向所有读者致以最美好的问候。

标签: c++macosvscode-settingsclang++dylib

解决方案


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

任务.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
        }
      }
      
      
    ]
  }

正如大家所看到的,只需将文件的完整路径写为附加参数,编译器就会接受


推荐阅读