Search code examples
c++cmakemingwfreetype2

Freetype2 not linking on Windows correctly


I've been fighting freetype2 for a week trying to get it to work on Windows 32 bit but it just won't. My CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.0.0)
set(CMAKE_CXX_STANDARD 17)

project(template-project) # change the name here

file(GLOB_RECURSE SOURCE_FILES src/*.cpp)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})

list(APPEND CMAKE_PREFIX_PATH "D:/Installs/ProgFiles/glew")

find_package( OpenGL REQUIRED )

include_directories( ${OPENGL_INCLUDE_DIRS} )

# this is so stupid
set(CMAKE_SIZEOF_VOID_P 4)

if (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
  # ensure 32 bit on clang
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -target i386-pc-windows-msvc")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target i386-pc-windows-msvc")
  add_definitions("--target=i386-pc-windows-msvc")
endif()

set(FT_DISABLE_HARFBUZZ TRUE)

target_include_directories(${PROJECT_NAME} PRIVATE
  libraries/minhook/include
  libraries/gd.h/include
  libraries/gd.h/
  libraries/imgui
  libraries/glad/include
  libraries/stb
  libraries/freetype2/include
)

add_subdirectory(libraries/minhook)
add_subdirectory(libraries/cocos-headers)
add_subdirectory(libraries/glfw)
add_subdirectory(libraries/glm)
add_subdirectory(libraries/freetype2)

target_link_libraries( ${PROJECT_NAME} ${OPENGL_LIBRARIES} glfw )
if( MSVC )
    if(${CMAKE_VERSION} VERSION_LESS "3.6.0") 
        message( "\n\t[ WARNING ]\n\n\tCMake version lower than 3.6.\n\n\t - Please update CMake and rerun; OR\n\t - Manually set 'GLFW-CMake-starter' as StartUp Project in Visual Studio.\n" )
    else()
        set_property( DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT GLFW-CMake-starter )
    endif()
endif()

target_link_libraries(${PROJECT_NAME} opengl32.lib minhook cocos2d rpcrt4.lib glm ${PROJECT_SOURCE_DIR}/libraries/freetype2/objs/Win32/Release/freetype.lib)

The biggest issue in the output are these lines:

[cmake] -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 
[cmake] -- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) 
[cmake] -- Could NOT find PNG (missing: PNG_LIBRARY PNG_PNG_INCLUDE_DIR) 
[cmake] -- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) 
[cmake] -- Could NOT find BZip2 (missing: BZIP2_LIBRARIES BZIP2_INCLUDE_DIR) 
[cmake] -- Could NOT find BrotliDec (missing: BROTLIDEC_INCLUDE_DIRS BROTLIDEC_LIBRARIES) 

I am on windows and completely unsure how to fix these. I've tried installing zlib via mingw32, and I've tried linking it the same way I do things like minhook and glad to no avail.


Solution

  • I recommend a different approach than manual downloading of opensource dependencies when using MinGW. Instead of searching for individual binary downloads switch to use msys2 to install MinGW and use the package management of msys2 to all of your dependent open source libraries.

    The first step is to remove your current MinGW install so you have no conflicting / possibly incompatible MinGW dlls in your PATH that may cause you problems in the future when executing your programs.

    After that install msys2: How to install MinGW-w64 and MSYS2?

    Then to install your dependencies for 32 bit open the mingw32 terminal which by default is installed in "C:\msys64\mingw32.exe" and use the package manager of msys2 pacman to install the dependent packages.

    The web page for msys2 has a convenient package search feature at the top center of this page: https://packages.msys2.org/queue

    Lets start with zlib from your dependencies. Type in zlib in the search box and press search. Type mingw-w64-zlib then look for i686 packages for mingw to find the correct package for 32 bit mingw. I found the following link for zlib for mingw32 has the following page: https://packages.msys2.org/package/mingw-w64-i686-zlib?repo=mingw32

    The install instructions for that are listed in the center of the page: pacman -S mingw-w64-i686-zlib

    so copy this command to the mingw32 terminal:

    JMDLAPTOP1+dresc@JMDLAPTOP1 MINGW32 ~
    # pacman -S mingw-w64-i686-zlib
    resolving dependencies...
    looking for conflicting packages...
    
    Packages (1) mingw-w64-i686-zlib-1.2.13-2
    
    Total Download Size:   0.10 MiB
    Total Installed Size:  0.39 MiB
    
    :: Proceed with installation? [Y/n]
    

    Press Y to install this package.

    :: Proceed with installation? [Y/n] y
    :: Retrieving packages...
     mingw-w64-i686-zlib-1.2.13...   102.8 KiB   126 KiB/s 00:01 [###############################] 100%
    (1/1) checking keys in keyring                               [###############################] 100%
    (1/1) checking package integrity                             [###############################] 100%
    (1/1) loading package files                                  [###############################] 100%
    (1/1) checking for file conflicts                            [###############################] 100%
    (1/1) checking available disk space                          [###############################] 100%
    :: Processing package changes...
    (1/1) installing mingw-w64-i686-zlib                         [###############################] 100%
    
    JMDLAPTOP1+dresc@JMDLAPTOP1 MINGW32 ~
    

    Continue a similar process for the other dependent packages.