Q: What is the right way of reading MATLAB formatted data file in C++ I am trying to read a MATLAB formatted data file (data.mat) file in C++. I referred to the following post "Link mat.h in a C++ file" and tried the given example code.
using namespace std;
int main() {
MATFile *pmat;
pmat = matOpen("data.mat","r");
return 0;
}

From the answer to the post, I ran the example code using the following command,
Command:
g++ main.cpp -o out -I/Applications/MATLAB_R2019a.app/extern/include -L/Applications/MATLAB_R2019a.app/extern/lib -lmat

Output:
ld: library not found for -lmat
clang: error: linker command failed with exit code 1 (use -v to see invocation)

In order to fix this, I copied 'mat.h', 'matrix.h' and 'tmwtypes.h' to the code directory. But, now when I am running the code, I am getting a different error and I am not able to find any solution for this. 
Command:
g++ main.cpp -o out

Output:
Undefined symbols for architecture x86_64:
  "_matOpen_800", referenced from:
      _main in main-d0f06c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can someone please help me with this.
I am using Mac OS Mojave. Thank you.

A: I had never used MacOS on a PC. I even don't know if the OS called same name on every device type. What you see here, is a runtime linkage problem. 
You pass flags to the linker that tell the linker where to find symbols and what symbols there will be. And then the binary is built. But at runtime, the ld(Binary executable file linker/loader. It may be same linker/loader that had produced your particular application) will search for dynamic symbols at the places that are standard to the OS.
As I said, I'm not used to MacOS. I use Linux now. For example my Linux uses /usr/lib and /usr/local/lib path to search for dynamic libraries. The way the ld program searches for the symbols can be configured in few ways.
For example Linux has LD_LIBRARY_PATH and MacOS has a DYLD_LIBRARY_PATH envinronment variables. See this question and answers: Is it OK to use DYLD_LIBRARY_PATH on Mac OS X? And, what's the dynamic library search algorithm with it?. BTW, I do agree with many posts there that editing an envinronment variable is a best way to solve this situation.
There are also a well known -Wl,--rpath flag for the linker. It explicitly tells to add a search path to the application file. I think this method fits more if you a developer and are experimenting with your code before installing it to a proper location.
My answer to this question will be such, that, you should read the manuals. Type man ld in your terminal and see what your options are.
Update
Well, at my Linux, the loader is called ld.so and linker is ld. The update is just to answer the comment.
