From f3ea230ddf14be3c3726ac443cef0f66ab89fff3 Mon Sep 17 00:00:00 2001 From: Hebangwen Date: Fri, 19 Aug 2022 15:24:04 +0800 Subject: [PATCH 1/3] feat: compile `stbiw` as a library --- stbiw/CMakeLists.txt | 12 +++++++++++- stbiw/stb_image_write.cpp | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 stbiw/stb_image_write.cpp diff --git a/stbiw/CMakeLists.txt b/stbiw/CMakeLists.txt index b56b853..6c11089 100644 --- a/stbiw/CMakeLists.txt +++ b/stbiw/CMakeLists.txt @@ -1 +1,11 @@ -message(FATAL_ERROR "请修改 stbiw/CMakeLists.txt!要求生成一个名为 stbiw 的库") +cmake_minimum_required(VERSION 3.12) +project(lib_stbiw LANGUAGES CXX) + +add_library(stbiw SHARED stb_image_write.cpp) +target_include_directories(stbiw PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# `add_definitions`添加宏定义到当前目录和子目录下的所有源文件中, 但是不会对外部的源文件产生影响 +# add_definitions(-DSTB_IMAGE_WRITE_IMPLEMENTATION=1) + +# 设置为 private, 避免宏被传播到所有引用它的文件中, 而导致的重定义错误 +target_compile_definitions(stbiw PRIVATE -DSTB_IMAGE_WRITE_IMPLEMENTATION=1) \ No newline at end of file diff --git a/stbiw/stb_image_write.cpp b/stbiw/stb_image_write.cpp new file mode 100644 index 0000000..8f98cb6 --- /dev/null +++ b/stbiw/stb_image_write.cpp @@ -0,0 +1 @@ +#include "stb_image_write.h" From cdb818744e9bc730f43d445268781c40647d5a96 Mon Sep 17 00:00:00 2001 From: Hebangwen Date: Fri, 19 Aug 2022 16:12:55 +0800 Subject: [PATCH 2/3] build: add `INTERFACE` keyword --- stbiw/CMakeLists.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/stbiw/CMakeLists.txt b/stbiw/CMakeLists.txt index 6c11089..1ad1636 100644 --- a/stbiw/CMakeLists.txt +++ b/stbiw/CMakeLists.txt @@ -8,4 +8,13 @@ target_include_directories(stbiw PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) # add_definitions(-DSTB_IMAGE_WRITE_IMPLEMENTATION=1) # 设置为 private, 避免宏被传播到所有引用它的文件中, 而导致的重定义错误 -target_compile_definitions(stbiw PRIVATE -DSTB_IMAGE_WRITE_IMPLEMENTATION=1) \ No newline at end of file +target_compile_definitions(stbiw PRIVATE -DSTB_IMAGE_WRITE_IMPLEMENTATION=1) + +# 不能使用 `INTERFACE` 进行编译, 因为 `STB_IMAGE` 里用的是 `#ifdef` 而不是 `#ifndef` +# add_library(stbiw INTERFACE) +# target_include_directories(stbiw INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) + +# option(STB_IMAGE_WRITE_IMPLEMENTATION "Enable stb_image_write implementation" OFF) +# if (NOT ${STB_IMAGE_WRITE_IMPLEMENTATION}) +# message(STATUS "stb_image_write is not enabled.") +# endif() From 26e68a4061cae6851049935ff7edff5ff3986692 Mon Sep 17 00:00:00 2001 From: hebangwen <1925585604@qq.com> Date: Fri, 19 Aug 2022 17:05:36 +0800 Subject: [PATCH 3/3] fix: add statement in stb_image_write.cpp to build in Windows --- stbiw/stb_image_write.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stbiw/stb_image_write.cpp b/stbiw/stb_image_write.cpp index 8f98cb6..f56f4a8 100644 --- a/stbiw/stb_image_write.cpp +++ b/stbiw/stb_image_write.cpp @@ -1 +1,3 @@ #include "stb_image_write.h" + +int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);