Visual Studio CMake项目中添加的新文件为”杂项文件”。

huangapple go评论114阅读模式
英文:

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

问题

以下是翻译的内容:

CMakeLists.txt 的一部分如下所示:

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

我正在使用 vs2022 开发这个 CMake 项目。从上面的 CMake 语法可以看出,源文件和头文件将被添加到 `pslite` 项目中。vs2022 确实识别 `src` 目录中的源文件。

然而,当我在 `include` 文件夹内添加了一个新的头文件时,vs2022 将该头文件识别为杂项文件,而不是属于 pslite 项目,这导致我无法准确定位代码的定义等内容。

另一个有趣的现象是,`src` 目录中的头文件被识别了,但新的头文件没有被识别。为什么呢?

我尝试直接在 CMakeLists.txt 中导入头文件,像这样:

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

尽管 `test.h` 头文件在 cmake 项目视图中出现在 `pslite` 库的 `src` 目录中,但它仍然是一个杂项文件。
英文:

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.

答案1

得分: 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.

英文:

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.

huangapple
  • 本文由 发表于 2023年6月6日 15:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定