问题描述
我尝试在MacOS虚拟机上使用Qwt和CMake来构建应用程序.
I Try to build an application using Qwt with CMake on a MacOS virtual machine.
我使用brew安装了Qt5和Qwt:
I installed Qt5 and Qwt using brew:
酿造qt5 qwt
结果是安装了Qwt框架.
The result is a Qwt framework installation.
标题在那里:
/usr/local/Cellar/qwt/6.1.4/lib/qwt.framework/Versions/6/Headers/qwt.h
图书馆在这里
文件/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位动态链接的共享库x86_64
在使用qmake进行buid期间很容易使用它
It is easy to use it during buid with qmake
LIBS + = -F/usr/local/Cellar/qwt/6.1.4/libLIBS + =-框架qwt
但是我仍然坚持使用 CMake .我没有成功找到方法.
but I'm stuck with CMake.I don't succeed to find how to do the same.
我已经根据CMake文档中指示的find_library编写了自己的 FindQwt 模块.它在Linux上运行良好,但没有以下技巧,我无法成功使其在MacOS上运行:
I have written my own FindQwt module based on find_library has indicated in CMake documentation.It works very well on Linux but I don't succeed to make it works on MacOS without the following hacks:
符号链接使qwt标头出现在包含目录中,而 qwt 作为 libqwt.dyn_lib :
symbolic links to make appear qwt headers in an include directory and qwt as 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/includeln -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(在/usr/local/Cellar/qwt/6.1.4/lib/qwt.framework上的符号链接),这是我在前2次黑客攻击后由FindQwt Cmake模块找到的,但我没有找到任何原因,并且导致了链接问题(ld错误22)
I also have to remove /usr/local/lib/qwt.framework ( symbolic link on /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework ) that was found by my FindQwt Cmake module after the 2 previous hack but I don't find any reason for that and was causing a link issue ( ld error 22 )
这是我的Qwt cmake模块的代码
Here is the code of my Qwt cmake module
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模块以更优雅的方式处理它们,将受到欢迎
If you have some ideas some ideas to avoid the hacks described above and hanle it in a more elegant way with CMake module they will be welcomed
解决方案可以运行,而无需先前描述的黑客攻击
- 请参阅与Qwt版本无关的 usr/local/opt/qwt/lib
- 使用以下经过修改的CMake模块,这要归功于Pedro的回答(主要修改与结尾处的目标属性有关,而对标题搜索的影响很小)
这是代码
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基础知识,这是我的建议:
Assuming that you are already familiar with cmake/qt basics, this is my proposal:
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风格的dylib.您可能会注意到,使用上面的代码片段输出执行cmake:
The CMake variable CMAKE_FIND_FRAMEWORK affects how find_* commands choose between macOS Frameworks and unix-style dylibs. You may observe that executing cmake with the above snippet outputs:
QWT found: /usr/local/Cellar/qwt/6.1.4/lib/qwt.framework
此代码段将qwt框架添加到CMakeLists.txt中定义的任何目标,但是您可能更喜欢将链接部分仅应用于一个目标,如下所示:
This snippet adds the qwt framework to any target defined in your CMakeLists.txt, but you may prefer to apply the link part to only one target, like this:
target_link_libraries(yourtarget ${QWT})
另一个警告:您应该将程序链接到由Qwt链接的相同Qt5框架,并且也要通过brew安装:QTDIR =/usr/local/opt/qt5
Another caveat: you should link your program to the same Qt5 frameworks linked by Qwt, and also installed by brew: QTDIR=/usr/local/opt/qt5
这篇关于使用通过CMake中的brew安装的Qwt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!