Compiler properties for building MEX-files

I'm working with MATLAB R2010b and need to compile a C++ source file to a .mex64 MEX-file.
I'm using the Microsoft VS2008 SP1 as a third party compiler.
mex.getCompilerConfigurations gives the following
mex.CompilerConfiguration
Package: mex
Properties:
Name: 'Microsoft Visual C++ 2008'
Manufacturer: 'Microsoft'
Language: 'C++'
Version: '9.0'
Location: 'C:\Program Files (x86)\Microsoft Visual Studio 9.0'
Details: [1x1 mex.CompilerConfigurationDetails]
So I think that the compiler is installed correctly. I tried to compile a simple hello.cpp file and it worked perfectly. However, my C++ source file requires the usage of MFC in a shared DLL. When I try to compile it in matlab using >>mex filenme.cpp I get the following error:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\INCLUDE\afx.h(24) : fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
C:\PROGRA~1\MATLAB\R2010B\BIN\MEX.PL: Error: Compile of 'filename.cpp' failed.
??? Error using ==> mex at 208
Unable to complete successfully.
How can I adjust the compiler used by MATLAB to use MFC in a Shared DLL? (as I guess that this is the problem, or?)
Thank you

Answers (1)

Perhaps you should build your code in the Visual Studio IDE directly. See Custom Building MEX-Files for some tips. I would recommend get a simple MEX example to compile on the IDE (without using the MATLAB mex command), before you try your code.
Once you can compile your MEX-function on the IDE, you might want to follow generic Visual Studio steps to use the MFC. For example, this page suggests setting:
Project + Properties, General. Ensure Use of MFC is set to "Use MFC in a Shared Library".
This will ensure that all Visual Studio settings are configured for use of the MFC.

2 Comments

Thanks for your reply.
Yes I was also thinking to do that, but I wanted to give it a try first using the mex command. I think if we included #define _AFXDLL in the source file it should prevent static linking.
Eventhough I tried to use the VS 2008 SP1 to compile my file. I started a new project and set its properties as recommended in your attached link. I think the problem of the MFC lib is solved, but I still get the following errors, eventhough libmat.lib is included in the project:
1>------ Build started: Project: sfunfg, Configuration: Debug Win32 ------
1>Linking...
1> Creating library .\Debug/sfunfg.lib and object .\Debug/sfunfg.exp
1>sfunfg.obj : error LNK2019: unresolved external symbol _mxGetString_730 referenced in function "void __cdecl mdlStart(struct SimStruct_tag *)" (?mdlStart@@YAXPAUSimStruct_tag@@@Z)
1>sfunfg.obj : error LNK2019: unresolved external symbol _mxCreateDoubleMatrix_730 referenced in function "void __cdecl _ProcessMexSfunctionCmdLineCall(int,struct mxArray_tag * * const,int,struct mxArray_tag const * * const)" (?_ProcessMexSfunctionCmdLineCall@@YAXHQAPAUmxArray_tag@@HQAPBU1@@Z)
1>Debug/sfunfg.mex32 : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\Users\Studer\Desktop\sfunfg\Debug\BuildLog.htm"
1>sfunfg - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Do you have any idea what is wrong?
Thanks
My best bet is that your Visual Studio project is not configured for an x64 target (by default, it is setup for a 32-bit target, and since libmat.lib, etc. are 64-bit libraries, they don't work with 32-bit projects).

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Asked:

on 22 Jul 2011

Community Treasure Hunt

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

Start Hunting!