CMake, QtCreator and header files

半城伤御伤魂 提交于 2021-01-27 05:40:13

问题


This is not exactly a compilation related problem, but much more a visual issue ...

I have a project, set up with CMake (this is not a Qt project). When I open this project with QtCreator it nicely finds all the related files, and the files in the project browser are in alphabetical order like:

Project
 + abc.cpp
 + abc.h
 + def.cpp
 + def.h

However, if I work with QtCreator and a QMake based project, the headers and sources are nicely separated like:

Project
 + Headers
 |  + abc.h
 |  + def.h
 + Sources 
    + abc.cpp
    + def.cpp

Question: How to achieve this separation of headers and sources with Qt Creator? I have tried with SOURCE_GROUP("Headers" FILES abc.h def.h) and although that this works (to some extent) with Visual Studio, it does not work with Qt Creator. Any other tips?


回答1:


Specifically for CMake, do something like:

ADD_CUSTOM_COMMAND(OUTPUT
    ${CMAKE_BINARY_DIR}/include/res.h ${CMAKE_BINARY_DIR}/Sources/abc.cpp
    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/Headers/abc.h
DEPENDS ${CMAKE_SOURCE_DIR}/resources/res.gen)

For further options and details:

  • [CMake] Generated Source Files

Of course you can still use Qt Creator if you like for editing your files. In the Qt Creator at the main control panel, in the upper left menu you need to select:

File > New File or Project > Files and Classes > C++ :

Then you have three options:

  • C++ Class: This option generate both sub-folders for source file and header file, automatically including a header and a source file into the respective folders.
  • C++ Source File: This will create and add only the source file with the respective sub-folder to your project.
  • C++ Header File: This will create and add only header file with the respective sub-folder to your project.

Select the one you need and it will be under your project folder organized in the way you want it.



来源:https://stackoverflow.com/questions/22298302/cmake-qtcreator-and-header-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!