Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C++ function from C without using extern "C"

Is it possible to call a function in a C++ DLL from C code? The function is not declared extern "C". An ugly platform dependent hack that only works with Visual Studio is fine.

Calling conventions should not be a major issue, but how do I deal with name mangling.? For instance with Visual Studio, a C++ function with signature void f() has the mangled name ?f@@YAXXZ and that is not a legal C identifier.

(You don't need to tell me that I should declare the C++ function as extern "C". I already know that. But I'm in a situation where I cannot change the C++ code.)

like image 673
Johan Råde Avatar asked Jan 04 '14 13:01

Johan Råde


1 Answers

Wrap the offenging function in another C++ function, and declare it with extern "C". No need to create a special DLL for it, just include one C++ file in your project.

like image 66
n. 1.8e9-where's-my-share m. Avatar answered Sep 28 '22 16:09

n. 1.8e9-where's-my-share m.