【问题标题】:How can I link correctly to QT4 libraries in CMake?如何正确链接到 CMake 中的 QT4 库?
【发布时间】:2013-04-20 18:35:15
【问题描述】:

所以我正在尝试构建一个最初使用 CMake 而不是 .pro 文件制作的程序。但是,我无法构建它,因为库声明有问题。例如,以下内容将不起作用:

#include <QtGui/QApplication>

但是,这或多或少有效,但并不完全:

#include <qt4/QtGui/QApplication>

如果我使用上述方法,那么我会收到大量错误,因为编译器说它找不到其他头文件指向的头文件。问题是 qt4 的前缀。 如何在 CMake 文件中正确链接它,以便可以在 qt4 下找到每个标题,而无需更改 QT-SDK 附带的所有标题?


CMAKE 文件:(来源:http://code.metager.de/source/xref/kde/Support/polkit-qt-1/examples/CMakeLists.txt

install(FILES org.qt.policykit.examples.policy DESTINATION ${SHARE_INSTALL_PREFIX}/polkit-1/actions/)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/gui
)

set(polkit_example_SRCS
main.cpp
PkExample.cpp
)

SET(polkit_example_RESOUCES
icons/icons.qrc
)

FIND_PACKAGE(Qt4 REQUIRED)

QT4_WRAP_CPP(polkit_example_MOC_SRCS
PkExample.h
)

QT4_WRAP_UI(polkit_example_UI_SRCS
PkExample.ui
)

QT4_ADD_RESOURCES (qtsourceview_RC_SRCS  ${polkit_example_RESOUCES})

add_executable(polkit-example
${polkit_example_SRCS}
${polkit_example_MOC_SRCS}
${polkit_example_UI_SRCS}
${qtsourceview_RC_SRCS}
)

target_link_libraries(polkit-example
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY}
polkit-qt-gui-1
polkit-qt-core-1
)

#--------Helper Application

# This macro is defined in FindPolkitQt.cmake
macro(dbus_add_activation_system_service _sources)
foreach (_i ${_sources})
    get_filename_component(_service_file ${_i} ABSOLUTE)
    string(REGEX REPLACE "\\.service.*$" ".service" _output_file ${_i})
    set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_output_file})
    configure_file(${_service_file} ${_target})
    install(FILES ${_target} DESTINATION ${SHARE_INSTALL_PREFIX}/dbus-1/system-services )
    #install(FILES ${_target} DESTINATION ${_install_dir})
endforeach (_i ${ARGN})
endmacro(dbus_add_activation_system_service _sources)

set(polkit_example_helper_SRCS
PkExampleHelper.cpp
mainHelper.cpp
)

qt4_add_dbus_adaptor(polkit_example_helper_SRCS
org.qt.policykit.examples.xml
PkExampleHelper.h
PkExampleHelper
)

QT4_WRAP_CPP(polkit_example_helper_MOC_SRCS
PkExampleHelper.h
)

add_executable(polkit-example-helper
${polkit_example_helper_SRCS}
${polkit_example_helper_MOC_SRCS}
)

# see our helper is pretty small :D
target_link_libraries(polkit-example-helper
${QT_QTCORE_LIBRARY}
polkit-qt-core-1
)


dbus_add_activation_system_service(org.qt.policykit.examples.service.in)

install(FILES org.qt.policykit.examples.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/system.d)

【问题讨论】:

  • 你的 CMake 是什么样的?
  • 它很大,但我只是添加了它。
  • 你需要修改include_directories来获取qt4基础目录,那么就不需要修改源了。
  • 在你的 find_package 后面加上INCLUDE(${QT_USE_FILE})。附:相信我,这不是一个巨大的 CMake 文件。 ;)
  • 谢谢!几分钟前我找到了该解决方案,但一直将 INCLUDE(${QT_USE_FILE}) 放在 find_package 之前。这就是为什么它不起作用。再次感谢。

标签: qt4 cmake


【解决方案1】:

据我所知,你已经很接近了。在您的FIND_PACKAGE(Qt4 REQUIRED) 之后添加以下行:

包括(${QT_USE_FILE})

根据the documentation

QT_USE_FILE 指向的文件将通过添加包含目录、预处理器定义和填充包含所有 Qt 库及其依赖项的QT_LIBRARIES 变量来设置您的编译环境。

所以应该为您设置正确的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多