How can I expose C++ function pointers in C?

前端 未结 4 1487
盖世英雄少女心
盖世英雄少女心 2021-01-19 16:57

I have two types of function pointers defined in my C++ that look like this:

typedef void(*CallbackFn)(bool, std::str         


        
4条回答
  •  春和景丽
    2021-01-19 17:23

    C doesn't do / cannot handle C++ name mangling (nor C++ types that are not identical to C types). You cannot use non-POD types (and plain function pointers involving types not usable in C) in anything exposed to C. And you need to use extern "C" for the exposed stuff, to disable name mangling (or rather, use whatever naming convention/mangling your current platforms C compiler uses).

    In short: use extern "C" for anything that must be callable from C and make sure anything exposed that way only uses types that you can write/use in C.

提交回复
热议问题