0

I want to use some matlab code in c++. For example I want to read matlab files directly in my c++ IDE. There are some pretty cool c++ packages that allow you to handle the most problems I face.

But I fail to use these packages. It seems like I cannot #include the packages correctly. The error looks always something like "undefined reference to `matOpen'".

I use Visual studio Code and mingw as c++ compiler on Windows.

  1. I tried different notations:
#include < mat.h>

#include "mat.h"

and also, because I use c++, #include < cmath> which does not work but gives a different error:

"'matOpen' was not declared in this scope".

  1. I tried compiling in cmd

I also tried to compile the files in the command prompt. I read here that there might be linkage problems and tried to "add the -lm flag to the gcc compiler command in order to use math functions in your C code" without success.

I am not an expert with c++ nor cmd and it might be possible that I made some mistakes with the correct compile command. The help from above uses ubuntu and c and not windows and c++. Are there any differences I have to pay attention to in order compile successfully?

  1. Different paths

I also tried to relocate the file mat.h to the different locations like the path of the project or the compiler path or write the full path to mat.h in the include statement.

A similar (Link mat.h in a C++ file) question is for ubuntu and did not help me either.

My code is pretty simple:

#include <iostream>
#include <mat.h>
#include <stdio.h>

using namespace std;

int main()
{
    MATFile *pmat;
    pmat = matOpen("test.mat","r");
    return 0;
}

The error message I get:

cd "c:\projects\test\" ; if ($?) { g++ test.cpp -o test } ; if ($?) { .\test }
C:\user_path\AppData\Local\Temp\ccKUxfwi.o:test.cpp:(.text+0x1c): undefined reference to `matOpen'
collect2.exe: error: ld returned 1 exit status

I would appreciate any help. I want to use some more packages but it is probably the same solution for all.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Erdbeer0815
  • 155
  • 8
  • 2
    The final error means you are missing a link to some library. `g++ test.cpp -o test` does not specify a library so its understandable. – drescherjm Oct 21 '19 at 13:44
  • 1
    https://uk.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-c-1.html – OrenIshShalom Oct 21 '19 at 13:46
  • See also https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Max Langhof Oct 21 '19 at 14:01
  • I believe this: [https://stackoverflow.com/questions/44564442/link-mat-h-in-a-c-file](https://stackoverflow.com/questions/44564442/link-mat-h-in-a-c-file) should help as it tells you what library is needed. `-lmat` you will have to update the paths to the library from that example to match where you have installed your matlib libraries. So `-L/usr/local/MATLAB/R2012a/bin/glnxa64` will need to be changed – drescherjm Oct 21 '19 at 14:14
  • You need to use the `mex` script to build these programs: https://www.mathworks.com/help/matlab/matlab_external/building-on-windows-operating-systems.html – Cris Luengo Oct 21 '19 at 14:59
  • @drescherjm how do I specify a library? I tried g++ -o test test.cpp -I\Path -L\Path -L\Path Did not work but there might be a problem with the paths. I'll try to figure it out. – Erdbeer0815 Oct 21 '19 at 15:05
  • @OrenIshShalom nice article I have already read but I have the same problem with #include "MatlabEngine.hpp" #include "MatlabDataArray.hpp" – Erdbeer0815 Oct 21 '19 at 15:12
  • The include errors mean you need to set the include path. – drescherjm Oct 21 '19 at 16:12
  • `-lmat` specifies the library. You should spend some time learning how to use an external library with `gcc` since this appears to be your main issue. – drescherjm Oct 21 '19 at 16:14
  • From the linked example: `-I/usr/local/MATLAB/R2012a/extern/include` sets the include path. Again on a modern system your path will be different. I assume you are not using a matlab version from 2012 on a linux system. – drescherjm Oct 21 '19 at 16:24
  • `-I\Path` I am not sure about using \ versus / you may need to quote if you use \ instead of the expected / – drescherjm Oct 21 '19 at 16:28
  • @drescherjm thanks for your help. Unfortunately I did not manage to make it work. I tried to teach myself how to use g++ command correctly. Tried all combinations with -I,-L and -l but it did not work neither did it work if I set the include path and library directories in VSCode or Visual Studio manually. Also checked for enviromental variables. - if I use -lmat at the end it gives me an error "lmat not found". lmat refers to libmat.lib is that correct? Tried to specify libmat.lib in the command but also no success. – Erdbeer0815 Oct 23 '19 at 12:59
  • When I use g++ -o test -c test.cpp , -c tries to link and to compile it gives no error message but does not create and executable file. For a simple hello world programm -c works just fine but when I try to include some header like I cannot execute the output file – Erdbeer0815 Oct 23 '19 at 13:01

1 Answers1

1

#include <mat.h> is correct as it avoids the "not declared" error.

The "undefined reference" is a link-time error, it has nothing to do with the #include statement (the message is emitted by ld, not gcc).

Check that you added the (precompiled) library in your project.