Search code examples
qtcmakeqt-creatorqmake

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, e.g.

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. A CMake file with the same target looks like

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. Is there a workaround?


Solution

  • 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. This is also the official way to include other files into an 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.