我有一个“Common.h”文件,它存储了在我的项目中重复使用的所有字符串.
namespace Common{ static const std::string mystring = "IamAwesum"; }
include "Common.h"
然后我需要我使用的字符串:
Common::mystring
现在我注意到在Xcode中,目标中没有包含“.h”文件.但似乎我的程序正常工作正常.有人可以解释.h文件会发生什么吗?这是如何运作的 ?是因为“包含”“Common.h”的文件有所有数据的副本吗?
解决方法
标头不会被编译(异常是预编译的标头,常见的构建优化技术,并且是错误的),但是,正如OP怀疑的那样,它被复制到使用它们的文件中.
来自C标准中的[cpp.include]
A preprocessing directive of the form
# include
” q-char-sequence” new-linecauses the replacement of that directive by the entire contents of the source file identified by the specified sequence between the ” delimiters.
包含的文件被粘贴到#include指令所在的正在编译的文件中,替换#include.
因此,如果没有包含文件,它永远不会被替换为正在编译的文件,并且文件没有任何反应.
包含的文件不需要在项目,目标makefile或者你拥有的任何地方列出.包含该文件完全取决于执行包含的cpp文件,尽管通常会出现一个查找包含标题的位置列表,以从源代码中抽象出工具链和库.