使用柯南安装 Qt5Svg

Installing Qt5Svg with Conan

所以我正在尝试将我一直在做的这个项目从 qmake 移植到 cmake。我还计划通过 Conan 获得我的 qt 依赖项并获得 CI 服务。 使用 qt/5.12.4@bincrafters/stable 获取大部分 qt 库相当容易。除了一个。出于某种原因,Qt5Svg 模块不包含在这个包中,我找不到好的解决方案,所以我进入了死胡同。下面是错误信息和相关的 cmake 文件。

错误信息

[CMake] CMake Error at src/analysis_tool/CMakeLists.txt:33 (find_package):
[CMake] By not providing "FindQt5Svg.cmake" in CMAKE_MODULE_PATH this project has
[CMake] asked CMake to find a package configuration file provided by "Qt5Svg", but
[CMake] CMake did not find one.
[CMake]
[CMake] Could not find a package configuration file provided by "Qt5Svg" with any
[CMake] of the following names:
[CMake]
[CMake] Qt5SvgConfig.cmake
[CMake] qt5svg-config.cmake
[CMake]
[CMake] Add the installation prefix of "Qt5Svg" to CMAKE_PREFIX_PATH or set
[CMake] "Qt5Svg_DIR" to a directory containing one of the above files. If "Qt5Svg"
[CMake] provides a separate development package or SDK, be sure it has been
[CMake] installed.

Conan.cmake(从顶层 CMakeLists.txt 文件调用)

macro(run_conan)
  # Download automatically, you can also just copy the conan.cmake file
  if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
    message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
    message(${CMAKE_BINARY_DIR};)
    file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.15/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake")

  endif()

  include(${CMAKE_BINARY_DIR}/conan.cmake)

  conan_add_remote(
    NAME
    bincrafters
    URL
    https://api.bintray.com/conan/bincrafters/public-conan)
    
  conan_cmake_run(
    REQUIRES
    qt/5.12.4@bincrafters/stable
    OPTIONS
    ${CONAN_EXTRA_OPTIONS}
    BASIC_SETUP
    CMAKE_TARGETS # individual targets to link to
    BUILD
    missing)
endmacro()

可执行CMake

cmake_minimum_required(VERSION 3.8)
project(codego)

file(GLOB CODEGO_SOURCES "src/*.cpp")
file(GLOB CODEGO_HEADERS "include/*.h")
file(GLOB CODEGO_UIS "ui/*.ui")
file(GLOB CODEGO_RES "res/*.qrc")

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_AUTOUIC_SEARCH_PATHS ui)

add_executable(${PROJECT_NAME} src/main.cpp ${CODEGO_UIS})

target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PUBLIC ui)
target_include_directories(${PROJECT_NAME} PUBLIC res)

find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Qt5Svg REQUIRED)

# Add the include directories for the Qt 5 modules to the compile lines.
include_directories(${Qt5Core_INCLUDE_DIRS}/QtCore)
include_directories(${Qt5Gui_INCLUDE_DIRS}/QtGui)
include_directories(${Qt5Widgets_INCLUDE_DIRS}/QtWidgets)
include_directories(${Qt5Xml_INCLUDE_DIRS}/QtXml)
include_directories(${Qt5Xml_INCLUDE_DIRS}/QtSvg)

# Use the compile definitions defined in the Qt 5 modules
add_definitions(${Qt5Core_DEFINITIONS})
add_definitions(${Qt5Gui_DEFINITIONS})
add_definitions(${Qt5Widgets_DEFINITIONS})
add_definitions(${Qt5Xml_DEFINITIONS})
add_definitions(${Qt5Svg_DEFINITIONS})

qt5_wrap_ui(WRAP_UI_MOC ${CODEGO_UIS})
qt5_wrap_cpp(WRAP_HEADER_MOC ${CODEGO_HEADERS})

include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_BINARY_DIR})
include_directories(${CMAKE_BINARY_DIR})

include_directories(include)

add_library(PROJECT_LIBRARIES SHARED
    ${CODEGO_SOURCES}
    ${WRAP_UI_MOC}
    ${WRAP_HEADER_MOC})

target_link_libraries(${PROJECT_NAME} 
                        Qt5::Core 
                        Qt5::Gui
                        Qt5::Widgets
                        Qt5::Xml
                        Qt5::Svg)

target_link_libraries(${PROJECT_NAME} PROJECT_LIBRARIES)

所有其他模块似乎安装得很好(Qt5Core Qt5Widgets 等),我可以在 .conan 文件夹中找到它们,但不能在 Qt5Svg 文件夹中找到它们。感谢您花时间阅读本文。

您需要提供并选择使用 qt 包构建 qtsvg。您的 conan_cmake_run 应如下所示:

cmake_minimum_required(VERSION 3.8)
project(codego)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_AUTOUIC_SEARCH_PATHS ui)

if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
    message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
    message(${CMAKE_BINARY_DIR};)
    file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.15/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake")

  endif()

  include(${CMAKE_BINARY_DIR}/conan.cmake)

  conan_add_remote(
    NAME
    bincrafters
    URL
    https://api.bintray.com/conan/bincrafters/public-conan)

conan_cmake_run(
       REQUIRES qt/5.14.1@bincrafters/stable
       OPTIONS qt:qtsvg=True
       BASIC_SETUP
       CMAKE_TARGETS
       BUILD missing
)

编辑:我使用 qt 5.14.1,因为 5.12.4 由于一些过时的软件包而无法在我的 osx 机器上构建。现在当我做 ls:

ls ~/.conan/data/qt/5.14.1/bincrafters/stable/package/<package_id>/lib/cmake/

给我 Qt5Svg 个文件夹