Search code examples
c++cmakevisual-studio-2022cmakelists-optionscmake-language

Why is a new file added in visual studio cmake project a miscellaneous files?


Part of teh CMakeLists.txt are as followed:

add_library(pslite)
file(GLOB_RECURSE SOURCE "src/*.cc")
target_include_directories(pslite PUBLIC "${PROJECT_SOURCE_DIR}/include/")
target_sources(pslite PRIVATE ${SOURCE})

I am using vs2022 to develop this cmake project. From the cmake syntax above, the source and header files will be added to the pslite project. vs2022 does indeed recognize source files in the src directory.

However, when I added a new header file inside the include folder, vs2022 recognized that header file as miscellaneous and not in pslite's project, which prevented me from accurately locating things like the definition of the code.

Another interesting point is that the header files in the src directory are recognized, but the new header files are not. Why?

I tried to import the header file directly in CMakeLists.txt, like this:

target_sources(psquic PRIVATE ${SOURCE} "src/test.h")

Although the test.h header appears in the src directory of the pslite library in the cmake project view, it is still a miscellaneous file.


Solution

  • Now I find the reason why headers are shown as miscellaneous fileS in VS2022. The headers are actually copied into the source file at the location of #include "[name].h" and do not participate in compilation themselves.

    The header in my project has not been included by any of the source files yet, so vs2022 recognizes that head file as a miscellaneous file.

    In fact, CMakeList doesn't need to use the target_source command to add headers to target. It just needs to provide a correct header address for the source file using the target_include_directories command.