QtCreator CMake project - how to show all project

2020-02-10 12:02发布

问题:

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.

回答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)



回答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:

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



回答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).



回答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)


回答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.



回答6:

There is a closed bug report about this issue: CMake project shows no files.

In that particular case the issue was with the chosen generator, Ninja, which is not well supported by QtCreator.

Please change that to "CodeBlocks - Ninja". Creator needs the CodeBlocks extra generator.

You should see a warning about that when hovering the kit (and the kit should have a warning icon in front of its name).

Using CodeBlocks - Ninja solved it for me too.

Overall, it may help to try up a few generators...