cmake包含头文件到每个源文件

时间:2023-02-10 02:05:35

I actually have a simple question, but couldn't find an answer. Maybe you can point me to a duplicate. So, the question is: is it possible to tell cmake to instruct a compiler to automatically include some header at the beginning of every source file, so there would be no need to put #include foo.h? Thanks!

我其实有一个简单的问题,但是找不到答案。也许你可以给我指一份副本。所以,问题是:是否可以告诉cmake,让编译器在每个源文件的开头自动包含一些头文件,这样就不需要加上#include .h?谢谢!

1 个解决方案

#1


11  

CMake doesn't have a feature for this specific use case, but as you've hinted, compilers such as GCC have the -include flag which acts as if there was an #include "foo.h" in the source file, and since CMake can pass arguments to compilers, you can do it via add_definitions.

CMake没有这个特定用例的特性,但是正如您所暗示的,像GCC这样的编译器有-include标志,它的作用就好像有一个#include“foo”。由于CMake可以将参数传递给编译器,所以可以通过add_definition来实现。

This answer covers what the flag is for GCC, Clang and MSVC which should cover a lot of bases. So in CMake, detect what the compiler is and pass the appropriate flag.

这个答案包含了GCC、Clang和MSVC的标志,它们应该包含很多基础。因此,在CMake中,检测编译器是什么,并传递适当的标志。

Here's what the CMake code might look like:

CMake代码是这样的:

if(MSVC)
    add_definitions(/FI"foo.h")
else()
    # GCC or Clang
    add_definitions(-include foo.h)
endif()

Comments

In general, doing this is a bad idea. Code inspection tools (like IDEs, or doxygen) will be confused by it, not to mention other humans looking at the code. If not all source files actually require the definition, adding extra #includes will slow down compile time. If you actually do need the same header (and it's not a system header) in all your source files, it may be symptomatic of high coupling in your code. And for what benefit? Not having to add one line to your files?

一般来说,这样做是个坏主意。代码检查工具(如ide或doxygen)将会被它所迷惑,更不用说其他查看代码的人了。如果不是所有的源文件都需要定义,添加额外的#include将会降低编译时间。如果您确实需要在所有源文件中使用相同的头(而不是系统头),那么这可能是代码中高耦合的症状。和有什么好处?不需要在文件中添加一行吗?

However, it's necessary to note that compilers support this for a reason; there are a few weird edge cases (example 1, example 2) where it's a useful thing to do.

但是,需要注意的是,编译器支持这一点是有原因的;有一些奇怪的边缘情况(示例1,示例2),在这些情况下它是有用的。

Just be aware that you're doing this for the right reasons.

只要意识到你这么做的理由是正确的。

#1


11  

CMake doesn't have a feature for this specific use case, but as you've hinted, compilers such as GCC have the -include flag which acts as if there was an #include "foo.h" in the source file, and since CMake can pass arguments to compilers, you can do it via add_definitions.

CMake没有这个特定用例的特性,但是正如您所暗示的,像GCC这样的编译器有-include标志,它的作用就好像有一个#include“foo”。由于CMake可以将参数传递给编译器,所以可以通过add_definition来实现。

This answer covers what the flag is for GCC, Clang and MSVC which should cover a lot of bases. So in CMake, detect what the compiler is and pass the appropriate flag.

这个答案包含了GCC、Clang和MSVC的标志,它们应该包含很多基础。因此,在CMake中,检测编译器是什么,并传递适当的标志。

Here's what the CMake code might look like:

CMake代码是这样的:

if(MSVC)
    add_definitions(/FI"foo.h")
else()
    # GCC or Clang
    add_definitions(-include foo.h)
endif()

Comments

In general, doing this is a bad idea. Code inspection tools (like IDEs, or doxygen) will be confused by it, not to mention other humans looking at the code. If not all source files actually require the definition, adding extra #includes will slow down compile time. If you actually do need the same header (and it's not a system header) in all your source files, it may be symptomatic of high coupling in your code. And for what benefit? Not having to add one line to your files?

一般来说,这样做是个坏主意。代码检查工具(如ide或doxygen)将会被它所迷惑,更不用说其他查看代码的人了。如果不是所有的源文件都需要定义,添加额外的#include将会降低编译时间。如果您确实需要在所有源文件中使用相同的头(而不是系统头),那么这可能是代码中高耦合的症状。和有什么好处?不需要在文件中添加一行吗?

However, it's necessary to note that compilers support this for a reason; there are a few weird edge cases (example 1, example 2) where it's a useful thing to do.

但是,需要注意的是,编译器支持这一点是有原因的;有一些奇怪的边缘情况(示例1,示例2),在这些情况下它是有用的。

Just be aware that you're doing this for the right reasons.

只要意识到你这么做的理由是正确的。