Search code examples
cmakeqt-creator

How to list custom files using CMAKE in QtCreator?


Using CMAKE to manage a project under QtCreator, some useful files are not shown in the project browser (e.g. xml files, .ui files, etc.).

Is there a way to explicitly add a custom file for being listed in the project using CMAKE?

#*************************************************************************
# Indicate a related file for the project, only for convenient showing in
# project, does not mean dependency, inclusion, but just related.
function ("IndicateExternalFile" NAME )
    # TODO: No idea how to make this.

endfunction()

Tries up to now:

file( GLOB "test.xml") #Not showing.

Solution

  • Turning my comments into an answer

    I've given it a try with and you can just add the "custom files" as source files to your library or executable CMake targets to show up in your IDE project:

    enter image description here

    But I've to admit that does not work for non-existing/optional files.

    So you may add if (EXISTS ...) checks to your function like the following example demonstrates:

    cmake_minimum_required(VERSION 2.8)
    
    project(CMakeTest)
    add_executable(${PROJECT_NAME} "main.cpp")
    
    function(IndicateExternalFile _target)
        foreach(_file IN ITEMS ${ARGN})
            if ((IS_ABSOLUTE "${_file}" AND EXISTS "${_file}") OR
                (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_file}"))
                target_sources(${_target} PRIVATE "${_file}")
            endif()
        endforeach()
    endfunction()
    
    file(WRITE "text.xml" "")
    IndicateExternalFile(${PROJECT_NAME} "text.xml" "test2.xml")
    

    Note: You need an existing target to add "sources" with target_sources().