CMake: How to show headers in “Header files” in Visual Studio project?

有些话、适合烂在心里 提交于 2021-02-07 23:47:11

问题


I have created a simple library project in C++ and added CMake file to automatically generate a Visual Studio project. My small project contains only 2 files:

include/
     testproject/
         testproject.h
src/
    testproject.cpp

CMakeLists.txt

Header file now in External Dependencies (screenshot). How to display it in the section "Headers"? (or any other. Just not "External Dependencies")

CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(PROJECTNAME testproject)

PROJECT(${PROJECTNAME})

FILE(GLOB MY_HEADERS "include/*.h")
FILE(GLOB MY_SOURCES "src/*.cpp")

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
ADD_LIBRARY(
    ${PROJECTNAME} STATIC
    ${MY_HEADERS} ${MY_SOURCES}
)

Note: If change dirs struct to

include/
     testproject.h
src/
    testproject.cpp

CMakeLists.txt

result will be like on a screenshot. Header file in "Header files". But I need in previous project structure


回答1:


Use GLOB_RECURSE:

GLOB_RECURSE will generate a list similar to the regular GLOB, except it will traverse all the subdirectories of the matched directory and match the files. Subdirectories that are symlinks are only traversed if FOLLOW_SYMLINKS is given or cmake policy CMP0009 is not set to NEW. See cmake –help-policy CMP0009 for more information.

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(PROJECTNAME testproject)

PROJECT(${PROJECTNAME})

FILE(GLOB_RECURSE MY_HEADERS "include/*.h")
FILE(GLOB MY_SOURCES "src/*.cpp")

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
ADD_LIBRARY(
    ${PROJECTNAME} STATIC
    ${MY_HEADERS} ${MY_SOURCES}
)


来源:https://stackoverflow.com/questions/34007099/cmake-how-to-show-headers-in-header-files-in-visual-studio-project

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