0
私はヘッダファイルを使用する新しい構造にコードを変換しています(私はよくそれらを使用しないことに注意してください)、コンパイルするのが難しいです(gcc --std = c11を使用)。私は問題が何であるかを知っていると思う、私はちょうどそれを修正するコードを実装する方法を知らない。構造体と列挙型を使用するヘッダーはお互いにありますか?
ここに問題を示すコードがあります。
main.cの
#include "file1.h"
#include "file2.h"
#include "header.h"
int main()
{
BigStruct bigStruct;
DoStuff(&bigStruct);
OtherStuff(&bigStruct);
return 0;
}
file1.h
#ifndef FILE1_H
#define FILE1_H
#include "header.h"
typedef enum TagMyEnum
{
MY_THING1,
MY_THING2,
MY_ENUM_END
}
MyEnum;
typedef struct TagMyStruct
{
// Variables.
}
MyStruct;
typedef struct TagBigStruct BigStruct;
void DoStuff(BigStruct *bigStruct);
#endif
file2.h
#ifndef FILE2_H
#define FILE2_H
#include "header.h"
typedef enum TagOtherEnum
{
OTHER_THING1,
OTHER_THING2,
OTHER_ENUM_END
}
OtherEnum;
typedef struct TagOtherStruct
{
// Variables.
}
OtherStruct;
typedef struct TagBigStruct BigStruct;
void OtherStuff(BigStruct *bigStruct);
#endif
がheader.h
#ifndef HEADER_H
#define HEADER_H
#include "file1.h"
#include "file2.h"
typedef struct TagMyStruct MyStruct;
typedef struct TagOtherStruct OtherStruct;
typedef struct TagBigStruct
{
MyStruct myStruct[MY_ENUM_END];
OtherStruct otherStruct[OTHER_ENUM_END];
}
BigStruct;
#endif
コンパイラがこれを教えています。
In file included from file1.h:4:0,
from main.c:2:
header.h:12:23: error: ‘MY_ENUM_END’ undeclared here (not in a function)
MyStruct myStruct[MY_ENUM_END];
私はこのエラーを理解していると思いますが、ソリューションの実装方法はわかりません。
あまり簡単ではありませんが、より正確な修正は、3つのファイル間のこのひどい依存関係を取り除くことです。それらを結合するか、適切に分離してください。 – StoryTeller
ありがとうございます。私はfile1.hとfile2.hから#include "header.h"を削除し、それも問題を解決しました。これはより良い解決策であるか、それともよりエレガントですか? –
@StoryTellerはい、確かです。私はしばらく書いていました。 – LPs