cmake生成libwebp的Xcode工程时部分静态库编译失败

1,197 阅读1分钟

在mac上使用cmake编译libwebp项目时, 生成了Xcode工程, 在编译时遇到报错

following build commands failed: Ld xxxxxxxx webp_quality normal.

原因应该是webp_quality所依赖的部分静态库没有成功生成而导致的问题, 经过一番google, 在下方的github commit找到了解决方法: github.com/webmproject…

# Creates a source file with an unused stub function in $CMAKE_BINARY_DIR and
# adds it to the specified target. Currently used only with Xcode.
#
# See also:
# https://cmake.org/cmake/help/v3.18/command/add_library.html#object-libraries
# "Some native build systems (such as Xcode) may not like targets that have
# only object files, so consider adding at least one real source file to any
# target that references $<TARGET_OBJECTS:objlib>."

原因如上, 大概意思应该是Xcode对于cmake的目标生成有一些限制, 需要额外生成一些文件. 对CMakeLists.txt的改动如下:

# ##############################################################################
# Build the webpdecoder library.

++++++++++++++++++++++++++++++++++++++++++++++
# Creates a source file with an unused stub function in $CMAKE_BINARY_DIR and
# adds it to the specified target. Currently used only with Xcode.
#
# See also:
# https://cmake.org/cmake/help/v3.18/command/add_library.html#object-libraries
# "Some native build systems (such as Xcode) may not like targets that have
# only object files, so consider adding at least one real source file to any
# target that references $<TARGET_OBJECTS:objlib>."
function(libwebp_add_stub_file TARGET)
  set(stub_source_dir "${CMAKE_BINARY_DIR}")
  set(stub_source_file
      "${stub_source_dir}/libwebp_${TARGET}_stub.c")
  set(stub_source_code
      "// Generated file. DO NOT EDIT!\n"
      "// C source file created for target ${TARGET}.\n"
      "void libwebp_${TARGET}_stub_function(void)\;\n"
      "void libwebp_${TARGET}_stub_function(void) {}\n")
  file(WRITE "${stub_source_file}" ${stub_source_code})

  target_sources(${TARGET} PRIVATE ${stub_source_file})
endfunction()
++++++++++++++++++++++++++++++++++++++++++++++

if(MSVC)
  # avoid security warnings for e.g., fopen() used in the examples.
  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  
@@ -195,6 +218,9 @@ add_library(webpdecoder
            $<TARGET_OBJECTS:webpdecode>
            $<TARGET_OBJECTS:webpdspdecode>
            $<TARGET_OBJECTS:webputilsdecode>)
            
++++++++++++++++++++++++++++++++++++++++++++++
if(XCODE)
  libwebp_add_stub_file(webpdecoder)
endif()
++++++++++++++++++++++++++++++++++++++++++++++

target_link_libraries(webpdecoder ${WEBP_DEP_LIBRARIES})
target_include_directories(
  webpdecoder
@@ -234,6 +260,9 @@ add_library(webp
            $<TARGET_OBJECTS:webpdsp>
            $<TARGET_OBJECTS:webpencode>
            $<TARGET_OBJECTS:webputils>)

++++++++++++++++++++++++++++++++++++++++++++++
if(XCODE)
  libwebp_add_stub_file(webp)
endif()
++++++++++++++++++++++++++++++++++++++++++++++

target_link_libraries(webp ${WEBP_DEP_LIBRARIES})
target_include_directories(webp
                           PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}