I am trying to make a poc which I want to create a pure static C++ library. I guess I am in the right away. Here is my code
This is the header:
/*
* Library's Header: MathFuncsLib.h
*/
namespace MathFuncs
{
extern "C" {
// Returns a + b
_declspec(dllexport) double Add(double a, double b);
// Returns a - b
_declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
_declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
_declspec(dllexport) double Divide(double a, double b);
_declspec(dllexport) void PrintSomething();
}
}
and my cpp file:
/*
* MathFuncsLib.cpp
*/
#include "MathFuncsLib.h"
using namespace std;
namespace MathFuncs
{
double Add(double a, double b)
{
return a + b;
}
double Subtract(double a, double b)
{
return a - b;
}
double Multiply(double a, double b)
{
return a * b;
}
double Divide(double a, double b)
{
return a / b;
}
}
I am using this library in a simple C# code using P Invoke:
/*
* Call a C++ library - PInvoke method
*
*/
using System;
using System.Runtime.InteropServices;
namespace CppConsoleAppTest
{
public class Program
{
[DllImport("MathFuncsLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double Add(double a, double b);
static void Main(string[] args)
{
double aValue = 20.0;
double bValue = 10.0;
var res = Add(aValue, bValue);
Console.WriteLine("The sum is : " + res.ToString());
Console.ReadLine();
}
}
}
So, as you can see I am importing the .dll file from my library. When I check my dependencies (using Dependency Walker) it is dependent from the MSVCP120D.dll and MSVCR120D.dll . When I try to run the C# .exe on a clean machine, it crashes because it is missing those files (MSVCP120D.dll and MSVCR120D.dll).
How could I get free of this dependency? The Visual Studio included automatically in my project the folder "External Dependencies". Should I delete it (how to do that) in order to get free from the MSVC dependency? Thank you!