Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to compile Qt program using CMake

📅 2014-Oct-09 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cmake, qt ⬩ 📚 Archive

Qt is a fantastic library, but it comes with a lot of dependencies. You typically need to use its IDE and compilation tools. If you are already working with standard tools for editing code and compiling, then this generates a huge disadvantage. If you are using CMake, it has support for compiling Qt programs. And hopefully this makes it a bit more easier to use Qt for you. 😊

Information on how to write a CMakeLists.txt to compile Qt can be found here. I have created a sample CMakeLists.txt that compiles a Qt program here:

# Check if Qt4 is present
find_package(Qt4 REQUIRED)

# Add Qt headers for compiling
include(
    ${QT_USE_FILE}
    )

# Add Qt definitions for compilation
add_definitions(
    ${QT_DEFINITIONS}
    )

# Your *.h files that need to be processed by MOC
qt4_wrap_cpp(
    FOOBAR_HEADERS_MOC
    FooForm.h
    BarForm.h
    )

# Your *.ui files
qt4_wrap_ui(
    FOOBAR_HEADERS_UI
    FooForm.ui
    BarForm.ui
    )

# Your *.qrc files
qt4_add_resources(
    FOOBAR_HEADERS_QRC
    Foo.qrc
    Bar.qrc
    )

# Add MOC, UI and QRC generated headers for compilation
add_executable(
    foobar
    main.cpp
    star.cpp
    ${FOOBAR_HEADERS_MOC}
    ${FOOBAR_HEADERS_UI}
    ${FOOBAR_HEADERS_QRC}
    )

# Add Qt libraries for linking
target_link_libraries(
    foobar
    ${QT_LIBRARIES}
    )

Tried with: Qt 4, CMake 2.8.12.2 and Ubuntu 14.04


© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email