#定义的整个图书馆的范围? [英] #Define's scope throughout library?

查看:92
本文介绍了#定义的整个图书馆的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个恒定的:

#define PI 3.14

说我有多个头文件和源文件的静态库。如果我宣布这个头文件,将其适用范围适用于所有的源文件?或做源文件需要包括 PI

推荐答案

他们将需要包括其中包含的#define PI 3.14文件,否则preprocessor不会读的#define线,随后编译会失败。

They will need to include the file which contains #define PI 3.14, otherwise the preprocessor will not read the #define line, and subsequently the compile will fail.

在C ++中,一个很好的方式去思考编译过程中是每一个人的C ++文件是通过一个preprocessor,这需要所有的#define,#包括第​​一次运行时,和其他preprocessor声明,并取代他们在整个code,然后编译(在这一点上,C ++的文件和任何通过的#include带来了处理仿佛它们是一个非常大的单个文件),然后在这之后,一个连接器会在最终输出preprocess /编译阶段,所有的C ++文件,并把它们组装成一个最终输出文件。在preprocessor(它处理定义)编译阶段之前的作品,不联动期间。

In C++, a good way to think of the compile process is that each individual C++ file is first run through a preprocessor, which takes all the #define, #include, and other preprocessor statements and replaces them throughout the code, then compiled (at this point, the C++ file and anything brought in via #include treated almost as if they were one very large single file), then after that, a linker takes the final output of the preprocess/compile stage for all of the C++ files and assembles them into one final output file. The preprocessor (Which handles the defines) works before the compile stage, not during linkage.

这篇关于#定义的整个图书馆的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