C++ QtCreator CMake 项目 - 如何显示所有项目文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28384935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QtCreator CMake project - how to show all project files
提问by Irbis
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.
我使用 QtCreator 打开 CMake 项目。除了 CMakeLists.txt 之外的一些目录仅包含头文件 *.h,对于这些目录,项目树视图中的 QtCreator 仅显示 CMakeLists.txt。如何解决?我需要查看来自 QtCreator 的所有项目文件。
采纳答案by Slava
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:
将项目作为文件系统查看根本不是解决方案,因为例如您的项目编辑器设置将不适用。而且我不喜欢向可执行目标添加标头,因为它们实际上并不属于那里。你有效地削弱了项目文件以与一个特定的 IDE 一起很好地工作......不好。恕我直言,更清洁的选项是:
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)
作为奖励,您可以将包含显示在单独的文件夹中。(借自https://cmake.org/pipermail/cmake/2012-August/051811.html)
回答by Jingjie Zheng
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.
如果您仍然需要项目模式,您可能希望通过单击右侧的第二个按钮将您的项目视图拆分为两个。
回答by Meysam Mohammadi
You should add header files to the list of your source files: add_executable(${Executable} ${Sources} ${headers})
您应该将头文件添加到源文件列表中: add_executable(${Executable} ${Sources} ${headers})
You can use GLOB_RECURSE
if have many header files:
GLOB_RECURSE
如果有很多头文件,您可以使用:
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).
不要忘记再次运行 CMake (Build>Run Cmake)。
回答by daminetreg
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).
基于另一个提出相同问题的线程,我找到了该问题的通用解决方案,适用于所有 IDE(至少使用 QtCreator 和 Visual Studio 进行了测试)。
Can be found here : https://github.com/sauter-hq/cmake-ide-support
可以在这里找到: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 :
然后用于 CMakeLists 中的任何目标,添加以下调用(可以在最顶层的 CMakeLists.txt 中进行所有 add_subdirectory :
include(add_ide_support.cmake)
target_add_ide_support(some-target)
回答by Monah Tuk
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.
你可以试试CMakeProjectManager2。用于显示已传播到上游的所有文件作为概念证明的代码。应用了概念,但由于某些原因无法按原样应用代码。因此,上游的简单等待功能。
回答by AntonyG
There is a closed bug report about this issue: CMake project shows no files.
有一个关于此问题的已关闭错误报告:CMake 项目显示没有文件。
In that particular case the issue was with the chosen generator, Ninja, which is not well supported by QtCreator.
在这种特殊情况下,问题出在所选的生成器Ninja 上,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).
请将其更改为“CodeBlocks - Ninja”。Creator 需要 CodeBlocks 额外的生成器。
当您将鼠标悬停在套件上时,您应该会看到一个警告(并且套件的名称前应该有一个警告图标)。
Using CodeBlocks - Ninjasolved it for me too.
使用CodeBlocks - Ninja也为我解决了这个问题。
Overall, it may help to try up a few generators...
总的来说,尝试一些生成器可能会有所帮助......