CMake,QtCreator和头文件 [英] CMake, QtCreator and header files

查看:135
本文介绍了CMake,QtCreator和头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不完全是与编译相关的问题,而更多是视觉问题...

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

我有一个使用CMake设置的项目(这不是Qt项目).当我使用QtCreator打开该项目时,它会很好地找到所有相关文件,并且项目浏览器中的文件按字母顺序排列,例如:

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

但是,如果我使用QtCreator和基于QMake的项目,则标题和源会很好地分开,例如:

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

问题:如何使用Qt Creator实现标题和源的这种分离?我已经尝试过使用 SOURCE_GROUP("Headers" FILES abc.h def.h),尽管这在一定程度上适用于Visual Studio,但不适用于Qt Creator.还有其他提示吗?

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?

推荐答案

专门针对CMake,请执行以下操作:

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:

当然,如果您想编辑文件,仍然可以使用Qt Creator.在主控制面板的Qt Creator中,在左上方菜单中,需要选择:

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++ :

那么您有三个选择:

  • C ++类:此选项会同时生成源文件和头文件的子文件夹,并自动将头文件和源文件包含到相应的文件夹中.
  • C ++源文件:这将创建源文件并仅将带有相应子文件夹的源文件添加到您的项目中.
  • C ++头文件:这样将只创建带有相应子文件夹的头文件并将其添加到您的项目中.
  • 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.

这篇关于CMake,QtCreator和头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