C/C++项目中.h和.inc文件区别
原问题:Difference between .h files and .inc files in c
C/C++的标准惯例是将class、function的声明信息写在.h文件中。.c文件写class实现、function实现、变量定义等等。然而对于template来说,它既不是class也不是function,而是可以生成一组class或function的东西。编译器(compiler)为了给template生成代码,他需要看到声明(declaration )和定义(definition ),因此他们必须不被包含在.h里面。
为了使声明、定义分隔开,定义泻在自己文件内部,即.inc文件,然后在.h文件的末尾包含进来。当然除了.inc的形式,还可能有许多其他的写法.inc
, .imp
, .impl
, .tpp
, etc.
英文原版回答
.inc
files are often associated with templated classes and functions.Standard classes and functions are declared with a
.h
file and then defined with a.cpp
file. However, a template is neither a class nor a function but a pattern that is used to generate a family of classes or functions. In order for the compiler to generate the code for the template, it needs to see both the declaration and definition and therefore they both must be included in the.h
file.To keep the declaration and definition separate, the definition is placed in its own file and included at the end of the
.h
file. This file will have one of many possible file extensions.inc
,.imp
,.impl
,.tpp
, etc.Declaration example:
123456789101112131415// Foo.h
#ifndef FOO_H
#define FOO_H
template
<
typename
T>
class
Foo {
public
:
Foo();
void
DoSomething(T x);
private
:
T x;
};
#include "Foo.inc"
#endif // FOO_H
Definition example:
123456789101112// Foo.inc
#include "Foo.h"
template
<
typename
T>
Foo<T>::Foo() {
// ...
}
template
<
typename
T>
void
Foo<T>::DoSomething(T x) {
// ...
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决