在 CMake 中使用通过 brew 安装的 Qwt

Use Qwt installed via brew in CMake

我尝试在 MacOS 虚拟机上使用 Qwt 和 CMake 构建应用程序。

我使用 brew 安装了 Qt5 和 Qwt:

brew install qt5 qwt

结果是一个Qwt框架安装。

header 在那里:

/usr/local/Cellar/qwt/6.1.4/lib/qwt.framework/Versions/6/Headers/qwt.h

图书馆就在这里

file /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework/qwt /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework/qwt: Mach-O 64-bit dynamically linked shared library x86_64

在使用qmake的时候很容易使用

LIBS+= -F/usr/local/Cellar/qwt/6.1.4/lib LIBS += -framework qwt

但我受困于 CMake。我没有成功找到如何做同样的事情。

我已经根据 find_library 在 CMake 文档中指出的 FindQwt 模块编写了自己的模块。 它在 Linux 上运行良好,但如果没有以下 hack,我无法使其在 MacOS 上运行:

symbolic links 使 qwt headers 出现在包含目录中,qwt 显示为 libqwt.dyn_lib:

ln -sf /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework/Versions/6/Headers /usr/local/Cellar/qwt/6.1.4/include ln -sf /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework/qwt /usr/local/Cellar/qwt/6.1.4/libqwt.dylib

我还必须删除 /usr/local/lib/qwt.framework ( symbolic link on /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework ),这是我的 FindQwt Cmake 模块在前 2 次 hack 之后发现的,但我没有找到任何原因,并且导致了 link 问题(ld error 22)

这是我的 Qwt cmake 模块的代码

find_package(PkgConfig)
pkg_check_modules(PC_QWT QUIET libqwt)
if("${PC_QWT}" STREQUAL "")
    set(QWT_HINTS $ENV{QWT_INSTALL})

    set(QWT_PATHS
        /usr
        /usr/local/
        /usr/local/share/
        /usr/share/
       )
    if(NOT ${QWT_HINTS} STREQUAL "")
        set(QWT_PATHS ${QWT_HINTS})
    endif(NOT ${QWT_HINTS} STREQUAL "")

    find_path(Qwt_INCLUDE_DIR
              NAMES qwt.h
              PATHS ${QWT_PATHS}
              HINTS ${QWT_HINTS}
              PATH_SUFFIXES include qwt-qt4 qwt-qt5 qwt
              DOC "Variable storing the location of Qwt header"
             )

    set(ARCH_SUFFIX "lib")
    if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
      set(ARCH_SUFFIX "")
    endif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")

    find_library(Qwt_LIBRARY
                 NAMES qwt qwt-qt4 qwt-qt5
                 PATHS ${QWT_PATHS}
                 HINTS ${QWT_HINTS}
                 PATH_SUFFIXES ${ARCH_SUFFIX}
                 DOC "Variable storing the location of Qwt library"
                 NO_DEFAULT_PATH
                 NO_CMAKE_PATH
                )

    set(Qwt_VERSION ${Qwt_FIND_VERSION})
    include(FindPackageHandleStandardArgs)
    find_package_handle_standard_args(Qwt
                                      FOUND_VAR Qwt_FOUND
                                      REQUIRED_VARS
                                      Qwt_LIBRARY
                                      Qwt_INCLUDE_DIR
                                      VERSION_VAR Qwt_VERSION
                                      )

    if(Qwt_FOUND)
        set(Qwt_LIBRARIES ${Qwt_LIBRARY})
        set(Qwt_INCLUDE_DIRS ${Qwt_INCLUDE_DIR})
        #set(Qwt_DEFINITIONS ${PC_Qwt_CFLAGS_OTHER})
    endif()
    if(Qwt_FOUND AND NOT TARGET Qwt::Qwt)
        add_library(Qwt::Qwt UNKNOWN IMPORTED)
        set_target_properties(Qwt::Qwt PROPERTIES
        IMPORTED_LOCATION "${Qwt_LIBRARY}"
        INTERFACE_COMPILE_OPTIONS ""
        INTERFACE_INCLUDE_DIRECTORIES "${Qwt_INCLUDE_DIR}")
    endif()
endif("${PC_QWT}" STREQUAL "")

