我想在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.a
和libpathkit.a
。
这张截图显示了我如何在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
有问题。然而,我找不到任何错误。。。
请帮我解决。
我在如何将C++项目的Skia库与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()