gpt4 book ai didi

c++ - matlab与c语言dll的连接

转载 作者:行者123 更新时间:2023-11-30 17:59:00 24 4
gpt4 key购买 nike

我试图在matlab中使用clllib函数,我将.dll文件和.h文件放在与正在开发的.m MATLAB文件相同的目录中,之后我尝试使用loadlibrary函数,但它有一些警告

警告:

Warning: 
Message from C preprocessor:

lcc preprocessor error: C:\Users\MAHNAZ\Documents\MATLAB\T1Header.h:1 Could not find include file
<iostream>

lcc preprocessor warning: C:\Users\MAHNAZ\Documents\MATLAB\T1Header.h:21 EOF inside comment

> In loadlibrary at 351

Warning: The function 'Add' was not found in the library

> In loadlibrary at 435

Warning: The function 'Function' was not found in the library

> In loadlibrary at 435

当我想使用这样的calllib函数时:calllib('t1', 'Add', 2,3) matlab给我一个错误:

??? Error using ==> calllib Method was not found.

我的头文件是:

#ifndef T1_HEADER_H
#define T1_HEADER_H
extern int Add( int a, int b );
extern void Function( void );
#endif

我的源文件是:

#include iostream
#include "T1Header.h"

extern int Add( int a, int b )
{
return( a + b );
}

extern void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}

我使用 Visual C++ 2010 和 Matlab 7.6.0(R2008a)

关于问题所在、我可以采取哪些措施来修复此错误,或者我还可以尝试从 MATLAB 中调用此 .dll 的其他方法,有什么建议吗?

最佳答案

前面的一些注释和评论:

  • 加载 iostream 时出现的错误听起来有点像代码正在作为 C 而不是 C++ 进行处理。
  • 您粘贴的源文件的名称 iostream 周围没有尖括号,但我猜当您撰写这篇文章时它们已经丢失了。
  • 定义函数时,它们不应该是 extern 的。
  • 您粘贴的错误消息中提到的评论未包含在您的粘贴中。
  • 令我有点惊讶的是,matlab 甚至查看 header ,我本以为它只使用 DLL 文件。

但是,假设最终创建了 DLL 文件,这个问题的主要原因应该是这样:Matlab 可能会想要使用C 调用约定,因此您应该将函数括在 extern "C"{ … } 在 header 和实现中都阻止。

关于c++ - matlab与c语言dll的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11844776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com