The method to solve the problem that the IDE environment cmake project does not display the header file

Method 1: add_executable

For example, in Qt Creator, when building a project with CMake, header files are not shown in the project browser by default. To make the header files show up, you can add the corresponding header files in the CMakeLists.txt file.

Here is a simple example showing how to add header files to CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(MyQtProject LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Core Widgets REQUIRED)

# 添加源文件和头文件
set(SOURCE_FILES
    main.cpp
    mainwindow.cpp
)

set(HEADER_FILES
    mainwindow.h
)

# 如果有 UI 文件或资源文件,也可以在此处添加
set(UI_FILES
    mainwindow.ui
)

set(QRC_FILES
    resources.qrc
)

qt5_wrap_ui(UI_HEADERS ${UI_FILES})
qt5_add_resources(QRC_RESOURCES ${QRC_FILES})

add_executable(MyQtProject ${SOURCE_FILES} ${HEADER_FILES} ${UI_HEADERS} ${QRC_RESOURCES})

target_link_libraries(MyQtProject Qt5::Core Qt5::Widgets)

In this example, we first define a variable HEADER_FILESthat contains the header files we want to display in the project. Then, add these header files in add_executable()the function so that they can be displayed in Qt Creator's project browser.

Likewise, we can also add UI files and resource files so they will also appear in the project browser. This qt5_wrap_ui()is qt5_add_resources()done with the and functions.

traverse header files

In order to iterate over all header files in ${CMAKE_SOURCE_DIR}/includethe directory , you can use file(GLOB_RECURSE ...)the command to add them to a variable instead of using set(). Here is an example:

file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")

This command will recursively traverse ${CMAKE_SOURCE_DIR}/includethe directory and .hadd all found files to HEADER_FILESthe variable . Afterwards, you can HEADER_FILESadd add_executable().

However, it should be noted that using GLOB_RECURSEmay cause some problems, because CMake may not automatically re-run when you add or remove header files. In this case, you need to manually re-run CMake to update the project. Therefore, in some cases it may be better to explicitly list all header files.

construction principle

Adding header files to an executable ( add_executable()) doesn't actually affect the compilation process. The main purpose of header files is to provide type declarations, function prototypes, and other information during compilation. When the compiler processes the source files, it will find and process these header files through #includethe directives .

add_executable()The main purpose of adding header files to is to make these header files show up in some integrated development environments (IDEs) such as Qt Creator. This makes it easy to browse and edit header files in the IDE. However, this does not change the compilation process.

Actually, when you compile a project, the compiler only processes source files (such .cppas . #includeDirectives in these source files cause the compiler to find and process the corresponding header files. So, even if you don't add the header files add_executable()to , they will still be processed during compilation as long as the source files include them correctly.

Notice

If you file()have add_executable()added header files to your executable using and , usually the compiler still needs to know where the header files are located. At this time you need to use include_directories()or target_include_directories()to specify the path of the header file.

The difference between these two commands is:

  • include_directories(): This command will add the given directory to the header file search path for all targets. This means that all targets defined in the project will use this path to find header files.
  • target_include_directories(): This command allows you to specify header file search paths for specific targets. This way, only the specified target will use this path to find the header file.

Here is an example target_include_directories()using :

cmake_minimum_required(VERSION 3.5)

project(MyProject LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp")

add_executable(MyProject ${HEADER_FILES} ${SOURCE_FILES})

# 指定头文件搜索路径
target_include_directories(MyProject PRIVATE ${CMAKE_SOURCE_DIR}/include)

In this example, file()we add_executable()added header and source files to the executable using and . Then, target_include_directories()we MyProjectspecify the header file search path for the target so that the compiler can find those header files. PRIVATEThe keyword is used here , indicating that the header file path is only MyProjectvalid for the target.

So, even if you have added the header file to the executable, you still need to use include_directories()or target_include_directories()to specify the location of the header file.

Method 2: source_group

Adding header files to add_executable()is a common solution to CMake projects not displaying header files in IDE environments. However, there are other ways to achieve a similar effect, such as using source groups.

Source file groups allow you to group and categorize files within the IDE. In CMake, you can use source_group()the command to create groups of source files. Here is an example:

cmake_minimum_required(VERSION 3.5)

project(MyQtProject LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Core Widgets REQUIRED)

# 添加源文件和头文件
set(SOURCE_FILES
    main.cpp
    mainwindow.cpp
)

set(HEADER_FILES
    mainwindow.h
)

# 创建源文件组
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${SOURCE_FILES} ${HEADER_FILES})

# 如果有 UI 文件或资源文件,也可以在此处添加
set(UI_FILES
    mainwindow.ui
)

set(QRC_FILES
    resources.qrc
)

qt5_wrap_ui(UI_HEADERS ${UI_FILES})
qt5_add_resources(QRC_RESOURCES ${QRC_FILES})

add_executable(MyQtProject ${SOURCE_FILES} ${HEADER_FILES} ${UI_HEADERS} ${QRC_RESOURCES})

target_link_libraries(MyQtProject Qt5::Core Qt5::Widgets)

In this example, we use source_group()the command to create a source filegroup to group source and header files together. TREEThe parameter indicates that we want to organize files starting from the current source file directory. This will make it possible to display header files in IDEs such as Qt Creator.

However, different IDEs support CMake to varying degrees, so these methods may vary between different IDEs. In general, adding header files to add_executable()is a more general approach.

epilogue

Either way, it will not affect the compilation process. Header files mainly provide type declarations, function prototypes, and other information for source files at compile time. Header files will be included in the source files through #includedirectives , these methods are mainly for convenient display and management of header files in the IDE.

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/130470916