Problem with C++ DLL in Matlab: The function was not found in the library

Hi there,
I'm trying to load a C++ DLL in Matlab. The compiler I'm using is Visual studio and I'm using Matlab 2012b. Everytime I'm trying to call a function in the library using "calllib" I get the following error:
"Error using calllib Method was not found".
The CPP file I'm using is just a simple add function as follows:
#include "stdafx.h"
#include "MyProject.h"
int MyProejct(int a,int b) { return(a+b); }
The header file I'm using is defined as follows:
#ifdef MYPROJECT_DLL_EXPORTS
#define MYPROJECT_DLL_API extern "C" _declspec(dllexport)
#else
#define MYPROJECT_DLL_API extern "C" _declspec(dllimport) #endif
MYPROJECT_DLL_API int MyProject(int,int);
I also added “MYPROJECT_DLL_EXPORTS” to the list of precompiler defines in the project properties tab.
The loadlibrary is successful when i use the following command: loadlibrary('MyProject.dll','MyProject.h')
But as I use the calllib as follows I get the "method was not found" error:
calllib('MyProject','MyProject', 5,6).
Any help would be appreciated. I researched a lot but I can't find the issue.
Thanks.

Answers (1)

Golnaz - while the library MyProject.dll may load successfully, are you sure there are no warnings being raised when you do load it? Try running the following when you load the library
[notfound,warnings] = loadlibrary('MyProject.dll','MyProject.h')
What does the warnings variable show? Is it an empty string or not?
I created a very simple dylib (for my Mac) using the code that you provided above, and missed some key lines. So while I could load the library successfully (using R2014a), I observed the following warning message as the output from the loadlibrary call
Warning: The function 'MyProject' was not found in the library
> In loadlibrary at 406
Several changes later, I got it to work fine (see attached) and could run calllib as
calllib('MyProject','MyProject',4,38)
ans =
42
What I did notice, from your code in the above question, is your definition for the function in the cpp file
#include "stdafx.h"
#include "MyProject.h"
int MyProejct(int a,int b) { return(a+b); }
Note how the method/function name is misspelt - MyProejct rather than MyProject. Could this be the problem?
I used gcc to compile the library as
gcc -g -O -c MyProject.cpp
gcc -dynamiclib MyProject.o -o MyProject.dylib
Then, in MATLAB, from the directory where the cpp, h, and dylib existed, I ran
hfile = fullfile(pwd,'MyProject.h');
loadlibrary('MyProject',hfile);
calllib('MyProject','MyProject',4,38);
unload library MyProject;

5 Comments

Dear Geoff,
Thank you for your response.
I actually modified the cpp code (changed MyProejct to MyProject). I also changed the header file to be the same as what you provided. I still get the same error when I use calllib.
In the load library I get the same warning as you expected:
Warning: The function 'MyProject' was not found in the library > In loadlibrary at 403
I'm looking to hear back from you.
Golnaz
Hi Golnaz - when I attached the MyProject.h file I wondered if it was a good idea or not. Since it worked for gcc on a Mac there was no guarantee that it would work elsewhere.
I changed the header to the following (see attached as well)
#ifndef MYMETHOD_H
#define MYMETHOD_H
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
#endif
__declspec( dllimport ) int MyProject(int a,int b);
#ifdef __cplusplus
}
#endif
#endif
I created the MyProject.dll with this file and the original MyProject.cpp in MS Visual C++ 2005 Express. Then did the following in MATLAB 2014a (as before)
hfile = fullfile(pwd,'MyProject.h');
loadlibrary('MyProject',hfile);
calllib('MyProject','MyProject',4,38);
unloadlibrary MyProject;
The library load was successful (no warnings) and the calllib returned the correct result of 42.
The above changes create an h-file that is very similar to yours. Looking more closely at what you pasted, I think that all you were missing was an underscore. In the updated header, there are two underscores before declspec as __declspec, whereas in your code, there is just the one underscore, _declspec.
You can try the above, or with your code again (with the corrected function name in the cpp file) and just add the extra underscore.
Thank you so much Geoff, it solved the problem. I'm getting the same error (method was not found) for the other functions, so now I'm sure that I've made a mistake in creating the header file.
Thank you so much again, it was a great help.

Sign in to comment.

Asked:

on 24 Sep 2014

Commented:

on 2 Sep 2018

Community Treasure Hunt

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

Start Hunting!