Using dlsym in c++ without extern “C”

前端 未结 3 2119
旧巷少年郎
旧巷少年郎 2021-01-14 18:11

I have a system in which I give the user a function prototype and the user has to implement it. Now, I compile this file using g++ and load it dynamically using dlopen and d

3条回答
  •  天命终不由人
    2021-01-14 19:02

    Another alternative to extern "C" before each declaration is to use the block-style syntax:

    extern "C" {
    
    void my_callback();
    int other_functionality( foo * );
    
    }
    

    Often the extern "C" { and closing brace } are wrapped in macros, which are conditional on the __cplusplus built-in macro, so the header can also be used from pure C. This also encapsulates the part you find objectionable.

    In any case, I don't see what the big deal is. If the user can write in C++, they should be competent to enclose their C interface function prototypes in extern "C" when the library documentation tells them to.

提交回复
热议问题