我觉得QT5库在与cmake链接时没有正确链接。 我有一个cmake文件,设置了使用QT4或QT5的选项:
if(USE_Qt5)
if(NOT Qt5_PREFIX_PATH)
set(Qt5_PREFIX_PATH /Applications/Qt/5.2.1/clang_64)
endif(NOT Qt5_PREFIX_PATH)
set(WINSDK_LIB_DIR
"C:/Program Files (x86)/Windows Kits/8.0/Lib/win8/um/x64"
"C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Lib"
)
set(CMAKE_PREFIX_PATH ${3RDPARTY} ${Qt5_PREFIX_PATH} ${WINSDK_LIB_DIR} ${CMAKE_PREFIX_PATH})
message(STATUS "Qt5 prefix path set to: ${CMAKE_PREFIX_PATH}")
find_package(Qt5Widgets)
find_package(Qt5OpenGL)
endif(USE_Qt5)
if(USE_Qt4)
file(GLOB QT4ROOTS ${3RDPARTY}/Qt/*/bin)
find_program(QT_QMAKE_EXECUTABLE NAMES qmake qmake4 qmake-qt4 PATHS ${QT4ROOTS})
find_package(Qt4 REQUIRED)
set(QT_USE_QTOPENGL TRUE)
endif(USE_Qt4)
如果选择qt4没问题,编译并执行。 编译Qt5会返回很多错误,在我看来是一个链接问题:
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:52:24: error: variable has incomplete type 'class Q_WIDGETS_EXPORT'
class Q_WIDGETS_EXPORT QSizePolicy
^
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:52:7: note: forward declaration of 'Q_WIDGETS_EXPORT'
class Q_WIDGETS_EXPORT QSizePolicy
^
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:52:35: error: expected ';' after top level declarator
class Q_WIDGETS_EXPORT QSizePolicy
^
;
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:163:31: error: expected a class or namespace
Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
???
这是我链接库:
if(USE_Qt4 OR USE_Qt5)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
# disable autolinking to qmain
cmake_policy(SET CMP0020 OLD)
set(USE_QT true)
add_definitions(-DWITH_QT)
endif(USE_Qt4 OR USE_Qt5)
if(USE_Qt5)
#target_link_libraries(myProject
# Qt5::Widgets
#)
qt5_use_modules(myProject Widgets)
endif(USE_Qt5)