プリプロセッサがシンボルを置き換えずにglibcがどのように初期化されるかを理解しようとしています。errno
errno
私は最初にcsu/errno-loc.cと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;
}
ただし、コンパイルしようとすると、次のエラー メッセージが表示されます。
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
プリプロセッサが3 行目(*myerrno_location(void))
に遭遇myerrno
したときに置換を行っていることがわかります。当然のことながら、これは予期された動作です。
これがglibcにとって問題にならない理由がわかりません。のスレッドセーフな実装は、静的変数errno
の名前を変更せずに、このプリプロセッサ置換の問題をどのように回避しますか?errno