Gateway function missing -->Matlab 2018a -->Mex - matlab

I am trying to access C++ code from Matlab R2018a but getting error
Invalid MEX-file 'C:\\C++ForMatlab\test.mexw64': Gateway function is
missing.
I did the similar thing on Windows 7 with VS2013 and Matlab 2016 and it all worked fine.
The function compiles successfully and dll is created as expected but when I try to run it I got error message.
OS --> Windows 10
Matlab --> Matlab R2018a
C++ -> VS 2017
Sample code written in VS2017
FileName --> test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World!\n");
}
In Matlab
I type following command
mex test.cpp
And Output I get is
Building with 'Microsoft Visual C++ 2017'.
MEX completed successfully.
But After that when I try to run by typing
Trial>> test
I get following error
Invalid MEX-file 'C:\C++ForMatlab\test.mexw64': Gateway function is
missing
.

Generate a file "ThisModule.def" and add following lines.(swap templateSFunc with your own file name)
LIBRARY "templateSFunc.mexw64"
EXPORTS
mexFunction
Then go to project properties in VS and add ThisModule.def to the linker/Input/Module Definition File option.

Related

mex file compiled without errors but not working in matlab

First I wanted to compile MatConvNet library for using in windows form this tutorial
(Compiling MatConvNet on Windows)
but I couldn't. Then I think it is better to compile a very simple file and after that going to compile the library.
I have Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
my program Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
I can compile Test.cpp in matlab with mex Test.cpp
And when I type test output is Hello! :)
I also can set the correct configuration according to tutorials below and compile it without errors.
1) http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm
2) http://www.orangeowlsolutions.com/archives/490
But when I run it in Matlab, nothing happen. There is no output and Matlab doesn't give me any error.
What is the problem?
Notice that:
in (1) second step is adding "mexversion.rc" from "matlab\extern\include" to the project But this file dose not exist in my computer so I couldn't do it.
In Visual Studio I needed to add tow headers in below for compiling the program.
include "stdafx.h"
include "maxrix.h"
so the Test.cpp in Visual Studio is:
#include "mex.h"
#include "stdafx.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
Pre-compiled header shenanigans
A problem with the Visual Studio version of the code is the pre-compiled header file stdafx.h causing the compiler to ignore any code above it (the mex.h include):
#include "mex.h"
#include "stdafx.h" // ANYTHING above here is IGNORED!
#include "matrix.h"
Move the stdafx.h include to the top or turn off PCH in projects settings and remove the include.
printf vs. mexPrintf
Before getting into MEX project setup, note that printf points to mexPrintf courtesy of mex.h:
#define printf mexPrintf
So, using printf is not an issue, but probably not good practice. The problem comes if you redefine printf after including mex.h or fail to get this define on account of the PCH header.
Regarding MEX in Visual Studio
I posted a more formal guide to setting up a Visual Studio projects for building MEX files as an answer to the more commonly-reference question on this topic, and I would also suggest here to use Visual Studio property sheets to get your project set up to build a MEX file. The details are in the referenced post, but you just need to:
Set the MATLAB_ROOT environment variable.
Create a new DLL project.
Under Property Manager (from View menu), right click on each project's build configuration and "Add Existing Property Sheet...", choosing the MATLABx64.props file from this GitHub repo.
printf only works in native C. You need to use mexPrintf. Therefore, your code should be this:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello! :)\n");
}
In general, printing to standard output in a MEX script doesn't appear in the MATLAB command prompt. If you want to display messages in MATLAB, you need to use mexPrintf instead of printf.
To be explicit, if you consult the mexPrintf documentation, a caveat can be seen towards the end:
In a C MEX-file, you must call mexPrintf instead of printf to display a string.
BTW, I recommend this awesome MEX tutorial here: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf. This is the tutorial that I used to get started with programming MEX wrappers in MATLAB. You'll also see that the first example is of the same "Hello World" caliber that you are trying to get running :)
Good luck!
How To Compile a C/CPP File and Create mex File for Using in Matlab
I found the solution by #rayryeng help and (How to build mex file directly in Visual Studio?) #jorre's post.
This is tested with Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World123! :)\n");
}
1.Create a new project in VS -> Visual C++ -> General -> Empty Project.
The name of your Project will be the name of mex file.
You can change it later.
2.Change the Debug to Release.
Right click on the project -> Properties -> Configuration properties -> General
Set Target Extension to .mexw64
Set Configuration Type to Dynamic Library (.dll)
Configureation poperties -> VC++ Directories:
5.Add $(MATLAB_ROOT)\extern\include; to Include Directories
Configuration properties -> Linker -> General:
Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories
Configuration properties -> Linker -> Input:
Add these things to Additional Dependencies
libmx.lib
libmex.lib
libmat.lib
Configuration properties -> Linker -> Command Line:
Add /export:mexFunction to Additional Options
Now you must set you platform to x64 otherwise you'll get error like"Error 1 error LNK2001: unresolved external symbol _mexPrintf".
9.Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Settings From Win32
Now you can compile your file and get mex file.
If you see these tutorials there are other things thar are not needed and may cause problems.
(http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm)
Create an empty Project, not a MFC Dll Project.
There is no need to *.def file.
There is no need to add "mexversion.rc" into your project.
There is no need to add "MATLAB_MEX_FILE" as a preprocessor definition.

Cannot link dll to mexfunction in Matlab cmex function

