Cè¨èªã®æ£ããããããã¡ã¤ã«ã®æ¸ãæ¹
æè¿ãä»äºã§Cè¨èªã§ã®çµã¿è¾¼ã¿ç³»ã®éçºã«æºãã£ã¦ãã¾ãã
éçºä¸ã®ã³ã¼ããçºãã¦ããã¨ãããããã¡ã¤ã«å
ã«staticé¢æ°ã®ãããã¿ã¤ã宣è¨ãè¨è¿°ãã¦ããããããããã¡ã¤ã«å
ã§ä¸å¿
è¦ã«ä»ã®ããããã¡ã¤ã«ãã¤ã³ã¯ã«ã¼ããã¦ãããªã©ãããããã¡ã¤ã«ã®æ¸ãæ¹ãåãã£ã¦ããªãã¨æãããç®æãå¤ã
è¦ããã¾ããã
å®éãCè¨èªã®å
¥éæ¸ã§ãããããã¡ã¤ã«ã®æ¸ãæ¹ã詳ãã説æãã¦ãããã®ã¯ãåã®ç¥ã£ã¦ããéãã§ã¯åå¨ããªãã®ã§ãCè¨èªã使ã£ã¦ãã¦ãããããã¡ã¤ã«ã®æ£ããæ¸ãæ¹ãç¥ããªã人ãå°ãªããªãã®ã§ã¯ãªããã¨æããã¾ãã
ããã§ããã®ã¨ã³ããªã§ã¯ãCè¨èªã®ããããã¡ã¤ã«ã®æ¸ãæ¹ã«ã¤ãã¦ãåãç¥ã£ã¦ãããã¯ããã¯ãã¾ã¨ãã¦ã¿ã¾ããã
ã¤ã³ã¯ã«ã¼ãã¬ã¼ããæ¸ã
ããããã¡ã¤ã«ãã¡ã¤ã«ã§ä»ã®ããããã¡ã¤ã«ãã¤ã³ã¯ã«ã¼ããã¦ããã¨ããã¤ã®éã«ãåãããããã¡ã¤ã«ã2åã¤ã³ã¯ã«ã¼ããã¦ãã¾ããã¨ãããã¾ãã
ä¾ãã°ãfoo.hãbar.hãbaz.hã®3ã¤ã®ããããã¡ã¤ã«ããã£ã¦ãããããã®ããããã¡ã¤ã«ã次ã®ä¾ã®ããã«ãªã£ã¦ããã¨ãã¾ãã
/* foo.h */ typedef struct { int a; } Foo;
/* bar.h */ #include "foo.h" typedef struct { Foo foo; } Bar;
/* baz.h */ #include "foo.h" #include "bar.h" typedef struct { Foo foo; Bar bar; } Baz;
ãã®å ´åãbaz.hå
ã§ãfoo.hãç´æ¥ã®ã¤ã³ã¯ã«ã¼ãã¨bar.hçµç±ã®ã¤ã³ã¯ã«ã¼ãã«ãã2åã¤ã³ã¯ã«ã¼ãããã¦ãããããéè¤å®ç¾©ã¨ãªãã³ã³ãã¤ã«ã¨ã©ã¼ãçºçãã¦ãã¾ãã¾ãã
ãããé¿ããããã«ãããããã¡ã¤ã«å
ã«æ¬¡ã®ä¾ã®ãããªãã¯ããè¨è¿°ãã¾ãã
/* foo.h */ #ifndef FOO_H #define FOO_H /* foo.h ã®ä¸èº« */ #endif
ãã®ãã¯ããã¤ã³ã¯ã«ã¼ãã¬ã¼ãã¨å¼ã³ã¾ããã¤ã³ã¯ã«ã¼ãã¬ã¼ããæ¸ããã¨ã«ãã£ã¦ã2åç®ä»¥éã®ã¤ã³ã¯ã«ã¼ãã§ã¯ãã¡ã¤ã«ã®ä¸èº«ãå±éãããªããªãã®ã§ãå®ç¾©ã®éè¤ã«ããã³ã³ãã¤ã«ã¨ã©ã¼ãé¿ãããã¨ãã§ãã¾ãã
ãã®ãã¯ããã¯ã¯é常ã«ãã使ãã®ã§ãã¨ãã£ã¿ã§èªåçã«ã¤ã³ã¯ã«ã¼ãã¬ã¼ããæ¿å
¥ãã¦ãããããã«ãã¦ããã¨ãé常ã«ä¾¿å©ã ã¨æããã¾ãã
Emacsã®å ´åã¯ãhttp://d.hatena.ne.jp/higepon/20080731/1217491155ã«è©³ç´°ãªè¨å®æ¹æ³ãç´¹ä»ããã¦ãã¾ãã
staticãªé¢æ°ãã°ãã¼ãã«å¤æ°ã®å®£è¨ã¯ããããã¡ã¤ã«å ã«ã¯æ¸ããªã
ããã¯å½ããåã®ãã¨ãã¨æãã¾ããããã®ååãå®ããã¦ãªãã½ã¼ã¹ãå®éã«åå¨ãããããããã¦å¼·èª¿ãã¦ããã¾ãã
staticæå®åã®æå³ã¯ãstaticæå®ãããã°ãã¼ãã«å¤æ°ãé¢æ°ã®ã¹ã³ã¼ãããã®ã½ã¼ã¹ãã¡ã¤ã«å
ã«éå®ããã¨ãããã¨ã§ãã
(é¢æ°å
ã®ãã¼ã«ã«staticå¤æ°ã®æå³ã¯ã¾ãå¥)
ã§ãã®ã§ãããããããããã¡ã¤ã«ã«æ¸ãã¦å¤é¨ã«å
¬éããã¨ããã®ã¯æå³çã«èãã¦æããã«ééãã§ãã
ã¾ããstaticå¤æ°ãããããã¡ã¤ã«ã«è¨è¿°ãã¦ãã¾ãã¨ããã®ããããã¤ã³ã¯ã«ã¼ãããã½ã¼ã¹ãã¨ã«å¥ã ã®å¤æ°ã®å®ä½ãä½æããã¦ãã¾ããããããããæå³ããæåã«ã¯ãªããªãã¨æããã¾ãã
ã°ãã¼ãã«å¤æ°ãextern宣è¨ãã
次ã®ä¾ã®ããã«ããããã¡ã¤ã«å ã§ã°ãã¼ãã«å¤æ°ãå®ç¾©ãã¦ãã¾ãã¨ããã®ããããã¡ã¤ã«ãè¤æ°ã®ã½ã¼ã¹ã§ä½¿ç¨ããå ´åã«ååã®ã°ãã¼ãã«å¤æ°ã®å®ç¾©ãè¤æ°ã®ã½ã¼ã¹ãã¡ã¤ã«å ã§è¡ããã¦ãã¾ãã®ã§ããªã³ã¯æã«ã¨ã©ã¼ãçºçãã¦ãã¾ãã¾ãã
/* sub.h */ #ifndef SUB_H #define SUB_H int global_var = 0; int sub(void); #endif
/* sub.c */ #include "sub.h" int sub(void) { return global_var; }
#include <stdio.h> #include "sub.h" int main(void) { printf("global_var = %d\n", global_var); printf("sub() = %d\n", sub()); return 0; }
ãã®å ´åãsub.hå ã®ã°ãã¼ãã«å¤æ°ã®å®£è¨ã«externæå®åã追å ãã¾ãããããã¦ãããã¨ã§ãã°ãã¼ãã«å¤æ°ã®å®ç¾©ãä»ã®å ´æã§è¡ããã¦ãããã¨ãã³ã³ãã¤ã©ã«ç¥ããããã¨ãã§ãã¾ããããã¦ãã½ã¼ã¹ãã¡ã¤ã«å´ã§externãã¤ããªãã§ã°ãã¼ãã«å¤æ°ãå®ç¾©ãã¾ãã
/* sub.h */ #ifndef SUB_H #define SUB_H extern int global_var; int sub(void); #endif
/* sub.c */ #include "sub.h" int global_var = 0; int sub(void) { return global_var; }
ããããã¡ã¤ã«å ã§ã¯ä»ã®ããããã¡ã¤ã«ã®ã¤ã³ã¯ã«ã¼ããæä½éã«çãã
makeãªã©ã®ãã«ãã·ã¹ãã (å人çã«ã¯Rakeã使ã£ã¦ãã¾ã)ã§ããããã¡ã¤ã«ã®ä¾åé¢ä¿ãèªåã§è§£æ±ºããããã«ãã¦ããã¨ãããããã¡ã¤ã«ãå¤æ´ãããå ´åããã®ããããã¤ã³ã¯ã«ã¼ããã¦ããã½ã¼ã¹ãå
¨ã¦åã³ã³ãã¤ã«ããã¾ãã
ãã®ãããããããã¡ã¤ã«å
ã§ä»ã®ããããä¸å¿
è¦ã«ã¤ã³ã¯ã«ã¼ããã¦ããã¨ãããããã¡ã¤ã«ãæ´æ°ããéã«ã大éã®ã½ã¼ã¹ã³ã¼ããåã³ã³ãã¤ã«ãããã³ã³ãã¤ã«æéãå¢å¤§ãã¦ãã¾ãã¾ãã
ãã®ãããããããã¡ã¤ã«å
ã§ã¯ä»ã®ããããã¡ã¤ã«ã®ã¤ã³ã¯ã«ã¼ããæä½éã«çããä»ã®ããããã¡ã¤ã«ã¯ã½ã¼ã¹ãã¡ã¤ã«å
ã§ã¤ã³ã¯ã«ã¼ããã¾ãã
ããããã¡ã¤ã«å ã§ã®ã¤ã³ã¯ã«ã¼ããæ¸ããããã®ãã¯ããã¯ã¨ãã¦ãæ§é ä½ã®åæ¹å®£è¨ãããã¾ãã
ä¾ãã°ãhoge.hã¨fuga.hã®2ã¤ã®ããããã¡ã¤ã«ããããhoge.hã§Hogeæ§é ä½ãå®ç¾©ãããfuga.hã®Fugaæ§é ä½ã§Hogeæ§é ä½ã使ç¨ãã¦ããã¨ãã¾ãã
/* hoge.h */ #ifndef HOGE_H #define HOGE_H typedef struct hoge_t { int a; } Hoge; #endif
/* fuga.h */ #ifndef FUGA_H #define FUGA_H #include "hoge.h" typedef struct fuga_t { Hoge* hoge; } Fuga; #endif
ãã®ä¾ã®å ´åãFugaæ§é ä½ã®ã¡ã³ãã¯Hogeæ§é ä½ã®å®ä½ã§ã¯ãªããã¤ã³ã¿ãªã®ã§ãFugaæ§é ä½ãå®ç¾©ããã ããªãã°Hogeæ§é ä½ã®å
·ä½çãªå®ç¾©ã¯å¿
è¦ããã¾ããã
ãã®ãããhoge.hãã¤ã³ã¯ã«ã¼ããã代ããã«ã次ã®ä¾ã®ããã«åæ¹å®£è¨ã使ã£ã¦æ¸ããã¨ãåºæ¥ã¾ãã
/* fuga.h */ #ifndef FUGA_H #define FUGA_H struct hoge_t;ã/* åæ¹å®£è¨ */ typedef struct fuga_t { struct hoge_t* hoge; } Fuga;
ã¡ãªã¿ã«ãåã®æå ã®ç°å¢(i686-apple-darwin10-gcc-4.2.1)ã§ã¯åæ¹å®£è¨ããªãã¦ãã³ã³ãã¤ã«ã§ããã®ã§ãåæ¹å®£è¨ãå¿ è¦ã¨ããã®ã¯åã®åéãããããã¾ããã
ããããã¡ã¤ã«ã®ä¾åãæ¸ããããã«æ§æã工夫ãã
ããæ§é ä½ãå®ç¾©ããã¦ããããããå¤ãã®ç®æã§ä½¿ç¨ãã¦ããå ´åããã®æ§é ä½ãä¿®æ£ããéã«ããã®æ§é ä½ã使ç¨ãã¦ããå¤ãã®ã½ã¼ã¹ãåã³ã³ãã¤ã«ããå¿
è¦æ§ãã§ã¦ãã¾ãã
ãã®åé¡ã¯ãããããã¡ã¤ã«ãå
¬éç¨ã¨éå
¬éç¨ã®2ã¤ã«åãã¦ãæ§é ä½ã®å®ç¾©ãéå
¬éç¨ãããã«è¨è¿°ããã¨ããéç¨ãè¡ããã¨ã«ãã£ã¦è§£æ±ºãããã¨ãåºæ¥ã¾ãã
å ·ä½çã«ã¯ã次ã®ä¾ã®ããã«åãã¡ã¤ã«ãè¨è¿°ãã¾ãã
/* foo.h */ #ifndef FOO_H #define FOO_H /* foo.hã§ã¯æ§é ä½ã®åæ¹å®£è¨ã ããè¡ã£ã¦ãæ§é ä½ã®å®ç¾©ã¯è¡ããªã */ struct foo_t; typedef struct foo_t Foo; /* Fooæ§é ä½ã¸ã®ã¢ã¯ã»ã¹ã¯å ¨ã¦å°ç¨ã®APIé¢æ°çµç±ã§è¡ãããã«ããã ãããã®é¢æ°ã§ã¯ãFooã®ãã¤ã³ã¿ã第ä¸å¼æ°ã«åãã */ Foo* Foo_alloc(); void Foo_do_something(Foo* obj, int arg1, int arg2); void Foo_free(Foo *obj); #endif
/* private/foo_p.h */ #ifndef FOO_P_H #define FOO_P_H #include "foo.h" /* Fooæ§é ä½ã®å®ç¾©ã¯éå ¬éãããå ã«è¨è¿°ãã */ struct foo_t { int a; int b; }; #endif
/* foo.c */ #include "private/foo_p.h" /* foo.cã§ã¯éå ¬éããããã¤ã³ã¯ã«ã¼ããã¦ãããã ãã®ä»ã®ã½ã¼ã¹ã§ã¯å ¬éãããã®ã¿ãã¤ã³ã¯ã«ã¼ãããã */ /* Foo_allocçã®é¢æ°ã®ã³ã¼ãããã®ãã¡ã¤ã«ã«æ¸ã */
ãã®ãããªæ§æã«ãããã¨ã«ãã£ã¦ãFooæ§é ä½ã®ä¿®æ£ãè¡ã£ã¦ããfoo.hãã¤ã³ã¯ã«ã¼ããã¦ããã½ã¼ã¹ã®åã³ã³ãã¤ã«ã¯å¿
è¦ãªããªãã¾ãã
ã¾ããæ§é ä½ã¸ã®ã¢ã¯ã»ã¹ãå
¨ã¦å°ç¨ã®APIé¢æ°çµç±ã§è¡ããã¨ãå¼·å¶ãããã¨ãã§ããã®ã§ãã«ãã»ã«åãä¿é²ãããã¨ãã§ããã¨ããå¥ã®ã¡ãªãããããã¾ãã