// MathLibrary.h - Contains declaration of Function class
#pragma once
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
namespace MathLibrary
{
// This class is exported from the MathLibrary.dll
class Functions
{
public:
// Returns a + b
static MATHLIBRARY_API double Add(double a, double b);
// Returns a * b
static MATHLIBRARY_API double Multiply(double a, double b);
// Returns a + (a * b)
static MATHLIBRARY_API double AddMultiply(double a, double b);
};
}
// MathLibrary.cpp : Defines the exported functions for the DLL application.
// Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp
#include "stdafx.h"
#include "MathLibrary.h"
namespace MathLibrary
{
double Functions::Add(double a, double b)
{
return a + b;
}
double Functions::Multiply(double a, double b)
{
return a * b;
}
double Functions::AddMultiply(double a, double b)
{
return a + (a * b);
}
}
How to make the application project import the dll file instead of from the project?
I gather that I need to generate a .lib file.
However, the command `TlbExp.exe MyDLL.dll` returns the following message:
TlbExp : error TX0000 : Could not load file or assembly 'file:///C:\...\MyDLL.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
.lib is a static library where functions and procedures can be placed and called as the application is being compiled. A DLL does the same function but is dynamic in a sense that the application can call these libraries during run-time and not during the compilation. You don't need .lib to to use the dll.
You just go to the project you want to use the dll and
right click -> add -> reference (then locate the dll from file chooser and you are good to go)
Once you get the dll in than the rest of your code should compile OK. Just make sure to use the same namespace.(I used to do this mistake a lot) Once again if you need static library at compile time then you can generate .lib file, then you have to compile it from the dll project.