solve Matlab "mex -setup" not work

本文介绍了在使用Matlab进行mex编译时遇到的配置问题及解决方案,包括如何手动配置mexopts.bat文件以适应不同的编译环境,并解决了找不到编译器和链接库的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

执行mex setup 出现以下情况:

Please choose your compiler for building external interface (MEX) files: 

Would you like mex to locate installed compilers [y]/n? y

Select a compiler: 

[0] None 

Compiler: 

如果选择n

Please choose your compiler for building external interface (MEX) files: 

Would you like mex to locate installed compilers [y]/n? n

Select a compiler: 
[1] Intel C++ 9.1 (with Microsoft Visual C++ 2005 SP1 linker) 
[2] Intel Visual Fortran 10.1 (with Microsoft Visual C++ 2005 SP1 linker) 
[3] Intel Visual Fortran 9.1 (with Microsoft Visual C++ 2005 SP1 linker) 
[4] Microsoft Visual C++ 2005 SP1 
[5] Microsoft Visual C++ 2008 Express 
[6] Microsoft Visual C++ 2008 SP1 
[7] Microsoft Visual C++ 2012 

[0] None 

Compiler: 7

The default location for Microsoft Visual C++ 2012 compilers is C:\Program Files (x86)\Microsoft Visual Studio 11.0, 
but that directory does not exist on this machine.  

Use C:\Program Files (x86)\Microsoft Visual Studio 11.0 anyway [y]/n? y

Please verify your choices: 

Compiler: Microsoft Visual C++ 2012  
Location: C:\Program Files (x86)\Microsoft Visual Studio 11.0 

Are these correct [y]/n? y

Trying to update options file: C:\Users\healthsee01\AppData\Roaming\MathWorks\MATLAB\R2009a\mexopts.bat 
From template:              C:\PROGRA~1\MATLAB\R2009a\bin\win64\mexopts\msvc110opts.bat 

Done . . . 

************************************************************************** 
  Warning: The MATLAB C and Fortran API has changed to support MATLAB 
           variables with more than 2^32-1 elements.  In the near future 
           you will be required to update your code to utilize the new 
           API. You can find more information about this at: 
           http://www.mathworks.com/support/solutions/data/1-5C27B9.html?solution=1-5C27B9 
           Building with the -largeArrayDims option enables the new API. 
************************************************************************** 

尝试编译cpp:

mex test.cpp
  Error: Could not find the compiler "cl" on the DOS path. 
         Use mex -setup to configure your environment properly. 


  C:\PROGRA~1\MATLAB\R2009A\BIN\MEX.PL: Error: Unable to locate compiler. 

说明编译器没有成功配置,此时,只能手动配置,方法如下:

(1)请先确保安装了相应的VS和win sdk,这里是VS2012和Windows Kits 8.0
(2)在目录 C:\Users\MyPC\AppData\Roaming\MathWorks\MATLAB\R2009a下找到mexopts.bat,用以下内容覆盖:

@echo off
rem MSVC110OPTS.BAT
rem
rem    Compile and link options used for building MEX-files
rem    using the Microsoft Visual C++ compiler version 11.0
rem
rem    $Revision: 1.1.6.4.2.1 $  $Date: 2012/07/12 13:53:59 $
rem    Copyright 2007-2009 The MathWorks, Inc.
rem
rem StorageVersion: 1.0
rem C++keyFileName: MSVC100OPTS.BAT
rem C++keyName: Microsoft Visual C++ 2012
rem C++keyManufacturer: Microsoft
rem C++keyVersion: 11.0
rem C++keyLanguage: C++
rem C++keyLinkerName: Microsoft Visual C++ 2012
rem C++keyLinkerVersion: 11.0
rem
rem ********************************************************************
rem General parameters
rem ********************************************************************

