在 CMake 构建脚本中添加哪些文件?

Which files to add in CMake build scripts?

这可能是一个简单的问题。让我们举一个.c程序的例子:

   #include <stdio.h> 
   #include "Example.h"

   int main(){
   
   int x;
   return 0;
 } 

如果我有一些 C 代码,其中包含一些 .h 文件,即“Example.h”,并且在该头文件中声明了一些在即“Example.c”中实现的函数。现在我想使用 CMake 文件构建这个示例。我是否还需要在构建过程(CMake 文件)中包含“Example.c”文件(“除了 Example.h”)?

在 CMake 中,您必须为要创建的可执行文件定义源文件,是的,这将包括 Example.hExample.c。例如:

add_executable(myprogram Example.c Example.h)

或类似以下内容的内容:

set(SRCS "Example.c" "Example.h")
add_executable(myprogram ${SRCS})

以下link 很好地介绍了 CMake:

https://cliutils.gitlab.io/modern-cmake/chapters/basics.html

虽然:

CMake is smart, and will only compile source file extensions. The headers will be, for most intents and purposes, ignored; the only reason to list them is to get them to show up in IDEs.