我正在尝试使用 cmake 构建 cpp/qt5 项目,但编译返回有关 QT5 的错误。
我用 Homebrew 安装了 QT5,
当我这样做时:
brew --prefix qt5
我明白了
/usr/local/Cellar/qt5/5.6.0
所以我在项目的 build
文件夹中,我这样做:
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix qt5) -G Xcode -DBUILD_TESTS=ON -DBUILD_TESTS_COVERAGE=off
给我返回这个错误:
CMake Error at CMakeLists.txt:124 (find_package):
By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Qt5", but
CMake did not find one.
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
qt5-config.cmake
Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
to a directory containing one of the above files. If "Qt5" provides a
separate development package or SDK, be sure it has been installed.
CMakelist.txt 中的错误行:
## QT5 ##
find_package(Qt5 COMPONENTS Core Gui Widgets Concurrent Qml Quick REQUIRED)
set_target_properties(${BIN_NAME} PROPERTIES AUTOMOC ON)
set_target_properties(${BIN_NAME} PROPERTIES AUTOUIC ON)
target_link_libraries(${BIN_NAME} PUBLIC Qt5::Core PUBLIC Qt5::Widgets PUBLIC Qt5::Gui PUBLIC Qt5::Qml PUBLIC Qt5::Quick PUBLIC Qt5::Concurrent)
#########
我还尝试在命令中添加 -DQt5_DIR=$(brew --prefix qt5)
,结果相同。
有什么想法吗? 谢谢
最佳答案
尝试:
-DQt5_DIR=$(brew --prefix qt5)/lib/cmake/Qt5
请注意,您可以在使用set_target_properties
时指定多个属性,并在使用target_link_libraries
时为多个值指定PUBLIC
范围:
set_target_properties(${BIN_NAME}
PROPERTIES
AUTOMOC ON
AUTOUIC ON
)
target_link_libraries(${BIN_NAME}
PUBLIC
Qt5::Core
Qt5::Widgets
Qt5::Gui
Qt5::Qml
Qt5::Quick
Qt5::Concurrent
)
关于c++ - MacOS Sierra - cmake 因 qt5 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40081327/