Search code examples
c++cmakeskia

Importing header files and linking .a file in CMake


Trying to put Skia in my CMake project. How do I tell CMake to link my executable against libskia.a and use the header files inside ext/skia so that I can include them like so?

#include <skia/subdirectory/headerfile.h>

My project structure is currently the following:

.
├── CMakeLists.txt
├── CMakeLists.txt.user
├── ext
│   ├── libskia.a
│   └── skia
│       └── <subdirectories>
│           └── <header files>.h
└── src
    └── main.cpp

and my CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(project LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")

add_executable(project src/main.cpp)

Solution

  • In your primary CMakeLists.txt file you would simply add the following:

    target_link_libraries(project skia)
    

    If CMake cannot find the library, you can either do:

    target_link_libraries(project /full/path/to/libskia.a)
    

    or:

    link_directories(/path/to/libraries)
    target_link_libraries(project skia)