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

17 Ansichten (letzte 30 Tage)
agg gpo
agg gpo am 24 Sep. 2014
Kommentiert: Royi Avital am 2 Sep. 2018
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.

Antworten (1)

Geoff Hayes
Geoff Hayes am 25 Sep. 2014
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu C Shared Library Integration finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by