gpt4 book ai didi

c++ - 在 Ubuntu 上从 C++ 调用 Matlab

转载 作者:行者123 更新时间:2023-11-28 04:16:58 27 4
gpt4 key购买 nike

我正在尝试在 Ubuntu 18.04 上的 Eclipse IDE 中从 C++(对于 NS-3)调用 Matlab 函数。我使用 Matlab Library Compiler 将 .m 文件编译成 C++ 共享库。如何正确调用函数?

相应的MATLAB Runtime已经安装配置好。起初它找不到“mclmcrrt.h”,我复制了/usr/local/MATLAB/R2018b/extern/include 文件夹并将库头更改为包含“./include/mclmcrrt.h”。这样IDE就可以正确找到头文件了。

用于测试库的 C++ 脚本如下:

/* C = A+B */
#include <iostream>
#include "libtestAdd.h"

using namespace std;

int main(){
double A[] = {1};
double B[] = {2};
int nargout = 1;

mwArray input_A (1,1,mxDOUBLE_CLASS,mxREAL);
input_A.SetData(M,1);
mwArray input_B (1,1,mxDOUBLE_CLASS,mxREAL);
input_B.SetData(K,1);
mwArray C (1,1,mxDOUBLE_CLASS,mxREAL);

if (!mclInitializeApplication(NULL,0)){
std::cerr << "Could not initialize the application properly."
<< std::endl;
return -1;
}
libtestAddInitialize();
testAdd(nargout, C, input_A, input_B);
libtestAddTerminate();

return 1;
}

Matlab函数为C=A+B。

.m 文件已编译成 C++ 共享库(“libtestAdd.so”和“libtestAdd.h”)。 C++ 脚本尝试从库中调用“testAdd”函数。

Matlab Runtime 已正确安装。/etc/profile 已更改为将以下路径导出到 LD_LIBRARY_PATH 环境变量:

/usr/local/MATLAB/MATLAB_Runtime/v95/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v95/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v95/sys/os/glnxa64

报错信息如下:

~/Desktop/libtestAdd/for_testing$ cd "/home/csrl/Desktop/libtestAdd/for_testing/" && g++ main.cpp -o main && "/home/csrl/Desktop/libtestAdd/for_testing/"main
/tmp/ccyFn7iq.o: In function `main':
main.cpp:(.text+0xde): undefined reference to `libtestAddInitialize'
main.cpp:(.text+0xf4): undefined reference to `testAdd(int, mwArray&, mwArray const&, mwArray const&)'
main.cpp:(.text+0xf9): undefined reference to `libtestAddTerminate'
/tmp/ccyFn7iq.o: In function `mwException::mwException()':
main.cpp:(.text._ZN11mwExceptionC2Ev[_ZN11mwExceptionC5Ev]+0x46): undefined reference to `mclcppCreateError_proxy'
/tmp/ccyFn7iq.o: In function `mwException::mwException(char const*)':
main.cpp:(.text._ZN11mwExceptionC2EPKc[_ZN11mwExceptionC5EPKc]+0x5a): undefined reference to `mclcppCreateError_proxy'
/tmp/ccyFn7iq.o: In function `mwException::mwException(error_info*, bool)':
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x61): undefined reference to `ref_count_obj_addref_proxy'
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x7a): undefined reference to `mclcppCreateError_proxy'
/tmp/ccyFn7iq.o: In function `mwException::~mwException()':
main.cpp:(.text._ZN11mwExceptionD2Ev[_ZN11mwExceptionD5Ev]+0x36): undefined reference to `ref_count_obj_release_proxy'
/tmp/ccyFn7iq.o: In function `mwException::what() const':
main.cpp:(.text._ZNK11mwException4whatEv[_ZNK11mwException4whatEv]+0x25): undefined reference to `error_info_get_message_proxy'
/tmp/ccyFn7iq.o: In function `mwException::raise_error()':
main.cpp:(.text._ZN11mwException11raise_errorEv[_ZN11mwException11raise_errorEv]+0x2a): undefined reference to `mclcppGetLastError_proxy'
/tmp/ccyFn7iq.o: In function `mwArray::mwArray(unsigned long, unsigned long, mxClassID, mxComplexity)':
main.cpp:(.text._ZN7mwArrayC2Emm9mxClassID12mxComplexity[_ZN7mwArrayC5Emm9mxClassID12mxComplexity]+0x52): undefined reference to `mclGetMatrix_proxy'
/tmp/ccyFn7iq.o: In function `mwArray::~mwArray()':
main.cpp:(.text._ZN7mwArrayD2Ev[_ZN7mwArrayD5Ev]+0x26): undefined reference to `ref_count_obj_release_proxy'
/tmp/ccyFn7iq.o: In function `mwArray::SetData(double*, unsigned long)':
main.cpp:(.text._ZN7mwArray7SetDataEPdm[_ZN7mwArray7SetDataEPdm]+0x2b): undefined reference to `array_ref_set_numeric_mxDouble_proxy'
collect2: error: ld returned 1 exit status

最佳答案

您需要链接.so 文件。将它添加到编译命令的末尾。

关于c++ - 在 Ubuntu 上从 C++ 调用 Matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56374991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com