【问题标题】:Calling C/C++ from Julia从 Julia 调用 C/C++
【发布时间】:2020-06-15 05:55:41
【问题描述】:

我正在尝试使用 C++ 为 Julia 编译一个动态库。 我在 Windows 上使用 CLion。当我使用 MinGW 编译时,ccall 与 dll 完美配合。 当我使用 MSVC 编译时,Julia ccall 找不到该函数。有谁知道为什么以及如何解决这个问题?我必须使用 MSVC ..

示例代码:

test.h

extern "C" int add2(int in);


test.cpp

#include "test.h"

int add2(int in){
return in+2;
}

【问题讨论】:

  • 感谢您的意见,但问题仍然存在...

标签: c++ c visual-c++ julia extern


【解决方案1】:

找到答案。 MSVC 编译器需要显式指令才能输出/输入外部“C”函数。以下代码适用于 MSVC,并被 Julia 的 ccall 识别:

test.h

extern "C" __declspec(dllexport) int add2(int in);


test.cpp

#include "test.h"

int add2(int in){
return in+2;
}

为了导入外部“C”函数,可以使用:

 __declspec(dllimport)

编辑: 这与编译器无关,而是所有 dll 文件都需要。 显然 MinGW 会自动执行此操作。

【讨论】:

  • 这与extern "C"的功能无关。您总是需要使用__declspec(dllexport) 来导出 DLL 中的函数。
  • 好的,但由于某种原因,它在没有指令的情况下与 MinGW 一起工作
  • 好像MinGW的链接器默认导出所有符号,见stackoverflow.com/questions/2810118/…
  • 谢谢,已加入回答
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多