我正在尝试按照here给出的答案将多个静态库组合到MinGW64下的单个存档中。具体来说,我使用CMake并指定以下命令:
add_custom_command(
OUTPUT ${COMBINED_CORE_LIB_NAME}
COMMAND ${AR} -crs ${COMBINED_CORE_LIB_NAME} ${CORE_LIB_TARGET_FILES}
DEPENDS ${DILIGENT_CORE_INSTALL_LIBS_LIST}
COMMENT "Combining core libraries..."
)
此外,按照here的建议,我不使用股票ar,而是使用交叉ar:
find_program(AR NAMES x86_64-w64-mingw32-gcc-ar)
但是,无论我做什么,ar拒绝生成索引,并且每次我尝试链接到生成的库时,都会出现此错误:
error adding symbols: Archive has no index; run ranlib to add one
按照建议的方式运行ranlib要么存入一个,要么x86_64-w64-mingw32-gcc-ranlib都没有影响。
我花了15分钟来使它与MSVC和lib.exe一起工作,而与MinGW却苦苦挣扎了8小时。任何建议将不胜感激。
[Edit]:事实证明,此问题并非真正针对MinGW,并且在Linux上也以非常相似的方式发生。
答案 0 :(得分:1)
事实证明,使用ar实用程序无法将多个.a文件合并为单个.a库:它拒绝生成索引。一个可行的解决方案是从所有静态库中解压缩所有目标文件,然后将它们重新打包到组合库中。
下面是我最后得到的CMake指令:
set(COMBINED_CORE_LIB_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}DiligentCore${CMAKE_STATIC_LIBRARY_SUFFIX}")
foreach(CORE_LIB ${DILIGENT_CORE_INSTALL_LIBS_LIST})
list(APPEND CORE_LIB_TARGET_FILES $<TARGET_FILE:${CORE_LIB}>)
endforeach(CORE_LIB)
if(MSVC)
add_custom_command(
OUTPUT ${COMBINED_CORE_LIB_NAME}
COMMAND ${LIB_EXE} /OUT:${COMBINED_CORE_LIB_NAME} ${CORE_LIB_TARGET_FILES}
DEPENDS ${DILIGENT_CORE_INSTALL_LIBS_LIST}
COMMENT "Combining core libraries..."
)
add_custom_target(DiligentCore-static ALL DEPENDS ${COMBINED_CORE_LIB_NAME})
else()
if(PLATFORM_WIN32)
# do NOT use stock ar on MinGW
find_program(AR NAMES x86_64-w64-mingw32-gcc-ar)
else()
set(AR ${CMAKE_AR})
endif()
if(AR)
add_custom_command(
OUTPUT ${COMBINED_CORE_LIB_NAME}
# Delete all object files from current directory
COMMAND ${CMAKE_COMMAND} -E remove "*${CMAKE_C_OUTPUT_EXTENSION}"
DEPENDS ${DILIGENT_CORE_INSTALL_LIBS_LIST}
COMMENT "Combining core libraries..."
)
# Unpack all object files from all targets to current directory
foreach(CORE_LIB_TARGET ${CORE_LIB_TARGET_FILES})
add_custom_command(
OUTPUT ${COMBINED_CORE_LIB_NAME}
COMMAND ${AR} -x ${CORE_LIB_TARGET}
APPEND
)
endforeach()
# Pack object files to a combined library and delete them
add_custom_command(
OUTPUT ${COMBINED_CORE_LIB_NAME}
COMMAND ${AR} -crs ${COMBINED_CORE_LIB_NAME} "*${CMAKE_C_OUTPUT_EXTENSION}"
COMMAND ${CMAKE_COMMAND} -E remove "*${CMAKE_C_OUTPUT_EXTENSION}"
APPEND
)
add_custom_target(DiligentCore-static ALL DEPENDS ${COMBINED_CORE_LIB_NAME})
else()
message("ar command is not found")
endif()
endif()
if(TARGET DiligentCore-static)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${COMBINED_CORE_LIB_NAME}"
DESTINATION "${DILIGENT_CORE_INSTALL_DIR}/lib/"
)
set_target_properties(DiligentCore-static PROPERTIES
FOLDER Core
)
else()
message("Unable to find librarian tool. Combined DiligentCore static library will not be produced.")
endif()