gpt4 book ai didi

macos - 在 OSX 上的 Clion 中使用 Skia

转载 作者:行者123 更新时间:2023-12-05 01:41:52 28 4
gpt4 key购买 nike

我想在 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;
}

但是克利昂显示

[ 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 有问题。但是,我找不到任何错误...

请帮我解决一下。

最佳答案

我在 How to link the Skia library for a C++ project with XCode 中找到了一些东西, 在 Add Mac OSX Specific Dependencies of Skia 部分,我在 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()

关于macos - 在 OSX 上的 Clion 中使用 Skia,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094613/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com