列出由 cmake 生成的 Visual Studio C++ 项目中的头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1167154/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:05:28  来源:igfitidea点击:

Listing header files in Visual Studio C++ project generated by cmake

c++visual-studio-2008cmake

提问by dimba

I'm building a cmake based build system for our product. The problem is that Visual Studio project, generated by cmake, doesn't display header files in solution browser.

我正在为我们的产品构建一个基于 cmake 的构建系统。问题是由 cmake 生成的 Visual Studio 项目不会在解决方案浏览器中显示头文件。

What I need to add in CMakeList.txt to list header files? The preferred solution is where no need to list each particular header file.

我需要在 CMakeList.txt 中添加什么来列出头文件?首选的解决方案是不需要列出每个特定的头文件。

SolutionHere is a solution I came with:

解决方案这是我带来的解决方案:

file(GLOB_RECURSE INCS "*.h")
add_library(myLib ${SRCS} ${INCS})

Thanks

谢谢

回答by tim_hutton

Just add the header files along with the source files:

只需添加头文件和源文件:

PROJECT (Test)

ADD_EXECUTABLE(Test test.cpp test.h)

Or using variables:

或者使用变量:

PROJECT (Test)

SET(SOURCE
  test.cpp
)

SET(HEADERS
  test.h
)

ADD_EXECUTABLE(Test ${SOURCE} ${HEADERS})

回答by metasim

The basic trick to this is to add the header files to one of the targets (either executable or library). This is particularly irritating because cmake already knows all the header file dependencies and should take care of this for us. You can organize it further using the source_groupcommand:

基本技巧是将头文件添加到目标之一(可执行文件或库)。这尤其令人恼火,因为 cmake 已经知道所有头文件依赖项,应该为我们处理这个问题。您可以使用source_group命令进一步组织它:

  source_group("My Headers" FILES ${MY_HDRS})

Note that you have to do the same thing in Xcode too.

请注意,您也必须在 Xcode 中做同样的事情。

回答by npclaudiu

I had the same problem while working at a build system for a Qt project and I came out with this solution, thanks to the other posts on this page. I included a complete example adapted from my makefiles. Hope this helps!

我在 Qt 项目的构建系统中工作时遇到了同样的问题,我想出了这个解决方案,这要归功于此页面上的其他帖子。我包含了一个改编自我的 makefile 的完整示例。希望这可以帮助!

cmake_minimum_required (VERSION 2.6) 
project (DemoSolution)

find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})

include_directories (../../include/)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

file(GLOB Demo_SOURCES *.cpp)
file(GLOB Demo_HEADERS *.hpp)
file(GLOB Demo_FORMS *.ui)
file(GLOB Demo_RESOURCES resources.qrc)

qt4_wrap_cpp(Demo_MOC ${Demo_HEADERS})
qt4_wrap_ui(Demo_FORMS_HEADERS ${Demo_FORMS})
qt4_add_resources(Demo_RESOURCES_RCC ${Demo_RESOURCES})

source_group("Headers" FILES ${Demo_HEADERS})
source_group("MOC" FILES ${Demo_MOC})

set(QT_USE_QTNETWORK, true)
set(QT_USE_QTSQL, true)
set(QT_USE_QTXML, true)

add_library(Demo SHARED
    ${Demo_SOURCES}
    ${Demo_HEADERS}
    ${Demo_MOC}
    ${Demo_FORMS_HEADERS}
    ${Demo_RESOURCES_RCC}
    )

target_link_libraries(Demo ${QT_LIBRARIES})
add_definitions(-D_DEMO_EXPORTS)

回答by tjwrona1992

I know this answer is really late to the game, but in more recent versions of visual studio you can change the view from "CMake Target Mode" to a "Folder View"

我知道这个答案对游戏来说真的很晚,但是在最新版本的 Visual Studio 中,您可以将视图从“CMake 目标模式”更改为“文件夹视图”

enter image description here

在此处输入图片说明

In this folder view you will be able to see all of your header files.

在此文件夹视图中,您将能够看到所有头文件。

To be honest i'd take simply changing the view in Visual Studio over modifying CMake files with Windows specific hacks any day.

老实说,我每天都会简单地更改 Visual Studio 中的视图,而不是使用 Windows 特定的 hack 修改 CMake 文件。