1

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!

  • 1
    You need also to link with the static library equivalents of those Microsoft's runtime DLLs. If Microsoft provides them, figure out how to link with them. If Microsoft does not provide statically-linked versions of their runtime libraries, this cannot be done. – Sam Varshavchik Aug 18 '16 at 12:30
  • 1
    Yes, static linking is supported. The procedure hasn't changed since VS 2002. http://stackoverflow.com/questions/14749662/microsoft-visual-studio-c-c-runtime-library-static-dynamic-linking – Cody Gray - on strike Aug 18 '16 at 12:31
  • @CodyGray, thank you. I found the answer I needed in the link you wrote. The solution was just changing the Runtime Library setting to /MT. – Henrique Amaral Aug 18 '16 at 16:04

0 Answers0