QtCreator CMake project - how to show all project files

26,800

Solution 1

Viewing project as a file system is not a solution at all cause your project editor settings for example would not apply. And I do not like to add headers to executable target, cause they do not actually belong there. You effectively cripple the project file to work nicely with one particular IDE... not good. The cleaner option IMHO would be:

FILE(GLOB_RECURSE LibFiles "include/*.hpp")
add_custom_target(headers SOURCES ${LibFiles})

As a bonus you get your includes shown in a separate folder. (borrowed from https://cmake.org/pipermail/cmake/2012-August/051811.html)

Solution 2

I would suggest you switching your project view to File System. This would display a view where you could view any file you want:

enter image description here

You might want to split your project view into two by clicking the second to right button, if you still desire the Projects mode.

Solution 3

You should add header files to the list of your source files: add_executable(${Executable} ${Sources} ${headers})

You can use GLOB_RECURSE if have many header files:

FILE(GLOB_RECURSE INC_ALL "headers/*.h")
include_directories("headers")
add_executable(main "main.cpp" ${INC_ALL})

Don't forget to run CMake again (Build>Run Cmake).

Solution 4

Based on another thread asking the same question, I found a generic solution to the problem, working for all IDE's (at least tested with QtCreator and Visual Studio).

Can be found here : https://github.com/sauter-hq/cmake-ide-support

# \brief adds for the given target a fake executable targets which allows all
#        headers and symbols to be shown in IDEs.
# \param target_name Which target properties should be added to the IDE support target.
function(target_add_ide_support target_name)
  if (NOT TARGET ${target_name})
    message(FATAL_ERROR "No target defined with name ${target_name}, cannot target_add_ide_support it.")
  endif()

  set (target_for_ide "${target_name}_ide_support")
  if (NOT TARGET ${target_for_ide})
      file(GLOB_RECURSE target_for_ide_srcs "*.h" "*.hpp" "*.hxx" "*.c" "*.cpp" "*.cxx")
      add_executable(${target_for_ide} ${target_for_ide_srcs})
      set_target_properties(${target_for_ide} PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
  endif()

  get_target_property(dirs ${target_name} INCLUDE_DIRECTORIES)
  target_include_directories(${target_for_ide} PRIVATE ${dirs})

endfunction(target_add_ide_support)

Usage is then for any targets in the CMakeLists, add the following call (can be made in top-most CMakeLists.txt after all add_subdirectory :

include(add_ide_support.cmake)
target_add_ide_support(some-target)

Solution 5

You can try CMakeProjectManager2. Code to display all files already propagated to upstream as a proof of concept. Concept applied but code can't be applied as-is for some reasons. So, simple wait feature in upstream.

Share:
26,800
Irbis
Author by

Irbis

Updated on January 17, 2020

Comments

  • Irbis
    Irbis over 4 years

    I use QtCreator to open CMake project. Some directories apart from CMakeLists.txt contains only headers files *.h and for those directories QtCreator in the project tree view shows only CMakeLists.txt. How to fix that ? I need to see all project files from QtCreator.

  • Irbis
    Irbis about 9 years
    Ok, but which statement in CMake files causes that files are visible in the project tree view ?
  • Jingjie Zheng
    Jingjie Zheng about 9 years
    I am not sure. But there are declaratives in CMake specifying source code probably add restriction to what the project tree view is displaying. It is some built-in feature of QtCreator itself, I doubt.
  • Zulan
    Zulan over 8 years
    Unfortunately the file view is flat, and therefore not a sufficient replacement for the project view.
  • void.pointer
    void.pointer over 8 years
    CMake officially does not recommend globbing your sources, because CMake then will not be able to detect when it must regenerate based on added, removed, or renamed files.
  • feedc0de
    feedc0de almost 7 years
    May I ask you what resolution your monitor had when taking this screenshot? It looks very nice!
  • Jingjie Zheng
    Jingjie Zheng almost 7 years
    @DanielBrunner It's a MacBook Pro Retina
  • mikkola
    mikkola over 6 years
    I had a problem with the accepted solution not being able to look up some external headers (from the Pointcloud library PCL) in my own project's header files. Applying this approach instead resolved the issue.
  • Adrian Maire
    Adrian Maire over 4 years
    @feedc0de: to be precise, is not about resolution, but about DPI (dot per inches). You might have a 4K 40" screen with normal DPI (usually ~96) and screenshots will look ok. Or you might have a 4K 16" laptop witch HighDPI (e.g. ~192) and a screenshot look huge.
  • Kiruahxh
    Kiruahxh over 3 years
    File system view is really good when you hide breadcrumb (attachment icon) & select your project in the path scrolldown. In project view, flat list has been fixed in QtCreator 4.11.1 (bugreports.qt.io/browse/QTCREATORBUG-23372)