Erdbeer0815
Erdbeer0815

Reputation: 155

#include <mat.h> in Visual Studio Code does not work

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.

Upvotes: 0

Views: 1768

Answers (1)

user1196549
user1196549

Reputation:

#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.

Upvotes: 1

Related Questions