digitalmars.D.learn - Global extern(C) in druntime header files?
- Gary Willoughby (27/27) Jun 05 2013 I've been doing some coding and noticed something strange with
- Jesse Phillips (4/4) Jun 05 2013 Not going to look into the file, but I'm pretty sure bsd_signal
- Gary Willoughby (6/10) Jun 05 2013 Ah sorry i wasn't clear.
- Mike Parker (5/16) Jun 05 2013 You are passing a function pointer to a C library, where it will
- Gary Willoughby (1/6) Jun 06 2013 Right ok thanks.
I've been doing some coding and noticed something strange with core.sys.posix.signal. In the following snippet you will see that i have to decorate the handleTermination function with extern(C) to satisfy the type requirements of the bsd_signal function. import core.sys.posix.signal; import std.c.stdlib; import std.stdio; void main(string[] args) { bsd_signal(SIGINT, &handleTermination); while (true) { } } extern(C) void handleTermination(int signal) { writefln("Caught signal: %s", signal); exit(signal); } This seems really odd until you take a look at the header file. In signal.d you will see extern(C): specified at the top and then further down this: private alias void function(int) sigfn_t; essentially decorating this alias with extern(C). Is this right? Should this alias be decorated like that in the header file?
Jun 05 2013
Not going to look into the file, but I'm pretty sure bsd_signal is function for an external C library right? If that is the case, it must use C calling convention, so yes this is correct.
Jun 05 2013
On Wednesday, 5 June 2013 at 18:54:45 UTC, Jesse Phillips wrote:Not going to look into the file, but I'm pretty sure bsd_signal is function for an external C library right? If that is the case, it must use C calling convention, so yes this is correct.Ah sorry i wasn't clear. The question i have is, why do i need to decorate the function (with extern(C)) that i pass to bsd_signal. The only reason i can see is that the type hint is decorated but surely it doesn't need to be.
Jun 05 2013
On Wednesday, 5 June 2013 at 20:40:59 UTC, Gary Willoughby wrote:On Wednesday, 5 June 2013 at 18:54:45 UTC, Jesse Phillips wrote:You are passing a function pointer to a C library, where it will be expected that the function uses the C calling convention. So you have to declare the function as extern(C), otherwise DMD will not compile it as such. That can cause segfaults.Not going to look into the file, but I'm pretty sure bsd_signal is function for an external C library right? If that is the case, it must use C calling convention, so yes this is correct.Ah sorry i wasn't clear. The question i have is, why do i need to decorate the function (with extern(C)) that i pass to bsd_signal. The only reason i can see is that the type hint is decorated but surely it doesn't need to be.
Jun 05 2013
You are passing a function pointer to a C library, where it will be expected that the function uses the C calling convention. So you have to declare the function as extern(C), otherwise DMD will not compile it as such. That can cause segfaults.Right ok thanks.
Jun 06 2013