Visual Studio Code settings for C++ (a) static and (b) dynamic library linking

Can I have some assistance configuring Visual Code (not Visual Studio) to build a C++ library? Both static and most importantly a dynamic library? Visual Code requires that you create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the Microsoft C++ compiler to create an executable file based on the source code.

My current tasks.json is as follows:

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "cl.exe build active file",
        "command": "cl.exe",
        "args": [
          "/Zi",
          "/EHsc",
          "/Fe:",
          "${fileDirname}${fileBasenameNoExtension}.exe",
          "${file}"
        ],
        "problemMatcher": ["$msCompile"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

In some scenarios, I need to link my main.cpp file dynamically (DLL) or statically (LIB). My library code is very simple – just two functions:

#include <stdio.h>
#include <stdlib.h>

void connect()
{
    printf("Connected to C extension...n");
}

//return random value in range of 0-50
int randNum()
{
    int nRand = rand() % 50; 
    return nRand;
}
 
//add two number and return value
int addNum(int a, int b)
{
    int nAdd = a + b;
    return nAdd;
}

I did my due diligence by doing a Google and Bing search but only landed on (a) Visual Studio and (b) .NET equivalents per the links below. I’m in need of a solution for C++ static and dynamic libaries.

https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp
https://code.visualstudio.com/docs/cpp/config-msvc

……………………………

BONUS QUESTION

While I’m here, are there any specific instructions needed to make in the tasks.json file for my my second solution where I’ll take my simple main.cpp file to call the methods from the code above? Meaning what adjustments are to be made to the tasks.json to (a) statically and (b) dynamically link to it?

Thanks everyone.

Source: Windows Questions C++

LEAVE A COMMENT