詳細モードを実装しています。これが私がやろうとしていることです:詳細を必要とするファイルにそのファイルのみを含める必要があるように、グローバル変数VERBOSE(verbose.h内)を定義します。例えば :
詳細.h:
void setVerbose(int test);
詳細.c:
#include "verbose.h"
// define VERBOSE if called
void setVerbose(int test) {
if (test) {
#ifndef VERBOSE
#define VERBOSE
#endif
}
}
ポイント.h:
typedef struct Point Point;
struct Point {
int x, y;
};
void printPoint(Point *point);
point.c:
#include "point.h"
#include "verbose.h"
void printPoint(Point *point) {
#ifdef VERBOSE
printf("My abscissa is %d\n", point->x);
printf("My ordinate is %d\n", point->y);
#endif
printf("[x,y] = [%d, %d]\n", point->x, point->y);
}
そしてメイン:
main.c:
#include "verbose.h"
#include "point.h"
int main(int argc, char *argv[]) {
if (argc >= 2 && !strcmp(argv[1], "-v"))
setVerbose(1);
Point *p = init_point(5,7);
printPoint(p);
return 0;
}
実行可能ファイルは次のように作成されています:
$ gcc -o test main.c point.c verbose.c
必要な出力は次のとおりです。
$ ./test
[x,y] = [5, 7]
$ ./test -v
My abscissa is 5
My ordinate is 7
[x,y] = [5, 7]
問題は、printPoint() を呼び出すときに、point.c で VERBOSE が定義されていないように見えることです。