FreeType库和“对FT_Init_FreeType的未定义引用"; [英] FreeType library and "Undefined reference to FT_Init_FreeType"
问题描述
这是我来自PHP的第一次使用C/C ++的经历(对我来说比较容易).我正在按照本教程使用FreeType库编写一个简单的脚本.以下编译就可以了:
Coming from PHP, this is my first experience with C/C++ (so go easy on me). I'm following this tutorial to write a simple script using the FreeType library. The following compiles just fine:
#include <ft2build.h>
#include FT_FREETYPE_H
main() {
FT_Library library;
FT_Face face;
}
这告诉我FreeType库很容易被编译器使用.但是,一旦尝试使用任何方法,事情都会中断.例如,使用以下脚本:
This tells me that the FreeType library is readily available to the compiler. However, things break once I try to use any methods. For example, take the following script:
#include <ft2build.h>
#include FT_FREETYPE_H
main() {
int error;
FT_Library library;
error = FT_Init_FreeType(&library);
if (error) {}
FT_Face face;
error = FT_New_Face(library, "/usr/share/fonts/truetype/arial.ttf", 0, &face);
if (error == FT_Err_Unknown_File_Format) {
printf("Font format is unsupported");
} else if (error) {
prinft("Font file is missing or corrupted");
}
}
此脚本在编译时产生以下错误:
This script produces the following error upon compiling:
#gcc render.c -I/usr/include/freetype2
/tmp/cc95255i.o: In function `main':
render.c:(.text+0x10): undefined reference to `FT_Init_FreeType'
render.c:(.text+0x30): undefined reference to `FT_New_Face'
collect2: ld returned 1 exit status
有什么想法吗?
推荐答案
这些是链接错误.如果演示中包含Makefile,则最好使用该文件.否则,您需要在编译命令行中添加-L和-l选项,以便编译器(实际上是在后台由编译器调用的链接器)知道在哪里可以找到FreeType库.
Those are link errors. If they include a Makefile with the demo you are better off using that. Otherwise, you need to add -L and -l options to your compile command line so the compiler (actually the linker, which gets invoked by the compiler behind the scene) knows where to find the FreeType library.
-L选项提供该库代码所在的路径.例如
The -L option gives the path to where the code for the library exists. For example
-L/usr/local/lib
-l选项给出库的名称.用-l选项命名的库以缩短的形式指定,也就是说,您在前面省略了"lib",而在后面省略了".a".因此,例如,如果FreeType库存储在文件libfreetype.a中,它将在-l选项中显示为
And the -l option gives the name of the library. The library named with the -l option is specified in a shortened form, that is you leave off the "lib" in the front and the ".a" in the back. So for example, if the FreeType library was stored in file libfreetype.a , it would show in the -l option as
-lfreetype
例如:
gcc render.c -I/usr/include/freetype2 -L/usr/local/lib -lfreetype
这篇关于FreeType库和“对FT_Init_FreeType的未定义引用";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!