[SOLVED] Complete but simple CMake example



  • Would anyone know of a complete but SIMPLE example of using CMake with a QtCore based non-gui console application, preferably on Github?

    I'm aware of this article and it even suggests source code but I can't find a link.

    http://developer.qt.nokia.com/quarterly/view/using_cmake_to_build_qt_projects



  • Here is a minimal console application example (main.cpp) with a CMakeLists.txt to build it...

    @#include <QtCore/QCoreApplication>
    #include <QtCore/QDebug>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    qDebug() << "Hello CMake";
    return a.exec();
    }@

    @# mkdir build && cd build

    cmake .. && make

    sudo make install

    cmake_minimum_required(VERSION 2.8)
    project(hello_cmake)
    set(SRCS main.cpp)
    find_package(Qt4 REQUIRED)
    include(${QT_USE_FILE})
    add_executable(hello_cmake ${SRCS})
    target_link_libraries(hello_cmake ${QT_LIBRARIES})
    install(TARGETS hello_cmake DESTINATION /usr/bin)@



  • Since the comments in the above CMakeLists.txt file suggest in-souce builds are not allowed, I would add that check to the file.

    I also like to use the PROJECT_NAME variable instead of retyping out the name of my project.

    The last snip is for the INSTALL location.

    @cmake_minimum_required(VERSION 2.8)
    project(hello_cmake)

    Do not allow in-source builds:

    if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
    message(FATAL_ERROR "In-source builds are not allowed.")
    endif()

    ... <SNIP> ...

    add_executable(${PROJECT_NAME} ${SRCS})

    ... <SNIP> ...

    if(UNIX)
    install( TARGETS ${PROJECT_NAME} DESTINATION /usr/bin)
    # And in addition, maybe?
    # else(WIN32)
    # install(
    # TARGETS ${PROJECT_NAME}
    # DESTINATION "C:/Program Files/${PROJECT_NAME}"
    # )
    endif()@



  • From one Lab Rat to another, thank you and welcome to the forums :-)

    Nice additions. Love the in-source build test.

    I think the install DESTINATION path is relative to CMAKE_INSTALL_PREFIX so "/usr/bin" should probably be just "bin" and allow the cmake -Dvar or env var to set the actual install path. The critical part that I was missing that got me to ask the question turned out to be this line...

    @target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES})@

    or more specifically, a missing reference to QT_LIBRARIES. Related questions, how to emulate these QMake settings?

    . OTHER_FILES=
    . CONFIG += console
    . QT -= gui
    . CMAKE_INSTALL_PREFIX=/usr



  • I apologize for not getting back to you right away (I've been busy).

    Thank you for the welcome.

    [quote author="markc" date="1325270450"]...<snip>...

    Related questions, how to emulate these QMake settings?

    . OTHER_FILES=
    . CONFIG += console
    . QT -= gui
    . CMAKE_INSTALL_PREFIX=/usr
    [/quote]

    I think this may be what you are looking for.
    http://developer.qt.nokia.com/doc/qt-4.7/qmake-environment-reference.html#id-4c6e9803-1d05-47bd-baa7-e139fd2adbd3

    However, I am not all that good with qmake so I could be way off. Sorry if that isn't what you were looking for or is just totally wrong.



  • For Qt5, the CMakeLists.txt should be
    @
    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)

    Find the QtWidgets library

    find_package(Qt5Widgets)

    Tell CMake to create the helloworld executable

    add_executable(helloworld main.cpp)

    Use the Widgets module from Qt 5.

    target_link_libraries(helloworld Qt5::Widgets)
    @

    "http://qt-project.org/doc/qt-5/cmake-manual.html":http://qt-project.org/doc/qt-5/cmake-manual.html



  • For Qt5, the CMakeLists.txt should be
    @
    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)

    Find the QtWidgets library

    find_package(Qt5Widgets)

    Tell CMake to create the helloworld executable

    add_executable(helloworld main.cpp)

    Use the Widgets module from Qt 5.

    target_link_libraries(helloworld Qt5::Widgets)
    @

    "http://qt-project.org/doc/qt-5/cmake-manual.html":http://qt-project.org/doc/qt-5/cmake-manual.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.