私はCでモジュラープログラミングを始めたばかりです。多くのエラーが発生しているため、インクルージョンで何か問題がconflicting types for 'functionName'
発生previous declaration of 'functionName' was here
していると思います。インクルージョンガードを設置しました。
Cでのモジュラープログラミング、特にインクルージョンがどのように機能するかを説明する明確なチュートリアルを知っていますか?
更新:問題を切り分けようとしました。リクエストに応じて、ここにいくつかのコードがあります。
更新2:更新されたコードは以下のとおりです。エラーも更新されました。
/*
* main.c
*/
#include <stdio.h>
#include "aStruct.h"
int main() {
aStruct asTest = createStruct();
return 0;
}
/*
* aStruct.h
*/
#ifndef ASTRUCT_H_
#define ASTRUCT_H_
struct aStruct {
int value1;
int value2;
struct smallerStruct ssTest;
};
typedef struct aStruct aStruct;
aStruct createStruct();
#endif /* ASTRUCT_H_ */
/*
* smallerStruct.h
*/
#ifndef SMALLERSTRUCT_H_
#define SMALLERSTRUCT_H_
struct smallerStruct {
int value3;
};
typedef struct smallerStruct smallerStruct;
smallerStruct createSmallerStruct();
#endif /* SMALLERSTRUCT_H_ */
/*
* aStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
#include "aStruct.h"
aStruct createStruct() {
aStruct asOutput;
printf("This makes sure that this code depends on stdio.h, just to make sure I know where the inclusion directive should go (main.c or aStruct.c).\n");
asOutput.value1 = 5;
asOutput.value2 = 5;
asOutput.ssTest = createSmallerStruct();
return asOutput;
}
/*
* smallerStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
smallerStruct createSmallerStruct() {
smallerStruct ssOutput;
ssOutput.value3 = 41;
return ssOutput;
}
これにより、次のエラーメッセージが生成されます。
aStruct.h:10で
- フィールド'ssTest'のタイプが不完全です
main.c:8で
- 未使用の変数`asTest' (これは理にかなっています)