Use Skia in Clion on OSX

2

I want to use Skia and SDL2 in Clion on OSX. I installed SDL2 successfully and test it in a single project in Clion, it worked well. And then I followed the tutorial on Skia's official site. I compiled it and put it in my Clion project, it didn't work! I checked several times but no ideas about why it failed.

Here is my config about the tested project.

The command that I built Skia:

$ cd skia
$ python tools/git-sync-deps
$ bin/gn gen out/Release --args="is_official_build=true is_component_build=true skia_use_system_expat=false skia_use_system_icu=false skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false skia_use_system_zlib=false extra_cflags_cc=[\"-frtti\"]"
$ ninja -C out/Release skia

It generates two static library libskia.a and libpathkit.a.

This screenshot shows how I config my project in Clion.

Here is my CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(skia_tutorial)

set(CMAKE_CXX_STANDARD 11)

set(SKIA_SDK "${CMAKE_SOURCE_DIR}/SKIA_SDK")

include_directories(
    ${SKIA_SDK}/include
    ${SKIA_SDK}/include/android
    ${SKIA_SDK}/include/atlastext
    ${SKIA_SDK}/include/c
    ${SKIA_SDK}/include/codec
    ${SKIA_SDK}/include/config
    ${SKIA_SDK}/include/core
    ${SKIA_SDK}/include/effects
    ${SKIA_SDK}/include/encode
    ${SKIA_SDK}/include/gpu
    ${SKIA_SDK}/include/gpu/gl
    ${SKIA_SDK}/include/gpu/mock
    ${SKIA_SDK}/include/gpu/mtl
    ${SKIA_SDK}/include/gpu/vk
    ${SKIA_SDK}/include/pathops
    ${SKIA_SDK}/include/ports
    ${SKIA_SDK}/include/private
    ${SKIA_SDK}/include/svg
    ${SKIA_SDK}/include/utils
    ${SKIA_SDK}/include/utils/mac
    ${SKIA_SDK}/include/views)


add_executable(skia_tutorial main.cpp)

target_link_libraries(skia_tutorial ${SKIA_SDK}/libskia.a)
target_link_libraries(skia_tutorial ${SKIA_SDK}/libpathkit.a)

set(CMAKE_MODULE_PATH "/usr/local/Cellar/sdl2/2.0.8/lib/cmake")
find_package(SDL2 REQUIRED)
if (SDL2_FOUND)
    include_directories(${SDL2_INCLUDE_DIR})
    target_link_libraries(skia_tutorial ${SDL2_LIBRARIES})
endif()

Here is my main.cpp.

#include <SkBitmap.h>
#include <SkTypeface.h>
#include <SkCanvas.h>
#include <SDL2/SDL.h>

//RGBA
struct RGBA {
    //Red
    Uint32 rmask = 0x00ff0000;
    //Green
    Uint32 gmask = 0x0000ff00;
    //Blue
    Uint32 bmask = 0x000000ff;
    //Alpha
    Uint32 amask = 0xff000000;
};


SkBitmap draw(int w, int h) {

    SkBitmap bitmap;

    bitmap.setInfo(SkImageInfo::Make(w, h, kBGRA_8888_SkColorType, kOpaque_SkAlphaType));

    bitmap.allocPixels();

    SkCanvas canvas(bitmap);

    SkPaint paint;

    canvas.clear(SK_ColorWHITE);

    paint.setAntiAlias(true);

    paint.setARGB(255, 255, 0, 0);

    canvas.drawCircle(80, 80, 40, paint);

    canvas.drawLine(0, 280, w, 280, paint);

    paint.setTextSize(60);

    canvas.drawString("Hello Skia", 300, 150, paint);

    return bitmap;
}


SDL_Rect create_rect(SDL_Surface *surface) {

    SDL_Rect src = { 0, 0, surface->w, surface->h };

    return src;
}


int main(int args, char *argv[]) {

    SDL_Window *window;

    SDL_Surface *surface;

    SDL_Renderer *renderer;

    SDL_Texture *texture;

    SkBitmap bitmap;

    RGBA rgba;

    SDL_Rect rect;

    int width = 800;
    int height = 480;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("Hello Skia", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height,
                              SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
    if (window == NULL) {
        return -1;
    }

    bitmap = draw(width, height);
    surface = SDL_CreateRGBSurfaceFrom(bitmap.getPixels(), width, height, 32, width * 4, rgba.rmask, rgba.gmask,
                                       rgba.bmask, rgba.amask);
    rect = create_rect(surface);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_RenderClear(renderer);
    texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_RenderCopy(renderer, texture, NULL, &rect);
    SDL_RenderPresent(renderer);
    SDL_Delay(5000);
    SDL_FreeSurface(surface);
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

But Clion shows

[ 50%] Linking CXX executable skia_tutorial
Undefined symbols for architecture x86_64:
      "_CFArrayCreate", referenced from:
.....
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[3]: *** [CMakeFiles/skia_tutorial.dir/build.make:86: skia_tutorial] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/skia_tutorial.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/skia_tutorial.dir/rule] Error 2
gmake: *** [Makefile:118: skia_tutorial] Error 2

Obviously, it guess there is something wrong with CMakeLists.txt. However, I couldn't find any error...

Please help me solve it.

macos
cmake
clion
skia
asked on Stack Overflow Nov 1, 2018 by Coneain • edited Nov 1, 2018 by Coneain

1 Answer

2

I found something in How to link the Skia library for a C++ project with XCode, in part Add Mac OSX Specific Dependencies of Skia, I added the following specification in CMakeLists.txt, and it works!

if (APPLE)
    target_link_libraries(skia_tutorial "-framework CoreServices")
    target_link_libraries(skia_tutorial "-framework CoreGraphics")
    target_link_libraries(skia_tutorial "-framework CoreText")
    target_link_libraries(skia_tutorial "-framework CoreFoundation")
endif()
answered on Stack Overflow Nov 1, 2018 by Coneain

User contributions licensed under CC BY-SA 3.0