Linux – Undefined reference for FreeType 2

Undefined reference for FreeType 2… here is a solution to the problem.

Undefined reference for FreeType 2

Undefined reference error 2. When building a simple example with FreeType, I get undefined reference error 2

gcc `/usr/bin/freetype-config --cflags`  `/usr/bin/freetype-config --libs` a.c 
/tmp/ccuSpdkr.o: In function `main':
a.c:(.text+0x10): undefined reference to `FT_Init_FreeType'
collect2: error: ld returned 1 exit status

I’m on Ubuntu 12.10 x64. The packages libfreetype6 and libfreetype6-dev are installed.

File a.c is:

#include <stdio.h>
#include <ft2build.h> 
#include FT_FREETYPE_H

int main() {
  FT_Library library;
  FT_Init_FreeType( &library );
  return 0;
}

I tried two-step compilation to make sure everything is on 64bit :

> gcc -c `/usr/bin/freetype-config --cflags` a.c
> file a.o
a.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
> /usr/bin/freetype-config --cflags
-I/usr/include/freetype2
> /usr/bin/freetype-config --libs
-L/usr/lib/x86_64-linux-gnu -lfreetype -lz
> file /usr/lib/x86_64-linux-gnu/libfreetype.so.6.9.0
/usr/lib/x86_64-linux-gnu/libfreetype.so.6.9.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]= 0x8ed223c2650372fc88b41fd348a72f03329adefa, stripped

What am I missing?

Solution

You should place the linker flag after the target file and/or source file; And not

gcc `freetype-config --libs` a.c

Write

gcc a.c `freetype-config --libs`

Related Problems and Solutions