问题描述
我使用Visual Studio 2012 x64直接构建和调试MATLAB 2014a x64 mex文件(不使用MATLAB中的 mex
命令)。我按照中的说明设置了名为 test1 $ c>的Visual Studio项目$ c>。我按照撰写一个简单的mex文件,
test1.cpp
:
#include<数学。
#include< matrix.h>
#include< mex.h>
void mexFunction(int nlhs,mxArray * plhs [],int nrhs,const mxArray * prhs [])
{
mexPrintf(Hello World!\\\
) ;
}
构建此解决方案给我这条消息:
> 1> ------ Build started:项目:test1,配置:Debug Win32
> - 1>创建库C:\PROJECTS\matlab\mex\test1\Debug\test1.lib
>和对象
> C:\PROJECTS\matlab\mex\test1\Debug\test1.exp
> 1> test1.obj:error LNK2019:unresolved external symbol _mexPrintf
>在函数_mexFunction 1> MSVCRTD.lib(crtexe.obj)中引用:error
> LNK2019:未解析的外部符号_main在函数
中引用> ___tmainCRTStartup 1> C:\PROJECTS\matlab\mex\test1\Debug\test1.mexw64
> :致命错误LNK1120:2 unresolved externals
> ========== Build:0成功,1失败,0最新,0跳过==========
我在这里缺少配置步骤吗?我完全使用了中的步骤,但是对于最新版本的MATLAB,它可能不完整吗?
您需要创建一个生成DLL(而不是控制台或GUI应用程序)的Visual Studio项目。 MEX文件基本上是具有自定义扩展的共享库(Windows上的 *。mexw32
或 *。mexw64
p>
错误消息表明链接器找不到可执行文件的入口点函数 main()
对于有价值的东西,你可以在MATLAB中运行 mex -v ...
这样它就显示了用于调用编译器和链接器的确切命令。
EDIT:
分步说明:
-
创建一个新的VS项目来构建DLL (
Visual C ++> Win32> Win32控制台应用程序
,然后在向导中将类型设置为DLL
> -
由于我们使用MATLAB x64,因此我们需要调整项目配置以生成64位二进制文件。从
构建
菜单中,选择配置管理器
。从下拉菜单中选择< New>
创建新的平台配置,选择x64
并接受。 / p> -
按照您之前提到的
-
添加MEX文件的C / C ++源代码
#includemex.h
void mexFunction(int nlhs,mxArray * plhs [],int nrhs,const mxArray * prhs [])
{
plhs [0] = mxCreateString 你好,世界);
}
-
在发布模式下将平台切换到x64构建项目。现在你应该有一个
somefile.mexw64
MEX文件,你可以在MATLAB中作为一个常规函数调用。
I'm using Visual Studio 2012 x64 to build and debug a MATLAB 2014a x64 mex file directly (without using the mex
command in MATLAB). I followed the instructions in this question to set up a Visual Studio project named test1
. I followed this tutorial to write a simple mex file, test1.cpp
:
#include <math.h>
#include <matrix.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World!\n");
}
Building this solution gives me this message:
> 1>------ Build started: Project: test1, Configuration: Debug Win32
> ------ 1> Creating library C:\PROJECTS\matlab\mex\test1\Debug\test1.lib
> and object
> C:\PROJECTS\matlab\mex\test1\Debug\test1.exp
> 1>test1.obj : error LNK2019: unresolved external symbol _mexPrintf
> referenced in function _mexFunction 1>MSVCRTD.lib(crtexe.obj) : error
> LNK2019: unresolved external symbol _main referenced in function
> ___tmainCRTStartup 1>C:\PROJECTS\matlab\mex\test1\Debug\test1.mexw64
> : fatal error LNK1120: 2 unresolved externals
> ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Are there configuration steps I'm missing here? I used the steps in this answer exactly, but maybe it's incomplete for more recent versions of MATLAB?
You need to create a Visual Studio project that produces DLLs (as opposed to console or GUI applications). MEX-files are basically shared libraries with a custom extension (*.mexw32
or *.mexw64
on Windows)
The error message indicates that the linker cannot find the entry-point function main()
for an executable, which does not exist for dynamic libraries.
For what it's worth, you can run mex -v ...
in MATLAB, that way it shows you the exact commands used to invoke the compiler and linker.
EDIT:
Step-by-step instructions:
create a new empty VS project to build DLLs (
Visual C++ > Win32 > Win32 console application
then set the type toDLL
in the wizard)Since we're working with MATLAB x64, we need to adjust project configuration to generate 64-bit binaries. From
Build
menu, selectConfiguration Manager
. From the drop-down menu choose<New>
to create a new platform configs, selectx64
and accept.follow the previous instructions you mentioned
add the C/C++ source code for the MEX-file
#include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { plhs[0] = mxCreateString("hello world"); }
switch platform to "x64" in "Release" mode, and build the project. Now you should have a
somefile.mexw64
MEX-file which you can call in MATLAB as a regular function.
这篇关于在Visual Studio中构建MATLAB mex文件给出“LNK2019未解析的外部符号_mexPrintf在函数mexFunction中引用”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!