Excel VBA, Can't Find DLL Entry Point from a DLL file
Solution 1:
I'm going to post this in a solution, as it doesn't fit in an comment.
The "inconsistent dll linkage" warning: I copied your exact code from the question as it is at this point (it might change in the future), and placed it in a newly created VStudio 2015 project:
- Configuration type: Dynamic Library (.dll)
- Using precompiled headers (although, I usually don't do it)
The project compiled with no warnings, if I define MATHLIBRARY_EXPORTS either:
- In the MathLibrary.cpp file
#define MATHLIBRARY_EXPORTS
(before#include "MathLibrary.h"
) - As a project setting: adding it under Project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions (next to other macros, separated by semicolons (;))
The only thing that I can imagine for you to still get the warning when building yours, is because you are defining the macro for the wrong configuration.
Example: you are building your project for Debug - x86, but you define the macro for Release - x86 (or Debug - x64).
You must check (it would be better select All Platfroms and All Configurations, and only define the macro once) that build configurations and settings configurations match, like in the image below:
But anyway, this warning is benign, the .dll is still built, and the symbols exported.
Going further, in your VBA module you declare the function name Add (plain). Based on the error message:
Can't find DLL entry point Add in "path\file.dll"
as I specified on one of my comments, I don't think that Excel is able to import C++ style exports because of [MS.Docs]: Decorated Names (C++ name mangling). While it searches for Add, your .dll exports the following symbols as shown in the (Dependency Walker) image below (you can play with the highlighted button and see how Dependency Walker is able to demangle those names):
Those (gibberish) names you should import from Excel, but I doubt that's possible. As a workaround you could either:
- Drop the C++ features (the class and the namespace) and define and export 3 simple functions
- Write 3 C functions (wrappers over the 3 methods), and export the functions not the methods
[SO]: Linker error when calling a C function from C++ code in different VS2010 project (@CristiFati's answer) contains all the details (pay attention on extern "C"
: [MS.Docs]: extern (C++)).