-3

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.

user14063792468
  • 839
  • 12
  • 28
Raghav
  • 87
  • 2
  • 13
  • You should add a proper tags to your question. The issue is more of `linker` related. Adding an OS tag will be great. – user14063792468 Mar 15 '20 at 09:07
  • Well... I did not give you a down-vote. To confirm that you may see my downvote as of now. The SO user can not downvote more then twice. So the last one was mine :P – user14063792468 Mar 15 '20 at 09:45
  • Do you have a `/Applications/MATLAB_R2019a.app/extern/lib/libmat.dylib`? This is what you are telling your build system to link to. The linker error says this file doesn’t exist. In your second try you didn’t link to the library, and therefore the linker couldn’t find the symbols defined in the library. – Cris Luengo Mar 15 '20 at 14:42
  • No, I only have ```maci64``` present in the ```/Applications/MATLAB_R2019a.app/extern/lib/```. I followed this post (https://stackoverflow.com/questions/44564442/link-mat-h-in-a-c-file) but then I found that libmat is not present in my system so I copied the header files to my working directory. But, since I am including the header file which is present in the same directory, shouldn't it link while compiling? @CrisLuengo – Raghav Mar 15 '20 at 17:04
  • If you don’t have the library, you can’t link to the library. Did you look inside the `maci64` directory? I think you might find the library in there. If so, all you need to do is adjust the `-L` option in your compile command to point to the directory that contains the `libmat` library. – Cris Luengo Mar 15 '20 at 18:56
  • @CrisLuengo I already looked for `libmat`. I also did search through the entire MATLAB_R2019a.app directory, but `libmat` is not present there. I will try to download externally and try again. – Raghav Mar 15 '20 at 21:05
  • You *should* have this library with a normal MATLAB installation. It might be in the `bin` directory too. It’s location is different on every platform, but it is always there. On MacOS it will have a `.dylib` extension. – Cris Luengo Mar 15 '20 at 21:40
  • That's one strange thing I'm facing. I searched the entire directory for libmat.dylib but it's not present. I think I should reinstall MATLAB and check. – Raghav Mar 15 '20 at 22:32

1 Answers1

-2

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.

user14063792468
  • 839
  • 12
  • 28