首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用CMake和conan软件包管理器正确设置Vulkan、glfw和spdlog

使用CMake和conan软件包管理器正确设置Vulkan、glfw和spdlog
EN

Stack Overflow用户
提问于 2020-02-26 20:53:48
回答 1查看 1.3K关注 0票数 3

我正在为Vulkan API开发一个渲染器。我在使用CMake和正确设置项目时遇到了很大的困难。让我们来看看我的依赖项设置的conanfile.py:

代码语言:javascript
运行
复制
from conans import ConanFile, CMake

class InexorConan(ConanFile):

settings = (
    "os",
    "compiler",
    "build_type",
    "arch"
)

requires = (
    "benchmark/1.5.0",
    "glm/0.9.9.7",
    "gtest/1.10.0",
    "spdlog/1.5.0",
    "glfw/3.3.2@bincrafters/stable",
    "toml11/3.1.0",
    "imgui/1.75",
    "assimp/5.0.1",
    "enet/1.3.14 "
)

generators = "cmake"

default_options = {
}

def imports(self):
    # Copies all dll files from packages bin folder to my "bin" folder (win)
    self.copy("*.dll", dst="bin", src="bin")
    # Copies all dylib files from packages lib folder to my "lib" folder (macosx)
    self.copy("*.dylib*", dst="lib", src="lib") # From lib to lib
    # Copies all so files from packages lib folder to my "lib" folder (linux)
    self.copy("*.so*", dst="lib", src="lib") # From lib to lib

def build(self):
    cmake = CMake(self)
    cmake.configure()
    cmake.build()

所有conan设置都正常工作,从CMake输出可以看出:

遗憾的是,Vulkan API没有conan设置。所以我使用了一些Sascha Willem的github存储库的代码。我的CMake文件如下所示:

代码语言:javascript
运行
复制
cmake_minimum_required(VERSION 3.4)

project(inexor-vulkan-renderer)

file(GLOB_RECURSE source_files
  "src/*.hpp"
  "src/*.cpp"
)

# Use the folder structure in source code directory as project structure in Visual Studio.
function(assign_source_group)
    foreach(source_files IN ITEMS ${ARGN})
        if (IS_ABSOLUTE "${source_files}")
            file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${source_files}")
        else()
            set(_source_rel "${source_files}")
        endif()
        get_filename_component(_source_path "${_source_rel}" PATH)
        string(REPLACE "/" "\\" _source_path_msvc "${_source_path}")
        source_group("${_source_path_msvc}" FILES "${source_files}")
    endforeach()
endfunction(assign_source_group)

# Use CMake to find Vulkan SDK.
if (NOT CMAKE_VERSION VERSION_LESS 3.7.0)
    message(STATUS "Using module to find Vulkan")
    find_package(Vulkan)
endif()

# Dependency setup via conan.
# Download conan executer in case it does not exists.
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/conan.cmake")
   message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
   file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.14/conan.cmake"
                 "${CMAKE_CURRENT_BINARY_DIR}/conan.cmake")
endif()

# Execute conan build instructions.
include(${CMAKE_CURRENT_BINARY_DIR}/conan.cmake)

conan_cmake_run(CONANFILE conanfile.py
                BASIC_SETUP
                BUILD outdated
                PROFILE default
                PROFILE_AUTO build_type
                KEEP_RPATHS
)

# Use the folder structure in source code directory as project structure in Visual Studio.
assign_source_group(${source_files})

add_executable(inexor-vulkan-renderer src/main.cpp ${source_files})

target_link_libraries(inexor-vulkan-renderer PUBLIC ${Vulkan_LIBS} ${CONAN_LIBS})

# Use C++17!
target_compile_features(inexor-vulkan-renderer PRIVATE cxx_std_17)

IF(WIN32)
    target_compile_definitions(inexor-vulkan-renderer PRIVATE VK_USE_PLATFORM_WIN32_KHR)
ENDIF()

target_include_directories(inexor-vulkan-renderer PRIVATE Vulkan::Vulkan)

target_link_libraries(inexor-vulkan-renderer Vulkan::Vulkan)

使用此设置生成Visual项目文件时,必须手动向项目添加glfw3.lib、spdlogd.lib和fmtd.lib。其他存储库,如官方的Vulkan样本,采用了更传统的方法,只需将库直接粘贴到回购文件夹中。我不想这么做,因为我想让柯南为我工作。我已经试着解决这个问题六个月了。

为什么CMake不能链接所需的库,尽管conan可以找到它们?

谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2020-02-26 23:26:10

我找到了解决方案:有一个名为conan_target_link_libraries的函数,必须使用它而不是target_link_libraries。谢谢你的回答。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60422367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档