I have some cpp files, and I want to combine them with LuaJit using FFI.
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.
Is there a simpler way to make this done?
I have some cpp files, and I want to combine them with LuaJit using FFI.
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.
Is there a simpler way to make this done?
Several functions can be placed inside a single extern "C"
block. This allows you to type extern "C"
only once for each header file.
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.
Gcc and Clang on Linux use the Itanium C++ ABI Name Mangling Rules, which can be found here.
On Windows, MSVC uses a non-documented name mangling scheme.
Yes. Define a simple, minimal, wrapper API and export it:
// 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);
}
© 2022 - 2024 — McMap. All rights reserved.
extern "C"
-ready? I.e. do they use only C types (no references, no classes etc.)?extern "C"
only turns off name mangling. – Galiotextern "C"
? Then it would be really difficult if I want to use opensource cpp files for my Lua project. – Companionwayextern "C"
is to provide an interface to C code, by disallowing features that are incompatible with C. If Lua requires a C interface, it is not possible to use features specific to C++ across that interface. If you want to use opensource C++ files in your project, you need to provide anextern "C"
set of functions which are compiled using a C++ compiler, and use capabilities of C++ in their implementation. – Lissnerextern "C"
whatever function you want, but it doesn't mean that all marked functions will be C-compatible. – Galiotextern "C"
block for it. It seems working now. – Companionway