Does anyone have idea about how to link dll or lib in the Mex command?
I have a C code link a external function from GHXDLL.dll and GHXDLL.lib. I defined the external function in header that the same one i used to generate the dll and lib.
typedef struct GHX{
double output[28];
int val;
}GHX;
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
#endif
#ifdef GHXDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
MATHFUNCSDLL_API void GHXfunction(double *XIN, double *parameter, int mode, int hour, GHX *result);
#ifdef __cplusplus
}
#endif
I wrote a test code to call this function with linking to the lib and dll, and it works. Meanwhile, I wrote a simple test code for the Mex API, then I compile it from command line by
mex test.c,
and it works.
Then, I met problem. When I add the external function to the Mex API and compile in matlab with the following command, it fails.
mex -largeArrayDims -I'c:\Users\zzhang\API' -L'c:\Users\zzhang\API' -lGHXDLL GHXCmexAPI.c
The errors are:
Error using mex
Creating library GHXCmexAPI.lib and object GHXCmexAPI.exp
GHXCmexAPI.obj : error LNK2019: unresolved external symbol __imp_GHXfunction referenced in function mexFunction
GHXCmexAPI.mexw64 : fatal error LNK1120: 1 unresolved externals
I know this error means the mexfunction did not link to the lib with external function, but i have tried many different ways, and still cannot deal with it.
I hope someone can help me figure out how to mex it.
Thanks.
Zhicheng
List the DLL import library file by name rather than via -l. Also add -v and inspect the link.exe command line:
mex -v -largeArrayDims -I'c:\Users\zzhang\API' -L'c:\Users\zzhang\API' ...
GHXCmexAPI.c GHXDLL.lib
When inspecting the link command, also verify that there is a /LIBPATH: with the folder containing your DLL.

mex Error: command not found

I am trying to run a simple mex example in matlab. The .c program is
#include <math.h>
#include <matrix.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
printf("Hello World!\n");
}
When I try the command "mex helloWorld.c" I get the following error message:
/home/rory/com/machine: Command not found.
Warning: You are using gcc version "4.7.2-2)". The version
currently supported with MEX is "4.4.x".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
I understand the second error message. But I have no idea what the first means. Rory is not my name (I am on a work pc). What specific command is being sought after and where can I find the option to change to a more appropriate path?
Thanks,
Thomas
[update] - I have replaced printf with mexPrintf, as suggested. The error remains, but it seems to compile and run correctly. Running -setup produces the same error, but otherwise informs me of the options file currently in use as it normally does.
At the moment, it looks like I can ignore the error.
Thanks again.

how to debug MATLAB .mex32/.mex64 files with Visual Studio 2010

I have a question related to debugging .mex32/.mex64 files. Suppose now I have a file named test.cpp:
#include "mex.h"
#include <iostream>
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello Matlab, and this is a test program\n");
}
I can then compile and build test.mex64 with Visual Studio 2010.Then in matlab, I can write the following script to test the function:
clc;
test;
Now suppose I want to debug the test.mex64 function, what should I do? The have adopted the following procedure, but failed:
Toggle break point at the begging of the line mexPrintfwith VS2010.
With VS2010 from Debug->Attach to Process... select MATLAB.exe.
Run MATLAB script clc; test;
The error message I have obtained is as follows:
The breakpoint will not currently be hit. No symbols have been loaded for this document.
Did you build your mex file with debug option "-g"?
I have found the solution: when I created the .mexw64 function (test.mexw64 in our case), I copied it to the MATLAB work directory. In order to debug this function, it is important to copy test.pdb file to the MATLAB work directory as well. After doing that, I can debug.

unresolved external symbol - Error using Matlab API

I'm trying to read a .mat-file in C++ with MSVS 2008 but when building a simple program I get the following error:
1>ex3.obj : error LNK2019: unresolved external symbol _matClose referenced in function _main
1>ex3.obj : error LNK2019: unresolved external symbol _matOpen referenced in function _main
I've researched Google as well and it seems that the compiler can't link to the libraries needed for using this functions (matOpen and matClose). I never used an external library before but I tried everything I found in Google to add the Matlab libraries. I did the following:
TOOLS --> Options --> Projects and Solutions --> VC++ Directories --> Show directories for: include files --> then I added the path of the matlab include directory --> C:\Program Files\MATLAB\extern\include
I did the same with the library files: C:\Program Files\MATLAB\extern\lib\win64\microsoft
I also did that for the project:
Right click on the project --> Properties --> Configuration Properties --> C/C++ --> General --> Additional Include Directories --> and added "C:\Program Files\MATLAB\extern\include\win64"
Then I did the same at Linker --> General --> Additional Library Directories --> and added "C:\Program Files\MATLAB\extern\lib\win64\microsoft"
So I really don't know where the problem is. Here is the source code I'm trying to build:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <mat.h>
int main(int argc, char *argv[])
{
const char *file = "mozart_part1.mat";
MATFile *pmat;
pmat = matOpen(file, "r");
if(pmat == NULL)
{
std::cout << "Error: could not open MAT-file!";
return(1);
}
matClose(pmat);
}
Can you see or guess any mistakes I made
Take a look here.
Go through the steps.
What I think you've missed is step 7:
7.Locate the .lib files for the compiler you are using under matlabroot\extern\lib\win32\microsoft or matlabroot\extern\lib\win64\microsoft. Under Linker Input properties, add libmx.lib, libmex.lib, and libmat.lib as additional dependencies.
Edit:
Both Matlab and Visual C++ should be either 32bit or 64bit. There are two options:
Find these 3 lib files from another Matlab which is 32bit. Direct your linker there instead.
Make your Visual C++ 64bit. See here how it's done.
Solution:
What eventually worked was option 2, using this link with instructions.
I solved the Problem!
#Michael Litvin: you were right! I didn't know that you have to switch MSVS 2008 to x64 platform.
As the Matlab libraries are provided as x64 binaries you have to switch your MSVS compiler to x64 as well. I followed these steps to do that: http://software.intel.com/en-us/articles/configuring-microsoft-visual-studio-for-64-bit-applications/
Thanks for you help!