C++调用matlab函数

在matlab编译过程中,显示无法打开libMatlabCppSharedLib.lib这个文件,编译器是设置好了的

img

img

img

由于问题描述不够详细,无法准确判断发生了什么问题。但是,根据参考资料和经验,可以提供以下解决方案:

  1. 检查路径是否正确:确保libMatlabCppSharedLib.lib文件位于正确的文件夹中,工程可以正确引用。

  2. 检查版本是否兼容:检查MATLAB版本和编译器版本是否兼容,以及使用的C++库是否能在此版本上运行。

  3. 检查依赖关系:检查项目依赖项是否正确设置,包括MATLAB相关的依赖库、编译器、操作系统等。

  4. 查找并添加必要库文件:根据提示信息查找需要的库文件,将其添加到工程中。

下面是部分C++的代码展示:

// 1. 检查路径是否正确

include

include

include

include

include "include/mat.h"

pragma comment(lib, "libmat.lib")

pragma comment(lib, "libmx.lib")

pragma comment(lib, "libMatlabCppSharedLib.lib")

int main() { MATFile matFile; mxArray dataArray = NULL; std::string filePath = "D:\data.mat"; matFile = matOpen(filePath.c_str(), "r"); if (matFile == NULL) std::cout << "Error opening mat file!" << std::endl; else { dataArray = matGetNextVariable(matFile, NULL); // other code } // other code }

// 2. 检查版本是否兼容

include "mex.h"

include "engine.h"

pragma comment(lib, "libeng.lib")

pragma comment(lib, "libmx.lib")

pragma comment(lib, "libMatlabCppSharedLib.lib")

void mexFunction(int nlhs, mxArray plhs[], int nrhs, const mxArray prhs[]) { Engine *ep = engOpen(NULL); if (ep == NULL) mexErrMsgTxt("Error connecting to MATLAB Engine API.");

mxArray *ptrMx = mxCreateDoubleScalar(123.0);
engPutVariable(ep, "inputVar", ptrMx);
// other code

mxDestroyArray(ptrMx);
engClose(ep);

}

// 3. 检查依赖关系

include "mex.h"

include

include

pragma comment(lib, "libmat.lib")

pragma comment(lib, "libmx.lib")

pragma comment(lib, "libMatlabCppSharedLib.lib")

void mexFunction(int nlhs, mxArray plhs[], int nrhs, const mxArray prhs[]) { // checks dependency std::vector lLibs; char pPath[1024]; GetModuleFileName(NULL, pPath, 1024); char pLastSlash = strrchr(pPath, '\'); if (pLastSlash) (pLastSlash + 1) = '\0';

const char* pMatLabVersion = mexGetVariable("global", "MatLabVersion");
if (pMatLabVersion && strlen(pMatLabVersion) > 0)
{
    // Check MATLAB runtime
    lLibs.push_back("libmx.dll");
    lLibs.push_back("libmat.dll");
    lLibs.push_back("libmex.dll");
    lLibs.push_back("mclcommain.dll");
    lLibs.push_back("mclmcrrt7_15.dll");
    std::for_each(lLibs.begin(), lLibs.end(), [](char * it) { LoadLibraryA(it); });
    // other code
}

}

// 4. 查找并添加必要库文件

include

include

include

pragma comment(lib, "libmx.lib")

pragma comment(lib, "libmat.lib")

pragma comment(lib, "libmatcppsharedlib.lib")

typedef unsigned char uchar;

void loadMatlabLibraries() {

std::cout
    << __FUNCTION__ << "::Checking for Matlab libraries load: " << std::endl;

HMODULE libHandle;
HWND consoleHandle = GetConsoleWindow();

// MATLAB engine library - connection to matlab
std::string matlabRoot(getenv("MATLABROOT"));

std::cout << "MATLABROOT: " << matlabRoot << std::endl;

std::string mexLibPath = matlabRoot + "\\extern\\lib\\win64\\microsoft\\libMatlabCppSharedLib.lib";
std::cout << "loading mex library: " << mexLibPath << std::endl;
libHandle = LoadLibraryA(mexLibPath.c_str());
if (libHandle == NULL)
{
    std::cout << "Error loading library!" << std::endl;
}
else
{
    std::cout << "Library loaded successfully!" << std::endl;
    FreeLibrary(libHandle);
}

}

int main() { loadMatlabLibraries(); // other code system("pause"); return 0; }

参考资料:

1.https://blog.csdn.net/yizeasd/article/details/63297103

2.https://stackoverflow.com/questions/17255940/c-matlab-compiler-libraries-not-linked

3.https://www.mathworks.com/matlabcentral/answers/165992-linker-errors-unresolved-external-symbols?s_tid=mlc_ans_email_view#answer_160777

4.https://www.mathworks.com/help/matlab/matlab_external/troubleshooting-issues-with-matlab-engine-api-for-c.html.