filecmakegenerate

CMake does not generate .ts files


I am trying to generate *.ts files with my CMake, but it does nothing. It even do not show any message. When I am trying directly with lupdate, it is working, so I don't know where the problem might be. Here my CMakeLists.txt:

cmake_minimum_required(VERSION 3.15 FATAL_ERROR)

project(Band VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(Qt5_DIR "C:/QtOpen/5.13.2/msvc2017_64/lib/cmake/Qt5")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS Designer)    
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set ( SOURCES
Data/Band.cpp  
MainWindow.cpp
)

set ( MOC_HEADERS 
Data/Band.h
MainWindow.h
)

set ( UIS Designer/band.ui)
set ( RESOURCES application.qrc )

find_package(Qt5 COMPONENTS Core Gui Widgets Xml Network PrintSupport LinguistTools REQUIRED)
    
qt5_create_translation(QM_FILES MainWindow.cpp english.ts)
   
add_executable( ${CMAKE_PROJECT_NAME} WIN32 ${SOURCES} ${MOC_HEADERS} ${RESOURCES} ${UIS} ${QM_FILES} icon.rc)
target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC DEBUG_MODE)

target_link_libraries(${CMAKE_PROJECT_NAME} Qt5::Widgets Qt5::Gui Qt5::Xml Qt5::PrintSupport)

Can anyone help? I went through all the tutorials, documentation and answers about this topic, but found nothing that could explain why it does not generate .ts file for me. Thanks in advance.

ref: https://doc.qt.io/qt-5/qtlinguist-cmake-qt5-create-translation.html


Solution

  • I think the problem is that just listing ${QM_FILES} as one of the sources in add_executable doesn't force CMake to understand that it needs to generate those files. I guess this is because normally source files are not generated.

    You can add the dependency more explicitly via a custom target. After the qt5_create_translation line, add this:

    add_custom_target(translations DEPENDS ${QM_FILES})
    

    Then, after the add_executable line, add this:

    add_dependencies(${CMAKE_PROJECT_NAME} translations)
    

    Essentially this says there is a target that depends on the generated translation files and that your executable depends on this target, and thus on the generation of the translation files. This should be enough to force qt5_create_translation to be invoked.