简体   繁体   English

Mex功能未解决的外部

[英]Mex function unresolved external

I am trying to build a mex function in MATLAB. 我正在尝试在MATLAB中构建mex函数。 The function depends on a C++ library. 该函数取决于C ++库。 However, I get unresolved externals in MATLAB no matter what I do. 但是,无论我做什么,都会在MATLAB中得到无法解析的外部函数。 I have created three trivial files to demonstrate the problem: 我创建了三个简单的文件来演示该问题:

my_test123.h my_test123.h

_declspec(dllexport) void my_test();

my_test.cpp my_test.cpp

extern "C" {
#include "my_test123.h"
}
void my_test() {
}

I compile and link the two files above using the command: 我使用以下命令编译并链接以上两个文件:

cl /LD /Femy_test.dll my_test.cpp

This generates two files, my_test.lib and my_test.dll . 这将生成两个文件my_test.libmy_test.dll

The third file is a trivial mexfunction: 第三个文件是简单的混合函数:

my_mex.cpp my_mex.cpp

#include "mex.h"

extern "C" {
void my_test(); 
}

/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
    my_test();
}

In MATLAB, I use the command below: 在MATLAB中,我使用以下命令:

mex  -v my_mex.cpp my_test.lib

I also tried: 我也尝试过:

mex  -v my_mex.cpp -lmy_test.lib

All files are in the same directory and the mex command is finding the .lib file (if I try a random name instead of my_test.lib , I get a file not found error). 所有文件都在同一目录中,并且mex命令正在查找.lib文件(如果我尝试使用随机名称而不是my_test.lib,则会出现文件未找到错误)。

The error I get is: 我得到的错误是:

Error using mex Creating library my_mex.lib and object my_mex.exp my_mex.obj : error LNK2019: unresolved external symbol my_test referenced in function mexFunction my_mex.mexw64 : fatal error LNK1120: 1 unresolved externals 使用mex创建库my_mex.lib和对象my_mex.exp my_mex.obj时出错:错误LNK2019:函数mexFunction my_mex.mexw64中引用的未解析外部符号my_test:致命错误LNK1120:1个未解析外部

I have also tried making every file a C file (removing the externs and changing the mexfunciton extension to .c) and compiling in C. But I get the same exact error. 我也尝试过将每个文件都制作为C文件(删除externs并将mexfunciton扩展名更改为.c)并在C中进行编译。但是我得到了相同的确切错误。

I am using Visual Studio 2013 and 64 bit version of MATLAB 2014b. 我正在使用Visual Studio 2013和MATLAB 2014b的64位版本。

Any help is much appreciated. 任何帮助深表感谢。

After many hours working on this and help from the MathWorks support line, I discovered the following: 经过数小时的研究并在MathWorks支持热线的帮助下,我发现了以下内容:

You need to take several factors into account: 您需要考虑几个因素:

  • Is your MATLAB 32 bit or 64 bit? 您的MATLAB是32位还是64位?
  • Is the extension of your mexfunction .c or .cpp? 您的混合功能的扩展名是.c还是.cpp?
  • How are you using the extern "C"? 您如何使用外部“ C”?
  • Is the .dll a 64 bit .dll or a 32 bit .dll? .dll是64位.dll还是32位.dll?

Let's assume the .dll is a 32 bit .dll and MATLAB is 32 bits 假设.dll是32位.dll,而MATLAB是32位

mex function has an extension .cpp, .dll is a C++ .dll You don't need to add any extern "C" neither in my_test.cpp or in my_mex.cpp. mex函数具有扩展名.cpp,.dll是C ++ .dll,您无需在my_test.cpp或my_mex.cpp中都添加任何外部“ C”

mex function has an extension .c, .dll is a C++ .dll You need to add "extern "C"* in my_test.cpp. mex函数具有扩展名.c,.dll是C ++ .dll,您需要在my_test.cpp中添加“ extern“ C” *。

mex function has an extension .cpp, .dll is a C .dll You don't need to add extern "C" to my_test.cpp but you need one in my_mex.cpp. mex函数的扩展名是.cpp,.dll是 C.dll。您不需要在my_test.cpp中添加extern“ C” ,但是在my_mex.cpp中需要一个扩展名。

mex function has an extension .c, .dll is a C .dll You don't need to add any extern "C" neither in my_test.cpp or in my_mex.cpp. mex函数的扩展名是.c,.dll是 C.dll。您无需在my_test.cpp或my_mex.cpp中添加任何外部“ C”

It looks like depending on the extension of the mex function file, MATLAB compiles it as a C or a C++ file. 根据mex函数文件的扩展名,MATLAB会将其编译为C或C ++文件。 Knowing this, the extern usage should make sense. 知道这一点,外部用法应该很有意义。

All the above is still valid but for a 64 bit MATLAB but you need a 64 bit dll. 以上所有内容仍然有效,但对于64位MATLAB而言,但您需要64位dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM