我在 Windows 7 中使用 MinGW 进行编译,使用 Code::Blocks 作为 IDE。一切都应该是最新的。我注意到有许多类似的问题,但那些似乎是关于链接动态库,而不是编译静态库。
我正在尝试使用 FTGL 来在 OpenGL 中使用文本。FTGL 需要安装 FreeType 2。我下载了 FreeType 源代码,并按照 INSTALL.GNU 说明使用 mingw32-make.exe 从 CMD.EXE 构建静态库。
如说明所示,我从 FreeType 安装的基本目录运行 mingw32-make。控制台输出符合预期:
D:\freetype-2.4.4>mingw32-make
FreeType build system -- automatic system detection
The following settings are used:
platform win32
compiler gcc
configuration directory .\builds\win32
configuration rules .\builds\win32\w32-gcc.mk
If this does not correspond to your system or settings please remove the file
'config.mk' from this directory then read the INSTALL file for help.
Otherwise, simply type 'make' again to build the library.
or 'make refdoc' to build the API reference (the latter needs python).
Could Not Find D:\freetype-2.4.4\objs\ftmodule.h
Generating modules list in ./objs/ftmodule.h...
"* module: truetype (Windows/Mac font files with extension *.ttf or *.ttc)"
"* module: type1 (Postscript font files with extension *.pfa or *.pfb)"
"* module: cff (OpenType fonts with extension *.otf)"
"* module: cid (Postscript CID-keyed fonts, no known extension)"
"* module: pfr (PFR/TrueDoc font files with extension *.pfr)"
"* module: type42 (Type 42 font files with no known extension)"
"* module: winfnt (Windows bitmap fonts with extension *.fnt or *.fon)"
"* module: pcf (pcf bitmap fonts)"
"* module: bdf (bdf bitmap fonts)"
"* module: sfnt (helper module for TrueType & OpenType formats)"
"* module: autofit (automatic hinting module)"
"* module: pshinter (Postscript hinter module)"
"* module: raster (monochrome bitmap renderer)"
"* module: smooth (anti-aliased bitmap renderer)"
"* module: smooth (anti-aliased bitmap renderer for LCDs)"
"* module: smooth (anti-aliased bitmap renderer for vertical LCDs)"
"* module: psaux (Postscript Type 1 & Type 2 helper module)"
"* module: psnames (Postscript & Unicode Glyph name handling)"
done.
看到平台和编译器都正确,我再次运行 make 命令:
D:\freetype-2.4.4>mingw32-make
(bunch of calls, building objects then linking them, until:)
ar: creating objs/freetype.a
这在目录 D:\freetype-2.4.4\objs 中生成了文件 freetype.a。
然后我按照 Code::Blocks 中的步骤将库链接到项目,添加了一段代码示例来测试 FTGL,并尝试构建它。
我有两个错误,这取决于我如何尝试做事。
如果我为链接器提供正确的目录 (-LD:\freetype-2.4.4\objs),并使用 -lfreetype 进行链接,那么一切都很好,直到 ld.exe 退出并显示标题中的错误,找不到 -lfreetype。
如果我没有提供正确的目录,我会得到同样的错误。找不到图书馆。
如果我将文件名 freetype.a 更改为 libfreetype.a,并提供正确的目录,则会得到几个未定义的引用。
obj\Release\CApp_OnRender.o:CApp_OnRender.cpp:(.text+0x250): undefined reference to `FTPixmapFont::FTPixmapFont(char const*)'
obj\Release\CApp_OnRender.o:CApp_OnRender.cpp:(.text+0x258): undefined reference to `FTFont::Error() const'
obj\Release\CApp_OnRender.o:CApp_OnRender.cpp:(.text+0x274): undefined reference to `FTFont::FaceSize(unsigned int, unsigned int)'
obj\Release\CApp_OnRender.o:CApp_OnRender.cpp:(.text+0x308): undefined reference to `FTFont::Render(char const*, int, FTPoint, FTPoint, int)'
obj\Release\CApp_OnRender.o:CApp_OnRender.cpp:(.text+0x316): undefined reference to `FTPixmapFont::~FTPixmapFont()'
obj\Release\CApp_OnRender.o:CApp_OnRender.cpp:(.text+0x328): undefined reference to `FTPixmapFont::~FTPixmapFont()'
collect2: ld returned 1 exit status
如果我提供了错误的目录,我会再次收到上一个错误,即找不到库。
我觉得奇怪的是,当按照安装文件中的说明进行操作时,make 会生成一个名称不以 lib 开头的库。我检查了所有其他 .a 文件都以 lib- 开头。似乎链接器正在寻找文件 libfreetype.a。那么为什么 make 会创建 freetype.a 呢?这是对我用来编译程序的库使用相同的编译器。
无论如何,不知道从这里去哪里。我曾想过使用作为 MinGW 包一部分的 MSys shell,但我对它没有太多经验,并且 INSTALL.UNIX 中的说明假设您知道更明显的命令。他们提到使用 ./configure [options],但没有提及哪些选项可能是可取的。对于对系统有更多了解的人来说可能很明显,但 MSys 是我第一次使用 sh。
对于此事,我将不胜感激。