简体   繁体   English

如何使用 CMake 从 qmake 获取 model OTHER_FILES?

[英]How can I model OTHER_FILES from qmake with CMake?

In qmake based projects, it's possible to include OTHER_FILES to use them in QtCreator, eg在基于qmake的项目中,可以包含OTHER_FILES以在 QtCreator 中使用它们,例如

QT += core gui widgets
TARGET = example-program
TEMPLATE = app

HEADERS = mainwindow.h
SOURCES = main.cpp mainwindow.cpp

OTHER_FILES = README.md LICENCE examples/network/server.c

That way one can access README.md within QtCreator easily.这样就可以轻松地在 QtCreator 中访问README.md A CMake file with the same target looks like具有相同目标的 CMake 文件看起来像

cmake_minimum_required(VERSION 3.5)
project(example CXX)

set(CMAKE_AUTOMOC ON)

find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED)

set(HEADERS mainwindow.h)
set(SOURCES main.cpp mainwindow.cpp)
set(OTHER_FILES README.md LICENCE examples/network/server.c)

add_executable(example-program ${SOURCES} ${HEADERS})
target_link_libraries(example-program Qt5::Core Qt5::Gui Qt5::Widgets)

However, the OTHER_FILES aren't shown in QtCreator and I have to access them via "File > Open" instead of the project browser.但是, OTHER_FILES没有显示在 QtCreator 中,我必须通过“文件 > 打开”而不是项目浏览器来访问它们。 Is there a workaround?有解决方法吗?

There's no problem with adding non-compiled files as the sources of a CMake target; 将未编译的文件添加为CMake目标的源没有问题。 CMake will simply ignore them if it doesn't recognise their extension. 如果CMake无法识别其扩展名,则只会忽略它们。 So you can easily do this: 因此,您可以轻松地做到这一点:

add_executable(example-program ${SOURCES} ${HEADERS} ${OTHER_FILES})

With files which could be confused for actual sources (based on their extension), there's always the option of setting their HEADER_FILE_ONLY property to true: 对于可能与实际来源混淆的文件(基于其扩展名),总是可以选择将其HEADER_FILE_ONLY属性设置为true:

set_source_files_properties(
  ${OTHER_FILES}
  PROPERTIES
  HEADER_FILE_ONLY TRUE
)

You can use a custom target that depends on the given files: 您可以使用取决于给定文件的自定义目标

add_custom_target(project-related-files SOURCES ${OTHER_FILES})

Since the COMMAND is missing, an empty target will be generated, but it will still depend on the SOURCES . 由于缺少COMMAND ,将生成一个空目标,但仍将取决于SOURCES This is also the official way to include other files into an IDE: 这也是将其他文件包含到IDE中的官方方法:

SOURCES
Specify additional source files to be included in the custom target. 指定要包含在定制目标中的其他源文件。 Specified source files will be added to IDE project files for convenience in editing even if they have no build rules. 即使没有构建规则,指定的源文件也将添加到IDE项目文件中,以便于编辑。

I'll comment that I think both answers are correct, which to use depends on the particulars of the project.我会评论说我认为这两个答案都是正确的,使用哪个取决于项目的细节。

Angew's answer adds the files to a single project, within the project. Angew 的回答将文件添加到项目中的单个项目中。

In my case, Zeta's answer worked better, as I had files at the top-level of a project with subprojects, and having a new folder (with a name I could customize) in QtCreator better fit my needs.就我而言, Zeta 的回答效果更好,因为我在带有子项目的项目的顶层有文件,并且在 QtCreator 中有一个新文件夹(我可以自定义的名称)更适合我的需求。

Adding an additional answer, as I didn't see the differences between the two approaches mentioned.添加一个额外的答案,因为我没有看到提到的两种方法之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM