Question about mexfunction link to dll

3 views (last 30 days)
Hello there,
Does anyone have idea about how to link dll or lib in the Mex command?
I have a C code link a external function from GHXDLL.dll and GHXDLL.lib. I defined the external function in header that the same one i used to generate the dll and lib.
typedef struct GHX{
double output[28];
int val;
}GHX;
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
#endif
#ifdef GHXDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
MATHFUNCSDLL_API void GHXfunction(double *XIN, double *parameter, int mode, int hour, GHX *result);
#ifdef __cplusplus
}
#endif
I wrote a test code to call this function with linking to the lib and dll, and it works. Meanwhile, I wrote a simple test code for the Mex API, then I compile it from command line by
mex test.c,
and it works.
Then, I met problem. When I add the external function to the Mex API and compile in matlab with the following command, it fails.
mex -largeArrayDims -I'c:\Users\zzhang\API' -L'c:\Users\zzhang\API' -lGHXDLL GHXCmexAPI.c
The errors are:
Error using mex
Creating library GHXCmexAPI.lib and object GHXCmexAPI.exp
GHXCmexAPI.obj : error LNK2019: unresolved external symbol __imp_GHXfunction referenced in function mexFunction
GHXCmexAPI.mexw64 : fatal error LNK1120: 1 unresolved externals
I know this error means the mexfunction did not link to the lib with external function, but i have tried many different ways, and still cannot deal with it.
I hope someone can help me figure out how to mex it.
Thanks.
Zhicheng

Accepted Answer

James Tursa
James Tursa on 21 Jul 2014
I usually explicitly put the name of the lib file directly on the mex command. E.g., try something like this assuming the lib file is in the current directory with the c file:
mex -largeArrayDims -I'c:\Users\zzhang\API' -L'c:\Users\zzhang\API' GHXCmexAPI.c GHXDLL.lib
  3 Comments
James Tursa
James Tursa on 22 Jul 2014
Edited: James Tursa on 22 Jul 2014
Same error message or a different one? I would have expected that by explicitly including the GHXDLL.lib filename on the mex command line the linker would be able to find the function in the lib. Maybe it isn't being exported. Did you define the GHXDLL_EXPORTS macro anywhere in your code to force the export when you built the library?
Zhicheng Zhang
Zhicheng Zhang on 25 Jul 2014
Same error. But i have figured out the problem. The reason is the dll version. I used to compile it in x32. Since I upgrade my matlab from R2012b to R2014a, it requires x64 version instead of x32. So i rebuilt the dll under x64 platform, then it works. Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!