mex compilation not working?

조회 수: 5 (최근 30일)
cui,xingxing
cui,xingxing 2022년 8월 8일
댓글: cui,xingxing 2022년 8월 8일
I have a very simple C++ program "readBinFile.cpp" for reading the binary file bin, but mex compiles with an error.How do I fix it to compile correctly?(I have successfully paired matlab with mingw64 and verified the runtime environment)
The matlab call syntax is:
outImg = readBinFile(''myBinFile.bin");
readBinFile.cpp is following:
#include "mex.hpp"
#include "mexAdapter.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using matlab::mex::ArgumentList;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
// Pointer to MATLAB engine
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
// Factory to create MATLAB data arrays
ArrayFactory factory;
typedef struct {
uint8_t B, G, R, A;
uint32_t AlignmentDummy;
} FColorStruct;
typedef struct {
uint32_t InSizeX;
uint32_t InSizeY;
FColorStruct imgUreal[540 * 480];
} imgBinStruct;
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
size_t resolutionX = 540;
size_t resolutionY = 480;
// read my file
FILE* pFile;
long lSize;
imgBinStruct* buffer;
size_t result;
std::string in1 = inputs[0][0]; // bin file name
// matlab::data::TypedArrayRef<MATLABString> in = in1;
// std::string cmdString = std::string(in1);
pFile = fopen(in1.c_str(), "rb");
if (pFile == NULL) {
std::ostringstream stream;
stream << "Failed to open " + in1 << std::endl;
displayOnMATLAB(stream);
exit(1);
}
// obtain file size:
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
buffer = (imgBinStruct*)malloc(sizeof(imgBinStruct));
if (buffer == NULL) {
std::ostringstream stream;
stream << "Memory error " << std::endl;
displayOnMATLAB(stream);
exit(2);
}
// copy the file into the buffer:
result = fread(buffer, 1, lSize, pFile);
if (result != lSize) {
std::ostringstream stream;
stream << "Reading error " << std::endl;
displayOnMATLAB(stream);
exit(3);
}
// matlab mat
matlab::data::TypedArray<uint8_t> outImg = factory.createArray<uint8_t>({resolutionY, resolutionX, 3});
for (size_t i = 0; i < resolutionY; i++) {
for (size_t j = 0; j < resolutionX; j++) {
outImg[i][j][2] = (uint8_t)(buffer->imgUreal[i * resolutionX + j]).B;
outImg[i][j][1] = (uint8_t)(buffer->imgUreal[i * resolutionX + j]).G;
outImg[i][j][0] = (uint8_t)(buffer->imgUreal[i * resolutionX + j]).R;
}
}
outputs[0] = outImg;
// terminate
fclose(pFile);
free(buffer);
}
void displayOnMATLAB(const std::ostringstream& stream) {
matlabPtr->feval(u"fprintf", 0,
std::vector<Array>({factory.createScalar(stream.str())}));
}
void checkArguments(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
if (inputs.size() != 1) {
matlabPtr->feval(u"error",
0, std::vector<matlab::data::Array>({factory.createScalar("One inputs bin FileName required")}));
}
if (inputs[0].getType() != matlab::data::ArrayType::MATLAB_STRING) {
matlabPtr->feval(u"error",
0, std::vector<matlab::data::Array>({factory.createScalar("Input binFileName must a string scalar")}));
}
if (outputs.size() != 1) {
matlabPtr->feval(u"error", 0, std::vector<matlab::data::Array>({factory.createScalar("输出参数必须为1个")}));
}
}
};
I then compile the mex using the following command, (run matlab 2022a, windows10)
mex readBinFile.cpp
Error using mex
In file included from C:\Program Files\MATLAB\R2022a/extern/include/MatlabEngine.hpp:11,
from D:\matlab_files\mytest\readBinFile.cpp:6:
C:\Program Files\MATLAB\R2022a/extern/include/MatlabEngine/engine_util.hpp:22:20: error:
multiple definition of 'enum class matlab::engine::WorkspaceType'
enum class WorkspaceType {
^~~~~~~~~~~~~
In file included from C:\Program Files\MATLAB\R2022a/extern/include/mex.hpp:81,
from D:\matlab_files\mytest\readBinFile.cpp:4:
C:\Program Files\MATLAB\R2022a/extern/include/cppmex/mexMatlabEngine.hpp:25:20: note:
previous definition here
enum class WorkspaceType {
^~~~~~~~~~~~~
In file included from C:\Program Files\MATLAB\R2022a/extern/include/MatlabEngine.hpp:12,
from D:\matlab_files\mytest\readBinFile.cpp:6:
...
  댓글 수: 1
cui,xingxing
cui,xingxing 2022년 8월 8일
I have experimented and after removing the header file “MatlabEngine.hpp, I managed to get my .mexw64 file through mex compilation, I don't know why this happened.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by