Location>code7788 >text

CMake build study notes 6 - giflib library build

Popularity:371 ℃/2024-08-26 13:49:31

The previous builds of zlib, libpng, libjpeg and libtiff all provide files, so they can be built via CMake. However, there are dependent libraries that do not have files, that is, there is no official way to build CMake, such as this article to say GIFLIB. GIFLIB is an open source C library for processing GIF (Graphics Interchange Format) image files.

GIFLIB is a typical open source library based on Linux environment, using Makefile to organize the project configuration file, and build it by make tool in Linux environment. So how to build it under Windows? One option is to install MSYS2 (Minimal SYStem 2), which is a Windows-based Unix-like shell environment capable of providing a cross-platform GNU toolchain including the make tool, called MinGW (Minimalist GNU for Windows). However, this solution is cumbersome, and the key thing is that there may be binary compatibility problems between MinGW-compiled libraries and MSVC-compiled dynamic libraries under Windows.

Another option is that if the library is not a large amount of code, you can organize your own files for compilation, here I found a god on the Internet to organize the files of the GIFLIB project:

# Output cmake version hints
message(STATUS "The CMAKE_VERSION is ${CMAKE_VERSION}.")

# Minimum required version of cmake
cmake_minimum_required (VERSION 3.10)

# Project name, version, language
project(giflib VERSION 5.2.2)

# Support for current directory
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Determine compiler type
message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")

# Source code file
set(GIF_SRC dgif_lib.c egif_lib.c gif_err.c gif_font.c gif_hash.c )
set(GIF_HEADER gif_hash.h gif_lib.h gif_lib_private.h )
#set(GIF_UTILSRC )
#set(GIF_UTILHEADER )

# Dynamic library prefixes and suffixes
IF(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(LibraryPrefix lib)
    set(LibraryPostfix so)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")
set(LibraryPrefix )
    set(LibraryPostfix lib)
ENDIF()

# Add the source code to the executable for this project.
add_library(${PROJECT_NAME} SHARED ${GIF_SRC} ${GIF_HEADER})

# Determine compiler type
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    message(">> using Clang")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    message(">> using GCC")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
    message(">> using Intel C++")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    message(">> using Visual Studio C++")
    add_compile_options(/utf-8 /wd4996)
    set_property(TARGET ${PROJECT_NAME} PROPERTY LINK_FLAGS
    /DEF:"${CMAKE_SOURCE_DIR}/")
else()
    message(">> unknow compiler.")
endif()

# TODO: Add a test if needed.

# Install the header files into the include directory
install(FILES gif_lib.h DESTINATION include)

# Install the library files into the lib directory
install(TARGETS ${PROJECT_NAME}
        LIBRARY DESTINATION lib # for shared libraries
        ARCHIVE DESTINATION lib # For static libraries.
        RUNTIME DESTINATION bin # for executables
)

Based on this, the author organized GIFLIB'sVersion 5.2.2, the key build instructions are shown below:

# Configuring CMake
cmake ... -G "$Generator" -A x64 -DCMAKE_CONFIGURATION_TYPES=RelWithDebInfo -DCMAKE_INSTALL_PREFIX="$InstallDir"

# Build phase, specify build type
cmake ---build . --config RelWithDebInfo

# Installation phase, specifying the build type and installation target
cmake --build . --config RelWithDebInfo --target install