简体   繁体   English

C ++头文件和声明到底是什么?

[英]What exactly is a C++ header file and declaration?

I know the basics about header files and forward declarations but I'm wondering, if I declare the exact same thing in two separate header files and then compile that, will that work? 我知道有关头文件和转发声明的基础知识,但我想知道,如果我在两个单独的头文件中声明完全相同的内容然后进行编译,那行得通吗?

Is the C++ interface, in this case, portable, I mean, If I have two libs and they share the same interface (declaration or what not) somewhere could I theoretically just replicate the same declaration in the program and actually compile that, or if not, why? C ++接口是可移植的,在这种情况下,我的意思是,如果我有两个库,并且它们在同一地方共享相同的接口(声明或不声明),那么我理论上可以在程序中复制相同的声明并进行实际编译,或者不是为什么?

How would C++ for instance be able to tell the difference between two identical declarations in two different files? 例如,C ++如何分辨两个不同文件中两个相同声明之间的区别?

Say I had two distinct libs but they shared some interface, they are compiled separately but by the same tools, would it be possible to in a future step bring these to libs together and actually pass the interface between these two libs as if it was the same, compatible interface, even though it was originally compiled from different (but identical) header files? 假设我有两个截然不同的库,但是它们共享一些接口,它们分别编译但使用相同的工具,是否有可能在以后的步骤中将它们合并到库中,并实际上在这两个库之间传递接口,就好像是相同的兼容接口,即使它最初是从不同(但相同)的头文件中编译的呢?

Function declarations, variable declarations and class definitions for a given identifier may appear in the source code as often as you like. 给定标识符的函数声明,变量声明和类定义可能会根据需要在源代码中出现的频率很高。 They just have to be identical each time they appear (which is automatic if you include a given header file in multiple translation units). 它们每次出现时都必须相同(如果在多个翻译单元中包含给定的头文件,这将是自动的)。

It is only the definition of functions, variables and class member functions that must appear precisely once (with a special rule for inlined functions). 仅函数,变量和类成员函数的定义必须精确地出现一次(对于内联函数有特殊规则)。

(It's a little different for templates: Template definitions may appear repeatedly, but again all occurrences have to be identical. But templates require some non-trivial deduplication work from the linker.) (模板略有不同:模板定义可能重复出现,但所有出现的对象必须相同。但是模板需要链接器进行一些不重要的重复数据删除工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM