MEX options when compiling with Visual Studio

2019-08-14 08:40发布

For some reasons I have to compile my MEX files under the Visual studio environment. There are many tutorials and my MEX files are working fine. However, there are a few MEX options, say -largeArrayDims in mex options, which I do not know where to turn on under the VS environment. Can anyone offer help?

1条回答
ら.Afraid
2楼-- · 2019-08-14 09:23

The -largeArrayDims option is a switch to the mex command in MATLAB that simply indicates not to define MX_COMPAT_32. So, in Visual Studio, you don't have to do anything since this is not defined by default. If you want the opposite behavior (-compatibleArrayDims), then define MX_COMPAT_32 in the Preprocessor section. From tmwtypes.h:

tmwtypes.h

#ifdef MX_COMPAT_32
typedef int mwSize;
typedef int mwIndex;
typedef int mwSignedIndex;
#else
typedef size_t    mwSize;         /* unsigned pointer-width integer */
typedef size_t    mwIndex;        /* unsigned pointer-width integer */
typedef ptrdiff_t mwSignedIndex;  /* a signed pointer-width integer */
#endif

In general, it is convenient to use a property sheet to set all the necessary settings for building a MEX file (library dependencies, headers, MEX-file extension, etc.). A single property sheet that works automatically for either 32-bit or 64-bit MATLAB can be found at GitHub.

Add the property sheet to each build configuration for the MEX project in the Property Manager (right click on the configuration such as Debug | x64 and select "Add Existing Property Sheet". See this post for detailed instructions.

A few additional notes:

  1. I prefer to use /EXPORT:mexFunction instead of a .def file. With a single exported function, this is much simpler.
  2. The property sheet makes a manifest file, but it's really not necessary.
  3. I include libut.lib, which provides a few nice functions for detecting a break (CTRL-C) from within a MEX file. The relevant declarations (although this is way off topic here):
// prototype the break handling functions in libut (C library)
extern "C" bool utIsInterruptPending();
extern "C" void utSetInterruptPending(bool);
查看更多
登录 后发表回答