Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Complete but simple CMake example
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Complete but simple CMake example

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 13.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    markc
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      markc
      wrote on last edited by
      #2

      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)@

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JohnKaul
        wrote on last edited by
        #3

        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()@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          markc
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JohnKaul
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aaichert
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aaichert
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved