For creation of C++ library you can refer to the MSDN. I'm following these steps and enhance on the same codes to produce C DLL.
- In order to access a library file, we can utilize the Dependency Walker.
- The library that generated by the sample code by MSDN is as picture below. Take note of the difference between C++ functions and C functions.
- To export C functions, we can the export function names in .def file
- Header file will look something like this. The function for the Add is normal declaration in Header file.
- The implementation code is a standard code.
- #include "stdafx.h"
- #include "CryptoCPPDLL.h"
- #include <stdexcept>
- using namespace std;
- namespace CryptoFuncs
- {
- double CryptoCPPDLL::Add(double a, double b)
- {
- return a + b;
- }
- double CryptoCPPDLL::Subtract(double a, double b)
- {
- return a - b;
- }
- double CryptoCPPDLL::Multiply(double a, double b)
- {
- return a * b;
- }
- double CryptoCPPDLL::Divide(double a, double b)
- {
- if (b == 0)
- {
- throw invalid_argument("b cannot be zero!");
- }
- return a / b;
- }
- double CryptoCPPDLL::Add2(double a, double b)
- {
- return a + b;
- }
- double CryptoCPPDLL::Subtract2(double a, double b)
- {
- return a - b;
- }
- double CryptoCPPDLL::Multiply2(double a, double b)
- {
- return a * b;
- }
- double CryptoCPPDLL::Divide2(double a, double b)
- {
- if (b == 0)
- {
- throw invalid_argument("b cannot be zero!");
- }
- return a / b;
- }
- void CryptoCPPDLL::PassStructIn(MyStruct* myStruct)
- {
- if (myStruct != NULL)
- {
- myStruct->SomeId = 234;
- myStruct->SomePrice = 456.23;
- }
- }
- void CryptoCPPDLL::PassStructIn2(MyStruct* myStruct)
- {
- if (myStruct != NULL)
- {
- myStruct->SomeId = 234;
- myStruct->SomePrice = 456.23;
- }
- }
- void PassStructIn3(MyStruct* myStruct)
- {
- if (myStruct != NULL)
- {
- myStruct->SomeId = 234;
- myStruct->SomePrice = 456.23;
- }
- }
- double Add3(double a, double b)
- {
- return a + b;
- }
- }
- The second way to do this is through extern "C" __declspec(dllexport) YOUR_FUNCTION_NAME.
- Hope this helps. I will create another post to how to do it in Eclipse using GCC.