0

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.

Abilyf
  • 1
  • 1
  • 2
    BTW, [why is CMake file GLOB evil?](https://stackoverflow.com/questions/32411963/why-is-cmake-file-glob-evil). And why do you expect to see changes in target `pslite` when changing `psquic`? – Friedrich Jun 06 '23 at 08:37
  • sorry, psquic is my misspelling in describing the problem, it's actually pslite. – Abilyf Jun 06 '23 at 10:53
  • 1
    How exactly are you using CMake with vs? "Open CMake folder" or `cmake -S ... -B ... && cmake --open ...`? Referring to the later case here: Adding the header file to the sources should work. Sometimes VS doesn't refresh everything properly. Check, if forcing a cmake configuration run and reopening the VS solution works: Select the solution in the solution explorer and use the context menu `Open in terminal`. Execute `cmake .`, close the solution in VS and reopen it... – fabian Jun 06 '23 at 16:14

1 Answers1

0

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.

Abilyf
  • 1
  • 1