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

时间:2023-01-15 00:11:55

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中添加什么来列出头文件?首选解决方案是不需要列出每个特定的头文件。

Solution Here is a solution I came with:

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

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

Thanks

谢谢

3 个解决方案

#1


37  

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})

#2


19  

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_group command:

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

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

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

请注意,您还必须在Xcode中执行相同的操作。

#3


9  

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)

#1


37  

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})

#2


19  

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_group command:

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

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

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

请注意,您还必须在Xcode中执行相同的操作。

#3


9  

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)