How to build .m files into .mex files correctly

Hi All,
I am trying to build all my M files into MEX files so that I can give them to other users to use without disclosing the source code. I used the following steps to create (suppose I have a function blank saved in blank.m, code pasted down at the bottom):
Platform: Windows 7, with Matlab 2010b, visual studio 2010
1. convert M into C++ header and Library by:
mcc -W cpplib:blank -T link:lib blank.m
2. Use the generated blank.h and blank.lib, I create a blankm.cpp which looks like
void mexFunction(int nlhs,
mxArray* plhs[],
int nrhs,
mxArray* prhs[])
{
// validate input argument number
if (nrhs < 1)
{
mexErrMsgTxt("At least the file name is required.");
}
else if (nrhs > 1)
{
mexErrMsgTxt("Too many input arguments.");
}
// validate output argument number
if (nlhs > 1)
{
mexErrMsgTxt("Too many output arguments.");
}
mlxBlank(nlhs,
plhs,
nrhs,
prhs);
}
3. Then, I compiled it using matlab:
mex blankm.cpp -L blank.lib
Or compile it in Visual Studio 2010. All of the above steps went well with no problem. So far I have generated blankm.mexw64 successfully. If I input the following script in Matlab, it comes out with nothing
blandm(' a ')
However, if I input like
a = blankm(' a ');
I'll get an error
??? One or more output arguments not assigned during call to "blankm".
Any idea on this? I did something wrong. Or, the whole path is not correct? Thank you in advance. function str2 = blank(str)
if ischar(str)
str2 = fliplr(deblank(fliplr(deblank(str))));
else
error(['input is not a string']);
end
end

 Accepted Answer

Shared libraries generating using MATLAB Compiler are meant for deploying on a machine that does not have MATLAB installed - this is not needed in your case since you only want code-protection.
Why not simply pcode your MATLAB-files? If you absolutely want to create a MEX-file however, the better option might be to use MATLAB Coder which will generate standalone C code from your MATLAB functions where possible. If some of your MATLAB code is not supported for code generation, they will be converted into mexCallMATLAB calls.

More Answers (2)

The content of generated blank.h is as below:
//
// MATLAB Compiler: 4.14 (R2010b)
// Date: Wed Jun 06 16:53:38 2012
// Arguments: "-B" "macro_default" "-W" "cpplib:blank" "-T" "link:lib"
// "blank.m"
//
#ifndef __blank_h
#define __blank_h 1
#if defined(__cplusplus) && !defined(mclmcrrt_h) && defined(__linux__)
# pragma implementation "mclmcrrt.h"
#endif
#include "mclmcrrt.h"
#include "mclcppclass.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__SUNPRO_CC)
/* Solaris shared libraries use __global, rather than mapfiles
* to define the API exported from a shared library. __global is
* only necessary when building the library -- files including
* this header file to use the library do not need the __global
* declaration; hence the EXPORTING_<library> logic.
*/
#ifdef EXPORTING_blank
#define PUBLIC_blank_C_API __global
#else
#define PUBLIC_blank_C_API /* No import statement needed. */
#endif
#define LIB_blank_C_API PUBLIC_blank_C_API
#elif defined(_HPUX_SOURCE)
#ifdef EXPORTING_blank
#define PUBLIC_blank_C_API __declspec(dllexport)
#else
#define PUBLIC_blank_C_API __declspec(dllimport)
#endif
#define LIB_blank_C_API PUBLIC_blank_C_API
#else
#define LIB_blank_C_API
#endif
/* This symbol is defined in shared libraries. Define it here
* (to nothing) in case this isn't a shared library.
*/
#ifndef LIB_blank_C_API
#define LIB_blank_C_API /* No special import/export declaration */
#endif
extern LIB_blank_C_API
bool MW_CALL_CONV blankInitializeWithHandlers(
mclOutputHandlerFcn error_handler,
mclOutputHandlerFcn print_handler);
extern LIB_blank_C_API
bool MW_CALL_CONV blankInitialize(void);
extern LIB_blank_C_API
void MW_CALL_CONV blankTerminate(void);
extern LIB_blank_C_API
void MW_CALL_CONV blankPrintStackTrace(void);
extern LIB_blank_C_API
bool MW_CALL_CONV mlxBlank(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
extern LIB_blank_C_API
long MW_CALL_CONV blankGetMcrID();
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
/* On Windows, use __declspec to control the exported API */
#if defined(_MSC_VER) || defined(__BORLANDC__)
#ifdef EXPORTING_blank
#define PUBLIC_blank_CPP_API __declspec(dllexport)
#else
#define PUBLIC_blank_CPP_API __declspec(dllimport)
#endif
#define LIB_blank_CPP_API PUBLIC_blank_CPP_API
#else
#if !defined(LIB_blank_CPP_API)
#if defined(LIB_blank_C_API)
#define LIB_blank_CPP_API LIB_blank_C_API
#else
#define LIB_blank_CPP_API /* empty! */
#endif
#endif
#endif
extern LIB_blank_CPP_API void MW_CALL_CONV blank(int nargout, mwArray& str2, const mwArray& str);
#endif
#endif
Ran
Ran on 6 Jun 2012
Hi Kaustubha,
Thank you. It does sound good. I'll give it a try. It should be the thing I am looking for. Thanks again.

1 Comment

Ran: Could you please accept my answer if it helps. Thanks!

Sign in to comment.

Categories

Products

Community Treasure Hunt

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

Start Hunting!