简体   繁体   English

我可以制作函数`extern“c”`吗?

[英]Can I make function `extern "c"`?

I have some cpp files, and I want to combine them with LuaJit using FFI.我有一些 cpp 文件,我想使用 FFI 将它们与 LuaJit 结合起来。

But the problem is that, I have to add extern "c" symbols for almost every function to make it possible for FFI to access them.但问题是,我必须为几乎每个函数添加extern "c"符号,以使 FFI 能够访问它们。

Is there a simpler way to make this done?有没有更简单的方法来完成这项工作?

Several functions can be placed inside a single extern "C" block.几个函数可以放在一个extern "C"块中。 This allows you to type extern "C" only once for each header file.这允许您为每个头文件只键入一次extern "C"

extern "C" {
    void function1();
    void function2();
}

Though it is non-portable, you could implement a function signature and generates the Mangled name using the name mangling protocol to find the symbol name for FFI.虽然它是不可移植的,但您可以实现一个函数签名并使用名称修饰协议生成Mangled 名称,以查找 FFI 的符号名称。

Gcc and Clang on Linux use the Itanium C++ ABI Name Mangling Rules, which can be found here. Linux 上的 Gcc 和 Clang 使用 Itanium C++ ABI 名称修改规则,可以在此处找到。

On Windows, MSVC uses a non-documented name mangling scheme.在 Windows 上,MSVC 使用未记录的名称修改方案。

Yes.是的。 Define a simple, minimal, wrapper API and export it:定义一个简单的、最小的包装 API 并将其导出:

// NOTE: Exported functions do heavy parley and medical-research leveraging C++ under the hood (and only and the hood).
extern "C" {
  void achieve_world_peace(void);

  void treat_cancer(void);
}

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

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