如果您有一些想法,一些想法可以避免上述黑客攻击并使用 CMake 模块以更优雅的方式处理它,我们将欢迎他们

没有先前描述的 hack 的解决方案

这是代码

find_package(PkgConfig)
pkg_check_modules(PC_QWT QUIET libqwt)
if("${PC_QWT}" STREQUAL "")
    set(QWT_HINTS $ENV{QWT_INSTALL})

    set(QWT_PATHS
        /usr
        /usr/local/
        /usr/local/share/
        /usr/share/
       )
    if(NOT ${QWT_HINTS} STREQUAL "")
        set(QWT_PATHS ${QWT_HINTS})
    endif(NOT ${QWT_HINTS} STREQUAL "")

    find_path(Qwt_INCLUDE_DIR
              NAMES qwt.h
              PATHS ${QWT_PATHS}
              HINTS ${QWT_HINTS}
              PATH_SUFFIXES include qwt-qt4 qwt-qt5 qwt Headers
              DOC "Variable storing the location of Qwt header"
             )

    set(ARCH_SUFFIX "lib")
    if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
       set(ARCH_SUFFIX "")
    endif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")

    find_library(Qwt_LIBRARY
                 NAMES qwt qwt-qt4 qwt-qt5
                 PATHS ${QWT_PATHS}
                 HINTS ${QWT_HINTS}
                 PATH_SUFFIXES ${ARCH_SUFFIX}
                 DOC "Variable storing the location of Qwt library"
                )

    set(Qwt_VERSION ${Qwt_FIND_VERSION})
    include(FindPackageHandleStandardArgs)
    find_package_handle_standard_args(Qwt
                                      FOUND_VAR Qwt_FOUND
                                      REQUIRED_VARS
                                      Qwt_LIBRARY
                                      Qwt_INCLUDE_DIR
                                      VERSION_VAR Qwt_VERSION
                                     )

    if(Qwt_FOUND)
        set(Qwt_LIBRARIES ${Qwt_LIBRARY})
        set(Qwt_INCLUDE_DIRS ${Qwt_INCLUDE_DIR})
    endif()
    if(Qwt_FOUND AND NOT TARGET Qwt::Qwt)
        if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
            get_filename_component(FRAMEWORK_LOC ${Qwt_LIBRARY} DIRECTORY)
            add_library(Qwt::Qwt INTERFACE IMPORTED)
            set_target_properties(Qwt::Qwt PROPERTIES
                                  INTERFACE_COMPILE_OPTIONS ""
                                  INTERFACE_INCLUDE_DIRECTORIES "${Qwt_INCLUDE_DIR}"
                                  )

            target_link_libraries(Qwt::Qwt INTERFACE "-F${FRAMEWORK_LOC} -framework qwt")
        else("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
            add_library(Qwt::Qwt UNKNOWN IMPORTED)
            set_target_properties(Qwt::Qwt PROPERTIES
                                  IMPORTED_LOCATION "${Qwt_LIBRARIES}"
                                  INTERFACE_COMPILE_OPTIONS ""
                                  INTERFACE_INCLUDE_DIRECTORIES "${Qwt_INCLUDE_DIR}"
                                  )
        endif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
    endif()
endif("${PC_QWT}" STREQUAL "")

假设你已经熟悉cmake/qt basics,这是我的建议:

if(APPLE)
    set(CMAKE_FIND_FRAMEWORK ONLY)
    find_library(QWT
        NAMES qwt
        HINTS /usr/local/opt/qwt/lib/
        REQUIRED)
    if(QWT)
        include_directories(${QWT}/Headers)
        link_libraries(${QWT})
        message("QWT found: ${QWT}")
    endif()
endif()

CMake 变量 CMAKE_FIND_FRAMEWORK 影响 find_* 命令如何在 macOS 框架和 unix 风格的动态库之间进行选择。您可能会观察到使用上述代码段输出执行 cmake:

QWT found: /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework

此代码段将 qwt 框架添加到您 CMakeLists.txt 中定义的任何目标,但您可能更愿意将 link 部分仅应用于一个目标,如下所示:

target_link_libraries(yourtarget ${QWT})

另一个警告:您应该 link 您的程序使用由 Qwt link 编辑并由 brew 安装的相同 Qt5 框架:QTDIR=/usr/local/opt/qt5