1

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!

Everv0id
  • 1,862
  • 3
  • 25
  • 47
  • What does the documentation tell you? You can start [here](http://www.mathworks.com/support/tech-notes/1600/1622_files/1622_R13.html) – Floris Oct 15 '13 at 17:13
  • Well it's obvious you're missing a link to a library somewhere. This might help you out: http://www.mathworks.com/support/solutions/en/data/1-30UM5H/index.html – JustinBlaber Oct 15 '13 at 17:55
  • You need to add the path to the lib (.so) file to LD_LIBRARY_PATH – Shai Oct 15 '13 at 18:09
  • Why not compile with the free Windows SDK? – chappjc Oct 16 '13 at 19:25
  • @chappjc Building with VC++ compiler gives same error. – Everv0id Oct 17 '13 at 21:21
  • @Everv0id Have you tried the procedure in [Compiling mex files with Visual Studio](http://www.orangeowlsolutions.com/archives/490)? – Vitality Oct 17 '13 at 21:24
  • @JackOLantern - He's not building a MEX file. – chappjc Oct 17 '13 at 21:25
  • Looking at the `msvc110engmatopts.bat` file, the only things that stand out as special are an environment variable `MW_TARGET_ARCH=win64`, the 3 MATLAB .libs: `libmx.lib libmat.lib libeng.lib`, the include and lib paths, and putting MATLAB's bin folder on PATH. Maybe try the env variable and restart VS2012. – chappjc Oct 17 '13 at 21:41
  • @chappjc Where is the file `msvc110engmatopts.bat` located? – Everv0id Oct 20 '13 at 10:50
  • I suspect on 32, 64 bit mix up problem. Check this: http://www.mathworks.com/support/solutions/en/data/1-FWTSV5/?product=ML&solution=1-FWTSV5 – durasm Oct 20 '13 at 19:37

1 Answers1

0

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.

durasm
  • 174
  • 3
  • 10
  • I executed your command: `/tmp/ccyvlTD9.o:main.cpp:(.text+0x32): undefined reference to '_engOpen' /tmp/ccyvlTD9.o:main.cpp:(.text+0x40): undefined reference to '_engClose' collect2: ld returned 1 exit status` I think it's clear that gcc doesn't see microsoft matlab engine libs. – Everv0id Oct 20 '13 at 10:40
  • Do you actually have .lib files in your .../extern/lib/win32/microsoft folder? Is your gcc 32-bit version? Which Matlab version are you running? – durasm Oct 20 '13 at 14:30
  • I use 64-bit version of Matlab (R2011b), so I changed your command to the following: `g++ main.cpp -o main.exe -I/cygdrive/c/Program\ Files/MATLAB/R2011b/extern/include -L/cygdrive/c/Program\ Files/MATLAB/R2011b/extern/lib/win64/microsoft -llibeng`. File `libeng.lib` is located in `/extern/lib/win64/microsoft/` – Everv0id Oct 20 '13 at 14:51
  • What type of binaries your g++ produces, 32 or 64 bit? – durasm Oct 20 '13 at 19:39
  • 32-bit gcc. So, do I need to change MATLAB libs to its 32-bit version? – Everv0id Oct 22 '13 at 08:19
  • 1
    One note: The libs you link are so called "import" libraries. At run time you need .dll(s) that correspond to those .lib files (libeng.dll in this case). If your .exe is 32-bit it can work only with 32 bit DLLs. You cannot have 32 bit exe load and run 64 bit DLLs (and vice versa). – durasm Oct 22 '13 at 18:39