1

我正在使用VulkanMemoryAllocation,这是一个仅限标题的库。我想使用 cmake 将它编译成一个静态库,但我最终得到一个空的 - 8 字节大小的 - 库文件,并且在链接时有很多未定义的符号。

这是相关部分CMakeList.txt

# The header with the implementation
add_library(VulkanMemoryAllocator STATIC VulkanMemoryAllocator-Hpp/vk_mem_alloc.h)
# The include path for a wrapper which uses above mentionned header
target_include_directories(VulkanMemoryAllocator PUBLIC VulkanMemoryAllocator-Hpp/)
# enable the actual implementation
target_compile_options(VulkanMemoryAllocator PRIVATE VMA_IMPLEMENTATION)
# consider this file as a C++ file
set_target_properties(VulkanMemoryAllocator PROPERTIES LINKER_LANGUAGE CXX)

编辑:我想做与此等效的操作:

clang++ -c -DVMA_IMPLEMENTATION -x c++ -o vk_mem_alloc.o  ../lib/VulkanMemoryAllocator-Hpp/vk_mem_alloc.h && ar rc libvma.a vk_mem_alloc.o

但使用 CMake

4

1 回答 1

1

VMA_IMPLEMENTATION是一个compile_definitioncompile_option

除了目标(我认为)之外,您还必须设置文件语言。

set_source_file_properties(
    VulkanMemoryAllocator-Hpp/vk_mem_alloc.h PROPERTIES
    LANGUAGE CXX
)

我只是复制标题,以便 CMake 从扩展中知道它是 C++,它更简单。

add_custom_command(
    COMMENT 'Copying vk_mem_alloc'
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/vk_mem_alloc.cpp
    DEPENDS VulkanMemoryAllocator-Hpp/vk_mem_alloc.h
    COMMAND ${CMAKE_COMMAND} -E copy
        VulkanMemoryAllocator-Hpp/vk_mem_alloc.h
        ${CMAKE_CURRENT_BINARY_DIR}/vk_mem_alloc.cpp
)
add_library(VulkanMemoryAllocator STATIC
    ${CMAKE_CURRENT_BINARY_DIR}/vk_mem_alloc.cpp
)
target_include_directories(VulkanMemoryAllocator PUBLIC
    VulkanMemoryAllocator-Hpp
)
target_compile_definitions(VulkanMemoryAllocator PRIVATE
    VMA_IMPLEMENTATION
)

这里https://github.com/usnistgov/hevx/blob/master/third_party/VulkanMemoryAllocator.cmake是一个类似的解决方案,它创建一个 C++ 文件#define并对其进行编译。

于 2021-08-24T18:54:05.340 回答