【问题标题】:Why does including the freetype header files in my C header file produce an error, but can be included in the .c file?为什么在我的 C 头文件中包含 freetype 头文件会产生错误,但可以包含在 .c 文件中?
【发布时间】:2017-06-12 07:58:30
【问题描述】:

下面是一个 engine_text.h 文件,我试图在其中包含 FreeType 库。我想在这个头文件中包含这些库,以便我可以定义可以在其他项目文件中使用的结构。

#ifndef H_ENGINE_TEXT
#define H_ENGINE_TEXT

#include <ft2build.h>
#include FT_FREETYPE_H

typedef struct
EngineFont
{

    FT_Library Library;
    FT_Face Face;

} EngineFont;

void
LoadFont(char* FontPath);

#endif

但是,在尝试编译它时,我从 Visual Studio cl.exe 编译器收到以下错误:

engine_main.c
h:\Engine\code\freetype/freetype.h(947): error C2208: 'FT_Face_InternalRec_ *': no members defined using this type
h:\Engine\code\freetype/freetype.h(1317): error C2208: 'FT_Size_InternalRec_ *': no members defined using this type
h:\Engine\code\freetype/freetype.h(1549): error C2208: 'FT_Slot_InternalRec_ *': no members defined using this type

请注意,错误来自engine_main.c,因为它包含engine_text.h 头文件。现在无论出于何种原因,如果我将 FreeType 包含在 engine_text.c 文件中而不是头文件中,程序编译并运行没有问题。但是当然我不能定义结构,这是我必须做的。

engine_main.c 看起来像这样:

#include "engine_main.h"

#include <SDL/SDL.h>
#include <GL/glew.h>

#include "engine_sdl.h"
#include "engine_shader.h"
// The problematic one:
#include "engine_text.h"

为什么当包含在另一个头文件中时会导致这么多问题?我也不太确定 C2208 错误真正向我表明了什么。

【问题讨论】:

  • 您能否在它编译的变体中提供engine_main.c
  • 我在上面添加了engine_main.c。 @StephanLechner
  • 这个版本不行吧? engine_main.c 在它工作的变体中看起来如何?
  • 抱歉,我误解了你的意思。 engine_main.c 在有效的那个中看起来是一样的。在有效的变体中,engine_text.c 文件中的 2 个包含语句被放置在 engine_text.h 文件中(因此必须删除 EngineFont 结构)。
  • 所以你没有任何 EngineFont 结构可以工作的版本,对吧?

标签: c visual-studio visual-c++ compiler-errors compilation


【解决方案1】:

我认为 FT_Library 和 FT_Face 类型的行为不符合预期;但是通过将其打包到struct 中,“真正的”问题可能会被 C2208 所覆盖,这只是表明您定义了一个没有成员的结构(我认为是误导)。

只是为了找出问题,请尝试以下engine_main.c

#include "engine_main.h"

#include <SDL/SDL.h>
#include <GL/glew.h>

#include "engine_sdl.h"
#include "engine_shader.h"

#include <ft2build.h>
#include FT_FREETYPE_H

FT_Library Library;
FT_Face Face;

看看这些声明是否有效。那我们继续下一步吧。

【讨论】:

  • 当我将 engine_main.c 更改为此变体时,我收到了相同的 C2208 错误。
  • 你已经删除了你的 EngineFont 结构?
  • 那么问题出在FT_Library本身。您是否有任何使用 FT_Library 的最小程序有效,而无需在结构等方面付出所有努力?可能有任何与图书馆一起出现的例子吗?
  • 如果我只将 FT 代码隔离到 engine_text.c,我可以得到一个简单的小程序来运行和工作。这是一个例子:
  • 无法让格式化工作,所以我把它扔进了一个粘贴箱:pastebin.com/rbKHHx3K
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
相关资源
最近更新 更多