04/07/04
何故、headerファイルを作成するか?でヘッダファイルの意味を説明したが、実装して再確認してみる。
このサンプルは、変数、関数、構造体を外部に公開するためにヘッダファイルに記述している。
test_types.h
/* global define */
extern unsigned char BYTE_CHECKER;
struct ADDRESS {
char name[32];
char address[128];
int sex;
};
typedef struct ADDRESS t_ads;
test_types.c /* global define */ unsigned char BYTE_CHECKER = '0';
test_extern.h #ifndef _TEST_EXTERN_H_ #define _TEST_EXTERN_H_ #include <stdio.h> #endif extern void put_hello();
test_extern.c
#include "test_extern.h"
void put_hello() {
printf("hello world\n");
}
test_main.c
#include <stdio.h>
#include <test_types.h>
#include <test_extern.h>
int main(int argc, char *argv[] ) {
/* test_types.h sample */
t_ads aaaa;
strncpy(aaaa.name, "Satoshi", sizeof("Satoshi"));
strncpy(aaaa.address, "Japan", sizeof("Japan"));
printf("%s\n", aaaa.name);
/* test_extern.h sample */
put_hello();
printf("%c", BYTE_CHECKER);
return 0;
}
$ gcc -c test_extern.c -I. $ gcc -c test_types.c -I. $ gcc -o run1 -I. test_main.c test_extern.o test_types.o
$ ./run1 Satoshi hello world 0