Fatal error LNK1168: cannot open filename.mexw64 for writing - c++

I am writing a c++/CUDA code with Visual Studio 2015 to generate a mex file to integrate with MATLAB.
When I run the mex file through the MATLAB console and then try to compile it again on VS I get this error:
LINK : fatal error LNK1168: cannot open filename.mexw64 for writing
Closing the MATLAB and opening the programme again solves the problem.
Does anyone know any solution which not involves closing the MATLAB?

MEX-files are DLLs (on Windows, shared objects on other systems). When first run, MATLAB links to them, but doesn't unlink unless explicitly told to (or quitting MATLAB of course).
Typing
clear mex
in MATLAB will unlink all MEX-files. See the relevant documentation.
But note that your MEX-file can call mxLock, which will cause it to be locked in memory and then it will not be cleared by clear mex. If you use this function in your MEX-file, you need to include a facility to have your MEX-file call mxUnlock. I usually add a syntax such as mymexfile --unlock: the MEX-file checks to see if it is called with one argument, and that argument is a string, and the string matches "--unlock", then it calls mxUnlock.

Related

How can I compile a mex function in Windows with Eclipse (4.20.0 version) and Matlab (R2021a)

I'm trying to compile a c++ mex function that I created according to the specifications given by mathworks here (Create a C++ MEX Source File). However, after following the steps of the answer in this thread to link Eclipse 4.4.2 and Matlab R2015a in a linux enviroment (old but the only one I've found about it), I get the following error:
undefined reference to `get_function_ptr' mpi_pevd_mex_f line 79, external location: C:\Program Files\MATLAB\R2021a\extern\include\MatlabDataArray\detail\ExceptionHelpers.hpp C/C++ Problem
I don't know what else is missing. I've been trying to find a solution, but nothing useful so far.
Thank you for your help.
I have found the problem. Apparently, I needed to add additional libraries to those detailed in most of the posts related to MEX files, i.e. libmex/ libmat/libmx.
Making use of the verbose mode in Matlab (mex -v MexFunction.cpp), I realized that these libraries were missing: libmwlapack, m, libmwblas, libMatlabEngine, libMatlabDataArray. After adding them, the error disappeared.

LabVIEW call library node error 1097 for c++ dll

I have a c++ library that I'm calling via the standard call library node in LabVIEW. I'm using 2017 VS to edit the c source files and 2020 32 bit version of LabVIEW. Consequently I've made sure to build new versions of my library in x86. I have not been able to nail down why I keep getting a 1097 error after the node call (I've included pictures of the function prototype that I'm attempting to call in the c header file, the node configuration interface, as well as a picture of the block diagram).
Additionally, LabVIEW reported these errors in the log when I restarted my development system:
DWarn 0x0E697B77: Caught exception in ExtCode call!
DWarn 0x50CBD7C1: Got corruption with error 1097 calling library Multilane_ML4039_Interface.dll function GetEyeDimensions
I've read that with this combination of errors, LabVIEW is catching an exception thrown by the .dll and this will prompt the library node to generator error 1097. This is often because of improperly configured parameters but as far as I can tell, my configuration is ok.
Any help is much appreciated.
Function prototype in .h file
Library configure node in LabVIEW
Block diagram containing library node
Got it resolved. My tired eyes didn't see I had one of my parameters in the prototype entered as a pointer instead of a value. Check those parameters people!

"No functions found" warning importing external dll file

Good afternoon,
I'm trying to work with some C++ dll files with Matlab, and I'm trying to implement a simple test case to understand the procedure. The dll file I'm using is copied verbatim from here:
https://msdn.microsoft.com/en-us/library/ms235636.aspx
I only implement up to step 5, since my hope is to call the dll file through Matlab. After completing step 5, I copy MathLibrary.h and MathLibrary.dll to the directory I'm using for my Matlab code, and then run
[notfound,warnings]=loadlibrary('MathLibrary.dll','MathLibrary.h');
Upon running this I get the warning
>Warning: No functions found in library.
>
>In C:\Program Files\MATLAB\R2014b\toolbox\matlab\general\loadlibrary.p>loadlibrary at 431
The cell array notfound is empty and warnings is an array with warnings = MathLibrary.h
If I try using one of the functions from the dll, I execute the following code:
calllib('MathLibrary','Add',5,3)
which throws the following error
>Error using calllib
>
>Method was not found.
I've tried Googling solutions to similar problems, but have not found solutions where I've looked (at least ones I've understood). My C++ is weak, which may be hindering my understanding of the problem and solution. I'm hoping to incorporate dll files from a much larger project soon, so understanding this would be a great help. Thanks so much!

Error on using Mex with OpenCVLibrary in Matlab

I am using a program that is written with OpenCV on Matlab. The program has been compiled and a .mexw64 file was generated.
I have added my OpenCV library to Matlab using mexopts.bat and the compile was successful.
When I call the function of the program, an error is generated:
Invalid MEX-file 'test.mexw64': test.mexw64 is not a valid Win32 application.
Make sure that the directory containing your mexw64 file is included in the MATLAB path.

IntelliSense error identifier "emlrtStack" is undefined

I am converting a MATLAB written function into C by "Matlab coder". After I get the converted files , the converted function always have first input argument as const emlrtStack *sp. Now when I am trying to test it on VC++ 2013, IntelliSense is giving mentioned above error.
I manually tried to locate this identifier in emlrt.h file but no such thing is present there. I tried to convert a simple multiply function with two input arguments[like, c=mul(a,b)] but still the converted function has this extra argument inside the function in addition to a and b.
(which means this argument is not function specific).
If someone has a solution to this or have experienced a problem like this, please share or help.
Moreover If someone know how to simply test these converted functions, it would be a much appreciated additional help .
It is likely that the code that was generated for a MEX function rather than a standalone target. MEX functions are binaries written C, C++ or Fortran that can be called like a normal MATLAB function. Generating code to produce a MEX function allows two things. First, you can test your generated code in MATLAB because you can call the MEX function from MATLAB like any other function. Look for a file named mul_mex.mex* after you do code generation and try to call it: mul_mex(1,2). The other use for generating a MEX function is that it can often be faster than the MATLAB code from which it was generated. MEX functions are only used in the context of MATLAB.
The parameter emlrtStack* that you saw appears in MEX generated code to aid in runtime error reporting. It is not present in standalone code that is designed to be run outside of MATLAB.
If you want to use the generated code in Visual Studio, or outside of MATLAB you should choose one of the standalone targets, LIB, DLL, or EXE. This page shows how to change the output type. To summarize, if using the command line you could say:
cfg = coder.config('lib'); %or 'dll' or 'exe'
codegen mul -config cfg -args {1,2}
If using the project interface, you click on the Build tab and choose static library or shared library in the "Output type" dropdown menu.
I would recommend reading this example that demonstrates how to use a generated DLL in Visual Studio.