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. How to rename Target Executable using CMake in Qt6?
Forum Updated to NodeBB v4.3 + New Features

How to rename Target Executable using CMake in Qt6?

Scheduled Pinned Locked Moved Solved General and Desktop
qt 6qt6executablecmake
4 Posts 2 Posters 1.8k 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.
  • K Offline
    K Offline
    Kevin470
    wrote on 27 May 2022, 07:42 last edited by
    #1

    Is there a possibility to rename my Executable file created through CMake for Qt6 ?
    In QMake, I believe if the TARGET is renamed in the .pro file, the build creates an executable with that name. Is there a similar method in CMake?

    Thanks.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 27 May 2022, 14:32 last edited by
      #2

      Just change the name of the target (the first argument you pass to add_executable)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      K 1 Reply Last reply 28 May 2022, 06:28
      2
      • V VRonin
        27 May 2022, 14:32

        Just change the name of the target (the first argument you pass to add_executable)

        K Offline
        K Offline
        Kevin470
        wrote on 28 May 2022, 06:28 last edited by Kevin470
        #3

        @VRonin Thank you for your response.

        This is the CMake code I have:

        cmake_minimum_required(VERSION 3.5)
        
        project(ProjectName1 VERSION 0.1 LANGUAGES CXX)
        
        set(CMAKE_INCLUDE_CURRENT_DIR ON)
        
        set(CMAKE_AUTOUIC ON)
        set(CMAKE_AUTOMOC ON)
        set(CMAKE_AUTORCC ON)
        
        set(CMAKE_CXX_STANDARD 17)
        set(CMAKE_CXX_STANDARD_REQUIRED ON)
        
        find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
        find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
        find_package(Qt6 REQUIRED COMPONENTS SerialPort)
        
        set(PROJECT_SOURCES
                main.cpp
                mainwindow.cpp
                mainwindow.h
                mainwindow.ui
                serialComm.h
                serialComm.cpp
        )
        
        if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
            qt_add_executable(ProjectName1
                MANUAL_FINALIZATION
                ${PROJECT_SOURCES}
            )
        # Define target properties for Android with Qt 6 as:
        #    set_property(TARGET ProjectName1 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
        #                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
        # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
        else()
            if(ANDROID)
                add_library(ProjectName1 SHARED
                    ${PROJECT_SOURCES}
                )
        # Define properties for Android with Qt 5 after find_package() calls as:
        #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
            else()
                add_executable(ProjectName1
                    ${PROJECT_SOURCES}
                )
            endif()
        endif()
        
        target_link_libraries(ProjectName1 PRIVATE
            Qt${QT_VERSION_MAJOR}::Widgets
            Qt::SerialPort)
        
        set_target_properties(ProjectName1 PROPERTIES
            MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
            MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
            MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
            MACOSX_BUNDLE TRUE
            WIN32_EXECUTABLE TRUE
        )
        
        if(QT_VERSION_MAJOR EQUAL 6)
            qt_finalize_executable(ProjectName1)
        endif()
        

        If I change the argument to qt_add_executable from ProjectName1 to ProjectName2, I get an error building the CMake file. It says - "Cannot specify link libraries for target "ProjectName1" which is not built by this project."

        And if I edit the argument for add_executable under if(!ANDROID), it builds under the old name.

        Is there some information that I am missing?

        V 1 Reply Last reply 28 May 2022, 16:25
        0
        • K Kevin470
          28 May 2022, 06:28

          @VRonin Thank you for your response.

          This is the CMake code I have:

          cmake_minimum_required(VERSION 3.5)
          
          project(ProjectName1 VERSION 0.1 LANGUAGES CXX)
          
          set(CMAKE_INCLUDE_CURRENT_DIR ON)
          
          set(CMAKE_AUTOUIC ON)
          set(CMAKE_AUTOMOC ON)
          set(CMAKE_AUTORCC ON)
          
          set(CMAKE_CXX_STANDARD 17)
          set(CMAKE_CXX_STANDARD_REQUIRED ON)
          
          find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
          find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
          find_package(Qt6 REQUIRED COMPONENTS SerialPort)
          
          set(PROJECT_SOURCES
                  main.cpp
                  mainwindow.cpp
                  mainwindow.h
                  mainwindow.ui
                  serialComm.h
                  serialComm.cpp
          )
          
          if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
              qt_add_executable(ProjectName1
                  MANUAL_FINALIZATION
                  ${PROJECT_SOURCES}
              )
          # Define target properties for Android with Qt 6 as:
          #    set_property(TARGET ProjectName1 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
          #                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
          # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
          else()
              if(ANDROID)
                  add_library(ProjectName1 SHARED
                      ${PROJECT_SOURCES}
                  )
          # Define properties for Android with Qt 5 after find_package() calls as:
          #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
              else()
                  add_executable(ProjectName1
                      ${PROJECT_SOURCES}
                  )
              endif()
          endif()
          
          target_link_libraries(ProjectName1 PRIVATE
              Qt${QT_VERSION_MAJOR}::Widgets
              Qt::SerialPort)
          
          set_target_properties(ProjectName1 PROPERTIES
              MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
              MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
              MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
              MACOSX_BUNDLE TRUE
              WIN32_EXECUTABLE TRUE
          )
          
          if(QT_VERSION_MAJOR EQUAL 6)
              qt_finalize_executable(ProjectName1)
          endif()
          

          If I change the argument to qt_add_executable from ProjectName1 to ProjectName2, I get an error building the CMake file. It says - "Cannot specify link libraries for target "ProjectName1" which is not built by this project."

          And if I edit the argument for add_executable under if(!ANDROID), it builds under the old name.

          Is there some information that I am missing?

          V Offline
          V Offline
          VRonin
          wrote on 28 May 2022, 16:25 last edited by
          #4

          @Kevin470 said in How to rename Target Executable using CMake in Qt6?:

          Is there some information that I am missing?

          Yes, how targets work in CMake. In what you posted above, however, everything is straightforward. Just do a find-and-replace for all instances of ProjectName1 to ProjectName2

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3

          2/4

          27 May 2022, 14:32

          • Login

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