Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking freetype with cmake

I'm having troubles with linking freetype 2 under linux using cmake when building a C++11 project with an extern C library.

With cmake and freetype 2 I basically have 2 options :

  • use the utility freetype-config like freetype-config --libs
  • use the FindFreetype cmake module

Now I'm trying to implement the second option and I'm not very skilled with cmake nor I understand the logic of it.

My problem is the linking phase, I have no idea how to do that properly plus this module is not as complete as the result of freetype-config --libs which really includes all the libraries and flags that I need, and not just the path of a file; so I'm assuming that I have to do the same for zlib and libpng.

CMakeLists.txt

cmake_minimum_required (VERSION 2.6)
project (FreetypeTutorials1)

include(FindFreetype)
include_directories(${FREETYPE_INCLUDE_DIRS})

SET(CMAKE_CXX_FLAGS "-O2 -std=c++11")

SET(CMAKE_EXE_LINKER_FLAGS "-v -lfreetype")

add_executable( demo "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")

./src/main.cpp ( just some random code so I have something to feed to the compiler )

extern "C" {
#include <ft2build.h>
#include FT_FREETYPE_H
}
#include <iostream>
int main()
{
  FT_Library library;
  auto error = FT_Init_FreeType(&library);
  if (error)
  {
    std::cout << "An error !\n";
  }
}
like image 263
user2485710 Avatar asked May 27 '14 11:05

user2485710


1 Answers

To load a module like FindFreetype.cmake you need to use it in cmake with the find_package-command. The first argument is the package name. The "Find" of its corresponding filename is added automatically by cmake.

While include might work with find_package you can add some flags. For example, as shown below, REQUIRED, to make cmake fail when freetype wasn't found.

Additionally linking with cmake should be done with the command target_link_libraries.

This is how I would write you CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (FreetypeTutorials1)

find_package(Freetype REQUIRED)

SET(CMAKE_CXX_FLAGS "-O2 -std=c++11")

SET(CMAKE_EXE_LINKER_FLAGS "-v")

add_executable( demo src/main.cpp) # CMAKE_CURRENT_SOURCE_DIR is implicit here
target_link_libraries(demo ${FREETYPE_LIBRARIES})
target_include_directories(demo PRIVATE ${FREETYPE_INCLUDE_DIRS})

target_link_libraries is platform independent whereas '-lfreetype in CMAKE_EXE_LINKER_FLAGS is not.

The CMakeLists.txt will work on other platforms where freetype is available.

(Edit 2019: use target_include_directories() instead of include_directories()

like image 145
Patrick B. Avatar answered Sep 24 '22 14:09

Patrick B.