QTCreator 和 VisualStudio 使用 CMake 进行代码编译

QTCreator and VisualStudio Code compilation with CMake

我有一个关于程序编译的问题。我不知道为什么相同的 CMakeLists.txt 在 QTcreator 中编译而不是在 Visual Studio 中编译。我使用的是完全相同的编译器,它是 Windows 的 MinGW。我已经阅读了文档,它编译得很好 EXCEPT 如果使用翻译文件 untitled_fr_FR.ts .

请参阅CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

#---------------------------------------------------------------------    
# I ADD THIS ONE TO COMPILE IN VSCODE AS ASKED IN THE DOCUMENTATION
#---------------------------------------------------------------------
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:\Qt\5.15.0\mingw81_64")
#---------------------------------------------------------------------

project(untitled LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Widgets LinguistTools REQUIRED)

set(TS_FILES untitled_fr_FR.ts)

  add_executable(untitled
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    ${TS_FILES}
  )

target_link_libraries(untitled PRIVATE Qt5::Widgets)

qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})

此代码在 QTCreator 中运行良好,但在 VSCode 中出现此错误:

[main] Building folder: untitled 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/remi/Desktop/ok/untitled/build --config Debug --target all -- -j 6
[build] [ 16%] Automatic MOC and UIC for target untitled
[build] [ 16%] Built target untitled_autogen
[build] mingw32-make.exe[2]: *** No rule to make target '../', needed by '../untitled_fr_FR.ts'.  Stop.
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:95: CMakeFiles/untitled.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:103: all] Error 2
[build] Build finished with exit code 2

***** No rule to make target '../', needed by '../untitled_fr_FR.ts'.  Stop.**

我该如何解决这个问题?我怎样才能弄清楚 QTCreator 在 CMake file/env 中改变了什么来编译 .ts 文件?

非常感谢您的帮助,希望我清楚,不要打扰您。

包含翻译的最佳方式是不使用此方法。编译程序的 CMakeLists.txt 是:

cmake_minimum_required(VERSION 3.5)

#---------------------------------------------------------------------    
# I ADD THIS ONE TO COMPILE IN VSCODE AS ASKED IN THE DOCUMENTATION
#---------------------------------------------------------------------
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:\Qt\5.15.0\mingw81_64")
#---------------------------------------------------------------------

project(untitled LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

  add_executable(untitled
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )

target_link_libraries(untitled PRIVATE Qt5::Widgets)

如果你想编译它(在 Qt 环境之外你将需要使用这个:

cmake_minimum_required(VERSION 3.5)

#---------------------------------------------------------------------    
# I ADD THIS ONE TO COMPILE IN VSCODE AS ASKED IN THE DOCUMENTATION
#---------------------------------------------------------------------
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:\Qt\5.15.0\mingw81_64")
#---------------------------------------------------------------------

project(untitled LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Widgets LinguistTools REQUIRED)

set(TS_FILES mainwindow.ts)

  add_executable(untitled
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    ${TS_FILES}
  )

target_link_libraries(untitled PRIVATE Qt5::Widgets)

qt5_create_translation(QM_FILES ${TS_FILES})

并在mainwindow.ts

中重命名untitled_fr_FR.ts