Qt的版本是Qt 5.9.1 for Windows,visual studio是2015 professional。cmake是3.72
Qt的官方文档说得很清楚
http://doc.qt.io/qt-5/cmake-manual.html
这里要注意的就是CMAKE_PREFIX_PATH的设置,要写在cmakelists.txt里。
cmake_minimum_required(VERSION 2.8.11)
project(testproject)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_PREFIX_PATH "E:\\Qt\\5.9.0\\5.9\\msvc2015_64\\")
# Find the QtWidgets library
find_package(Qt5Widgets)
# Tell CMake to create the helloworld executable
add_executable(helloworld main.cpp mainwindow.cpp mainwindow.h)
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld Qt5::Widgets)
然后
cmake -G "Visual Studio 14 Win64" Cmakelists.txt所在路径,即可生成visual studio所需的sln文件且编译通过。