Search code examples
c++macosvisual-studio-codeclang++dylib

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


I unsuccessfully was looking 2 days on google to find a clear paragraph to describe how to include dynamic libraries (files with .dylib extension on Mac iOS) in order to be compiled by clang++ when someone is setting up the task.json and/or c_cpp_properties.json files - (prior to press F5 in order to launch the task and execute the source code)

Particularly I would like to include next two .dylib files:

  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;

Have to mention that I successfully succeeded to make the clang++ to compile in the OpenGL main.cpp file both glew.h and glfw3.h headers as per the following snippet:

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

This task was accomplished writing the next c_cpp_properties.json file:

{
    "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
}

where the magic is done by "macFrameworkPath" instruction.

But yet is not enough. I still have this error when the clang++ compiles:

clang: error: linker command failed with exit code 1 (use -v to see invocation).

And this - as I understood after googling the solution - happens because is necessary to include those two dynamic libraries which I mentioned earlier. But as I said in the beginning I didn't find how to do it. Maybe someone can come up with a clear and appropriate solution. My best guess is to add a new argument in task.json script, which by the way looks like that:

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

Best regards to all readers.


Solution

  • The solution to this particular problem is to add in task.json the next command arguments:

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

    As everyone can see, simply writing the full path of the file as additional argument will be accepted by compiler