Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FreeType library and "Undefined reference to FT_Init_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;
}

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

Any ideas?

like image 428
David Jones Avatar asked Sep 07 '12 05:09

David Jones


1 Answers

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.

The -L option gives the path to where the code for the library exists. For example

-L/usr/local/lib  

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

e.g.:

gcc render.c -I/usr/include/freetype2 -L/usr/local/lib -lfreetype
like image 74
Scooter Avatar answered Oct 25 '22 06:10

Scooter