What is the difference between scope and linkage?

て烟熏妆下的殇ゞ 提交于 2021-01-27 05:22:31

问题


I tried different websites but I don't get it. Could you explain it in simple english?


回答1:


"scope" is a namespace of the compiler; "linkage" is about compiled units.

I explain a bit more: A variable declared in a function has the scope of that function, i.e. it is visible only within that function. A variable declared as static in a source file, can be seen only by the code in that source file (and all included files!). Variables can also have global scope: they can be referred to in a source file, but not declared (allocated) in that source file but declared in another source file.

In stead of "source file" we should say "compilation unit" as it is the C source file being compiled, plus all included files. Scope refers to everything the compiler can "see" in a compilation unit. These are namespaces.

After compilation of a project there are a number of object files, one for each compile unit. Each may refer to variables used that are not declared in the compile unit. The linker must now resolve these references between object files: linkage.

This also holds for functions.




回答2:


Keep reading on your page (http://msdn.microsoft.com/en-us/library/teta4z44.aspx). This is talking about visibility of objects between translation units (source files). It first talks about "internal linkage": objects defined as static, unique to the translation unit but available throughout.

Next it talks about "external linkage": a like-level object not declared static. These are shared between translation units.

Finally, "no linkage": an object such as a variable within a function, not declared extern, which is unique to that scope.

If you follow the links on the bottom of the page, it's all explained.



来源:https://stackoverflow.com/questions/26580038/what-is-the-difference-between-scope-and-linkage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!