CMake 配置 Vulkan 出现链接失败,找不到 vkEnumerateInstanceExtensionProperties 符号的错误的解决方法

使用 CMake 配置 glfw, glm 的时候,总是提示链接失败,找不到 vkEnumerateInstanceExtensionProperties 符号

cmake_minimum_required(VERSION 3.4...3.27)

if(${CMAKE_VERSION} VERSION_LESS 3.27)
    cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
    cmake_policy(VERSION 3.27)
endif()

project(VulkanTest VERSION 1.0
	DESCRIPTION "Learning Vulkan"
	LANGUAGES CXX)

# ---- Include guards ----
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(
  FATAL_ERROR
    "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there."
)
endif()

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

add_executable(VulkanTest src/test.cpp src/test.h)

# vulkan sdk

target_include_directories(VulkanTest PUBLIC $ENV{VULKAN_SDK}/Include)
target_link_libraries(VulkanTest $ENV{VULKAN_SDK}/Lib)

# link glfw with cmake guide: 
# https://www.glfw.org/docs/3.3/build_guide.html

# don't compile example

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

add_subdirectory(dependencies/glfw)

target_include_directories(VulkanTest PUBLIC dependencies/glfw/include)
target_link_libraries(VulkanTest glfw)

# glm

add_subdirectory(dependencies/glm)

target_include_directories(VulkanTest PUBLIC dependencies/glm)
target_link_libraries(VulkanTest glm)

这很明显就是链接不到 Vulkan SDK 的库,但是我觉得我这么写应该没有问题

我还以为是要把 Vulkan SDK 的 lib 文件复制到 exe 目录底下什么的,我还在想要不要用 install

之后我看别人的仓库,别人都没有用 install,于是我再仔细看,最后发现别人不是 link 到文件夹,而是 link 到一个 lib 文件,所以我应该这么写:target_link_libraries(VulkanTest $ENV{VULKAN_SDK}/Lib/vulkan-1.lib),然后就好了

本来我 link 到一个文件夹的时候,cmake 在控制台输出那里也提示了,会忽略掉这一行……果然是因为我不能 link 到一个文件夹

你可能感兴趣的:(GameEngineDev,c++,vulkan)