问题
I'm trying to use Matlab library in C++ program:
#include <cstdlib>
#include <cstdio>
#include <string.h>
#include "engine.h"
using namespace std;
int main(int argc, char** argv) {
Engine* mweng = engOpen("");
engEvalString(mweng, "n = func(5)");
printf ("%d",engGetVariable(mweng, "n"));
engClose(mweng);
return 0;
}
I compile my project with g++ with included MATLABROOT\extern\include
directory and have a following error:
build/Debug/Cygwin-Windows/main.o: In function `main':
/cygdrive/d/Projects/Task1/main.cpp:10: undefined reference to `_engOpen'
/cygdrive/d/Projects/Task1/main.cpp:11: undefined reference to `_engEvalString'
/cygdrive/d/Projects/Task1/main.cpp:12: undefined reference to `_engGetVariable'
/cygdrive/d/Projects/Task1/main.cpp:14: undefined reference to `_engClose'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/task1.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
I suppose I have problems with linking some Matlab libs.
UPDATE:
I have same problem with VS2012 and windows compiler.
Directory extern\include
included to Additional include directories
, same as extern\lib\win64\microsoft
added to linker Additional library dependencies
.
According to @jucestain comment, the libeng.lib
file added to Additional dependencies
in linker options.
UPDATE 2:
We detected a compatibility problem: 32-bit gcc doesn't work with 64-bit Matlab engine. So, I merged /extern
folder from 32-bit Matlab with my /extern
folder, changed included linker library to /extern/lib/win32/lcc
, and my code has compiled successfully. Thanks to @aircooled!
回答1:
This is the line I use on my cygwin prompt to get main.exe:
$ g++ main.cpp -o main.exe -I/cygdrive/c/Program\ Files/MATLAB/R2009b/extern/include -L/cygdrive/c/Program\ Files/MATLAB/R2009b/extern/lib/win32/microsoft -llibeng
Note that order of giving sources and libs for gcc
is important - first sources, than libs.
来源:https://stackoverflow.com/questions/19387054/error-linking-matlab-with-c