简体   繁体   English

在 C++ 中读取 MATLAB 格式的数据文件的正确方法是什么

[英]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++.我正在尝试用 C++ 读取 MATLAB 格式的数据文件 (data.mat) 文件。 I referred to the following post " Link mat.h in a C++ file " and tried the given example code.我参考了以下帖子“ 在 C++ 文件中链接 mat.h ”并尝试了给定的示例代码。

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.为了解决这个问题,我将'mat.h', 'matrix.h' and 'tmwtypes.h'复制到代码目录中。 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.我正在使用 Mac OS Mojave。 Thank you.谢谢你。

I had never used MacOS on a PC.我从未在 PC 上使用过MacOS I even don't know if the OS called same name on every device type.我什至不知道OS是否在每种设备类型上都调用了相同的名称。 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.但是在运行时, ld (二进制可执行文件链接器/加载器。它可能与生成您的特定应用程序的链接器/加载器相同)在操作系统标准的位置搜索动态符号。

As I said, I'm not used to MacOS.正如我所说,我不习惯 MacOS。 I use Linux now.我现在使用Linux。 For example my Linux uses /usr/lib and /usr/local/lib path to search for dynamic libraries.例如我的Linux 使用/usr/lib/usr/local/lib路径来搜索动态库。 The way the ld program searches for the symbols can be configured in few ways. ld程序搜索符号的方式可以通过几种方式进行配置。

For example Linux has LD_LIBRARY_PATH and MacOS has a DYLD_LIBRARY_PATH envinronment variables.例如, LinuxLD_LIBRARY_PATH ,MacOS 有DYLD_LIBRARY_PATH环境变量。 See this question and answers: Is it OK to use DYLD_LIBRARY_PATH on Mac OS X?请参阅此问题和答案:是否可以在 Mac OS X 上使用 DYLD_LIBRARY_PATH? 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.链接器还有一个众所周知的-Wl,--rpath标志。 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.在您的终端中输入man ld并查看您的选项。

Update更新

Well, at my Linux , the loader is called ld.so and linker is ld .好吧,在我的Linux ,加载器名为ld.so ,链接器名为ld The update is just to answer the comment.更新只是为了回答评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM