C linkage for function pointer passed to C library

谁说胖子不能爱 提交于 2019-12-01 15:13:39

问题


My case is pretty simple: I want my C++ program to deal with Unix signals. To do so, glibc provides a function in signal.h called sigaction, which expects to receive a function pointer as its second argument.

extern "C"
{
void uponSignal(int);
}

void uponSignal(int)
{
    // set some flag to quit the program
}

static
void installSignalHandler()
{
    // initialize the signal handler
    static struct sigaction sighandler;
    memset( &sighandler, 0, sizeof(struct sigaction) );
    sighandler.sa_handler = uponSignal;

    // install it
    sigaction( SIGINT, &sighandler, nullptr );
}

My question is: is the extern "C" linkage specifier necessary?

Bonus question: can uponSignal be declared static?


回答1:


My question is: is the extern "C" linkage specifier necessary?

For maximum portability, yes; the C++ standard only guarantees interoperability with C via functions declared extern "C".

Practically, no; most sensible ABIs (including the GNU ABI used by glibc) will use the same calling convention for C and C++ non-member (and static member) functions, so that extern "C" is only needed to share the function name between languages.

Bonus question: can uponSignal be declared static?

Yes. External linkage is only needed to access the function by name from other translation units; it's not necessary to call the function via a function pointer.




回答2:


extern C is only necessary if you export your symbols from your binary or import them from another binary (typically in both cases, a shared library), in order to avoid name mangling.

Here this is not the case, you're not linking uponSignal across various binaries so you don't need extern C. All you're doing is pass your function's address to sigaction from a function that already knows uponSignal's address since they are (apparently) part of the same translation unit, or at the very least of the same binary.

Bonus question: can uponSignal be declared static?

Sure if you want. uponSignal doesn't need external linkage anyway.



来源:https://stackoverflow.com/questions/16520951/c-linkage-for-function-pointer-passed-to-c-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!