为什么 matlab 不链接我的 C MEX 代码?

发布于 2024-12-11 18:50:04 字数 725 浏览 0 评论 0原文

我在使用 MS VC++ 编译器编译 mex C 函数时遇到以下问题:我在一个单独的文件 disc_rhs.c 中有一个函数 disc_rhs__ ,该文件是由 f2c 创建的(应该不重要,但是......)。在我的主函数中,我在某个地方编写了

...
disc_rhs__(...);
...

尝试使用

mex sfun_kalman.cpp -L. -llapack -lcblas_WIN -lblas_WIN -lf2c disc_rhs.c kalmanfilter_f2c.cpp

这会导致错误的编译。链接器找不到 ""int __cdecl Disc_rhs__(double *,double *,double *,double *,double *)" (?disc_rhs__@@YAHPAN0000@Z)" 在 <代码>kalmanfilter.cpp。

为了编译所有内容,我将 disc_rhs.cpp 复制到 disc_rhs.h 并删除了函数声明,只留下了一个存根。我将其包含在 kalmanfilter.h 中。

那么为什么 matlab mex 编译器无法识别正确的符号并链接到 disc_rhs.c 的编译呢?

谢谢 基督教

I have the following problem in compiling my mex C functions using MS VC++ Compiler: I have a function disc_rhs__ in a separate file disc_rhs.c which was createtd by f2c (which should be unimportant, but...). In my main function I write somewhere

...
disc_rhs__(...);
...

I try to compile using

mex sfun_kalman.cpp -L. -llapack -lcblas_WIN -lblas_WIN -lf2c disc_rhs.c kalmanfilter_f2c.cpp

This leads to an error. The linker cannot find the external symbol ""int __cdecl disc_rhs__(double *,double *,double *,double *,double *)" (?disc_rhs__@@YAHPAN0000@Z)" used in the kalmanfilter.cpp.

To get everything compiling I copied the disc_rhs.cppto disc_rhs.h and removed the function declaration just letting a stub. This I included in kalmanfilter.h.

So why does matlab mex compiler not recognize the corrct symbol and link with the compilation of disc_rhs.c?

Thanks
Christian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-12-18 18:50:04

不要将 disc_rhs.cpp 的全部内容放入头文件中,然后将其包含在另一个文件中。仅当该文件被单个源文件包含时,这才有效,一旦将其包含在另一个源文件中,您就会收到多个定义错误。

解决此问题的正确方法是创建一个 disc_rhs.h 文件,其中包含 disc_rhs.cpp 中需要由其他模块使用的所有函数的原型。然后,您可以将头文件包含在 kalmanfilter.cpp 中(以及需要使用这些函数的任何其他文件)。

编辑:
发生错误的原因是 disc_rhs 文件具有 .c 扩展名,导致 MSVC 将其编译为 C 文件。然而,由于它在 kalmanfilter.cpp 中使用,链接器期望找到一个名称已损坏的 C++ 函数,而该函数并不存在。要解决此问题,您需要告诉编译器 disc_rhs__() 是一个 C 函数。

在disc_rhs.h中,

#ifdef __cplusplus
extern "C" 
#endif
int disc_rhs__(double *,double *,double *,double *,double *);

extern "C"指令需要出现在disc_rhs.c导出的每个项目的前面,因此,如果您有几项需要extern,则以下内容语法更方便。

#ifdef __cplusplus
extern "C" {
#endif
int disc_rhs__(double *,double *,double *,double *,double *);

// other stuff being externed

#ifdef __cplusplus
}
#endif

Do not put the entire contents of disc_rhs.cpp into a header file and then include it in another file. This will only work as long that file is being included by a single source file, as soon as you include it in another source file you'll get multiple definition errors.

The right way to fix this problem is to create a disc_rhs.h file that contains the prototypes of all functions from disc_rhs.cpp that need to be used by other modules. Then you'd include the header file in kalmanfilter.cpp (and any other files that need to use those functions).

EDIT:
The error is happening because the disc_rhs file has a .c extension which causes MSVC to compile it as a C file. However, since it is being used in kalmanfilter.cpp the linker is expecting to find a C++ function that has been name mangled, which doesn't exist. To fix the problem, you need to tell the compiler that disc_rhs__() is a C function.

In disc_rhs.h

#ifdef __cplusplus
extern "C" 
#endif
int disc_rhs__(double *,double *,double *,double *,double *);

The extern "C" directive needs to appear in front of each item being exported by disc_rhs.c, so if you have several things to extern, the following syntax is more convenient.

#ifdef __cplusplus
extern "C" {
#endif
int disc_rhs__(double *,double *,double *,double *,double *);

// other stuff being externed

#ifdef __cplusplus
}
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文