헤더를 만든다


// sample.h #ifndef SAMPLE_H #define SAMPLE_H int sample_func(void); static const struct { int (*func)(void); } sample = { sample_func }; #endif


sample이 namespace 역할을 할 것이다


C에는 scope resolution operator (::) 가 없기에 .을 쓴다


구현을 다른 파일에 작성한다


// sample.cc #include "sample.h" int sample_func(void) { return 42; }


구현을 마쳤으니 사용해보자


// main.cc #include "sample.h" #include <stdio.h> int main(void) { printf("Result: %dn", sample.func()); return 0; }


컴파일한다


? clang -c sample.cc ? clang -c main.cc ? clang -o main main.o sample.o ? ./main Result: 42


성공적이다