set MATLAB=%MATLAB%
set VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 11.0
***************************************************************************
  Warning: MEX-files generated using Microsoft Visual C++ 2012 require
           that Microsoft Visual Studio 2012 run-time libraries be 
           available on the computer they are run on.
           If you plan to redistribute your MEX-files to other MATLAB
           users, be sure that they have the run-time libraries.
***************************************************************************

set VCINSTALLDIR=%VSINSTALLDIR%\VC
rem In this case, LINKERDIR is being used to specify the location of the SDK

set LINKERDIR=C:\Program Files (x86)\Windows Kits\8.0\
set PATH=%VCINSTALLDIR%\bin\amd64;%VCINSTALLDIR%\bin;%VCINSTALLDIR%\VCPackages;%VSINSTALLDIR%

\Common7\IDE;%VSINSTALLDIR%\Common7\Tools;%LINKERDIR%\bin\x64;%MATLAB_BIN%;%PATH%
set INCLUDE=%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\ATLMFC\INCLUDE;%INCLUDE%
set LIB=%VCINSTALLDIR%\LIB\amd64;%VCINSTALLDIR%\ATLMFC\LIB\amd64;%MATLAB%\extern\lib\win64;C:\Program 

Files (x86)\Windows Kits\8.0\Lib\win8\um\x64;%LIB%
set MW_TARGET_ARCH=win64

rem ********************************************************************
rem Compiler parameters
rem ********************************************************************
set COMPILER=cl
set COMPFLAGS=/c /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 

/DMATLAB_MEX_FILE /nologo /MD
set OPTIMFLAGS=/O2 /Oy- /DNDEBUG
set DEBUGFLAGS=/Z7
set NAME_OBJECT=/Fo

rem ********************************************************************
rem Linker parameters
rem ********************************************************************
set LIBLOC=%MATLAB%\extern\lib\win64\microsoft
set LINKER=link
set LINKFLAGS=/dll /export:%ENTRYPOINT% /LIBPATH:"%LIBLOC%" libmx.lib libmex.lib libmat.lib /MACHINE:X64 

kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib 

oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /manifest /incremental:NO /implib:"%LIB_NAME%.x" 

/MAP:"%OUTDIR%%MEX_NAME%%MEX_EXT%.map"
set LINKOPTIMFLAGS=
set LINKDEBUGFLAGS=/debug /PDB:"%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb"
set LINK_FILE=
set LINK_LIB=
set NAME_OUTPUT=/out:"%OUTDIR%%MEX_NAME%%MEX_EXT%"
set RSP_FILE_INDICATOR=@

rem ********************************************************************
rem Resource compiler parameters
rem ********************************************************************
set RC_COMPILER=rc /fo "%OUTDIR%mexversion.res"
set RC_LINKER=

set POSTLINK_CMDS=del "%LIB_NAME%.x" "%LIB_NAME%.exp"
set POSTLINK_CMDS1=mt -outputresource:"%OUTDIR%%MEX_NAME%%MEX_EXT%;2" -manifest "%OUTDIR%%MEX_NAME%

%MEX_EXT%.manifest"
set POSTLINK_CMDS2=del "%OUTDIR%%MEX_NAME%%MEX_EXT%.manifest"
set POSTLINK_CMDS3=del "%OUTDIR%%MEX_NAME%%MEX_EXT%.map"

注:以上教程在win 7 x64 matlab 2009a上测试通过,原则上适用于任何版本的matlab;x86、不同的编译器,可以尝试修改mexopts.bat文件相应的内容。

异常分析:

无法找到编译器:

Error: Could not find the compiler "cl" on the DOS path. 
         Use mex -setup to configure your environment properly. 

请检查 mexopts.bat中的

set VSINSTALLDIR= VS安装目录

无法输入kernel32:

LINK : fatal error LNK1181: 无法打开输入文件“kernel32.lib” 

  C:\PROGRA~1\MATLAB\R2009A\BIN\MEX.PL: Error: Link of 'test.mexw64' failed. 

请检查mexopts.bat中的

set LIB = ... win sdk的lib目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值