I'm trying to understand how glibc initializes errno
without the preprocessor substituting the errno
symbol.
I first tried to implement a simple version myself based on csu/errno-loc.c and csu/errno.c:
myerrno.h
#ifndef MYERRNO_H
#define MYERRNO_H
extern int *myerrno_location(void);
#define myerrno (*myerrno_location())
#endif
myerrno.c
#include "myerrno.h"
static int myerrno = 0;
int *myerrno_location(void){
return &myerrno;
}
However, when I try to compile I receive the following error messages:
myerrno.c:3:1: error: function ‘myerrno_location’ is initialized like a variable
myerrno.c:3:12: error: static declaration of ‘myerrno_location’ follows non-static declaration
myerrno.h:4:13: note: previous declaration of ‘myerrno_location’ was here
I can tell that the preprocessor is substituting (*myerrno_location(void))
when it encounters myerrno
on line 3 -- and naturally this is expected behavior.
I don't understand why this isn't a problem for glibc. How do thread-safe implementations of errno
get around this preprocessor substitution issue without renaming the static errno
variable?