I am new on windows and trying to understand how __declspec(dllimport)
and __declspec(dllexport)
work. To do this, I created a solution with two projects : MathLibrary and MathClient. The MathLibrary project contains
MathLibrary.h
#pragma once
namespace MathLibrary
{
class __declspec(dllexport) Arithmetic
{
public:
// Returns a + b
static double Add(double a, double b);
// Returns a - b
static double Subtract(double a, double b);
// Returns a * b
static double Multiply(double a, double b);
// Returns a / b
static double Divide(double a, double b);
};
}
MathLibrary.cpp
// MathLibrary.cpp : Defines the functions for the static library.
//
#include "pch.h"
//#include "framework.h"
#include "MathLibrary.h"
// TODO: This is an example of a library function
namespace MathLibrary
{
double Arithmetic::Add(double a, double b)
{
return a + b;
}
double Arithmetic::Subtract(double a, double b)
{
return a - b;
}
double Arithmetic::Multiply(double a, double b)
{
return a * b;
}
double Arithmetic::Divide(double a, double b)
{
return a / b;
}
}
The MathClient projects contains
main.cpp
#include "MathLibrary.h"
#include <iostream>
using namespace std;
int main()
{
cout << MathLibrary::Arithmetic::Add(2, 3) << endl;
return 0;
}
If I don't put the __declspec(dllexport)
I get linking error, however, I don't need the __declspec(dllimport)
. Why is it not needed and where would I actually put it in the code?