在OSX的Clion中使用Skia

时间:2018-11-01 02:54:12

标签: macos cmake clion skia

我想在OSX的Clion中使用Skia和SDL2。我成功安装了SDL2,并在Clion的一个项目中对其进行了测试,效果很好。然后,我在Skia的官方网站上关注了该教程。我将其编译并放入Clion项目中,但没有成功!我检查了几次,但对失败的原因一无所知。

这是我关于测试项目的配置。

我构建了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

它会生成两个静态库libskia.alibpathkit.a

screenshot显示了我如何在Clion中配置项目。

这是我的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()

这是我的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;
}

但是Clion展示了

[ 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

很显然,它猜测CMakeLists.txt有问题。但是,我找不到任何错误...

请帮助我解决问题。

1 个答案:

答案 0 :(得分:1)

我在How to link the Skia library for a C++ project with XCode中找到了一些内容,部分是添加Skia的Mac OSX特定依赖项,我在CMakeLists.txt中添加了以下规范,并且可以使用!

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()