使用CMake和Conan软件包管理器正确设置Vulkan,glfw和spdlog [英] Setting up Vulkan, glfw and spdlog correctly using CMake and conan package manager

查看:260
本文介绍了使用CMake和Conan软件包管理器正确设置Vulkan,glfw和spdlog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Vulkan API渲染器。我在使用CMake和柯南软件包管理器正确设置项目时遇到了很大的麻烦。让我们看一下我的conanfile.py进行依赖项设置:

I'm working on a renderer for Vulkan API. I have big trouble setting up the project correctly using CMake and conan package manager. Let's take a look at my conanfile.py for dependency setup:

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()

所有柯南设置均正常工作,如CMake输出所示:

All the conan setup is working correctly, as can be seen by CMake output:

遗憾的是,Vulkan API没有柯南设置。因此,我正在使用Sascha Willem的github存储库中的一些代码。我的CMake文件如下所示:

Sadly, there is no conan setup for Vulkan API. So I am using some code of Sascha Willem's github repository. My CMake file looks like this:

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 Studio项目文件时,必须手动将glfw3.lib,spdlogd.lib和fmtd.lib添加到项目中。其他存储库(例如Vulkan官方样本)采用的是更传统的方法,只需将库直接粘贴到repo文件夹中即可。我不想这样做,因为我希望柯南为我工作。我已经尝试解决此问题已有6个月了。

When I generate a Visual Studio project file with this setup, I have to add glfw3.lib, spdlogd.lib and fmtd.lib to the project manually. Other repositories like the official Vulkan samples take a more traditional approachand just paste the libs directly into the repo folders. I don't want to do this since I want conan to work for me. I've been trying to fix this problem for 6 months now.

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

谢谢。

推荐答案

我找到了解决方案:一个名为conan_target_link_libraries的函数,必须使用它代替target_link_libraries。感谢您的回答。

I found the solution: There is a function called conan_target_link_libraries which must be used instead of target_link_libraries. Thanks for the answer.

这篇关于使用CMake和Conan软件包管理器正确设置Vulkan,glfw和spdlog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