How to rename Target Executable using CMake in Qt6?
Solved
General and Desktop
-
@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?
-
@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