简体   繁体   English

没有为多个定义生成编译器警告

[英]Compiler warning not generated for multiple definitions

problem I am facing is function with same signature is defined in two .c files and is not giving compile time error. 我面临的问题是在两个.c文件中定义了具有相同签名的函数,并且没有给出编译时错误。 I have included declaration in .h file, which is included to both .c files. 我已经在.h文件中包含了声明,这两个.c文件中都包含了声明。

For example: 例如:

int add(int x, int y) { return x+y;}

same definition is given in two .c files (Say Ac and Bc) and declaration in one .h file which is included in both Ac and Bc But why this is not giving compile time error or How can I make to give them compile error 在两个.c文件(说Ac和Bc)中给出了相同的定义,并在一个包含在Ac和Bc中的.h文件中进行了声明,但是为什么这没有给出编译时错误,或者如何使它们出现编译错误?

Even Linker is not giving any error, it looks it is taking first definition 连Linker都没有给出任何错误,它看起来像是第一个定义

I am using GCC compiler mingw 我正在使用GCC编译器mingw

I found another pattern in this. 我发现了另一个模式。 if I am using this in header file 如果我在头文件中使用它

#ifndef H_H_
#define H_H_

linker is not giving warning warning but If i don't use this Linker gives warning which is expected. 链接器未给出警告警告,但如果我不使用此链接器,则将给出警告。

The compiler doesn't analyze your program as a whole. 编译器不会整体分析您的程序。 It simply processes one .c file at a time. 它一次只处理一个.c文件。 If the declaration in the .h file matches the definition in the .c file, then everything is good as far as the compiler is concerned. 如果.h文件中的声明与.c文件中的定义匹配,则就编译器而言,一切都很好。

The linker will detect that the function was defined twice and will generate a "duplicate symbol" error. 链接器将检测到该函数已定义两次,并将生成“重复符号”错误。

Compiler sees each source file apart from the other. 编译器看到每个源文件彼此分开。 Compiler includes the content of header file(s) into Ac then geneates an object file A.obj from AcAobj file will contain symbols of the variables and functions defined in Ac On the other hand, compiler will process Bc apart without checking Ac, or any other source file, content. 编译器将头文件的内容包含到Ac中,然后从AcAobj文件生成一个目标文件A.obj,其中将包含Ac中定义的变量和函数的符号。另一方面,编译器将对Bc进行分开处理,而无需检查Ac,或者其他源文件,内容。 It will start by including header file(s) into Bc then it generates B.obj which also includes symbols of the variables and functions defined in Bc 首先将头文件包含到Bc中,然后生成B.obj,其中还包含Bc中定义的变量和函数的符号

As a result, you will not get errors at compile time as the function duplication is not detected by the compiler. 结果,由于编译器未检测到函数重复,因此在编译时不会出现错误。 It is the linker job to check the symbols consistency and that there are no duplication present. 链接器工作是检查符号一致性以及是否存在重复项。 Linker will get all generated object files in order to generate an executable. 链接器将获取所有生成的目标文件,以生成可执行文件。 Linker must assign a unique memory address to each symbol. 链接器必须为每个符号分配一个唯一的内存地址。 For example, in your code if there is a point (let's say in main function) where a function of Ac is called, actually, this is translated into a jump to an address in memory where that function is located. 例如,在您的代码中,如果确实有一个点(比如说在主要函数中)调用了Ac的函数,那么实际上这会转换为对该函数所在的内存地址的跳转。 Now, imagine if two functions with the same signature coexist in the executable and each symbol has a different address. 现在,假设在可执行文件中是否存在两个具有相同签名的函数,并且每个符号具有不同的地址。 Then, how can the processor figure out which function exactly do you intend to call in your program. 然后,处理器如何确定您打算在程序中确切调用哪个函数。 For that reason, if linker finds a symbol which is duplicated it will signal an error. 因此,如果链接器发现重复的符号,则将发出错误信号。

This situation is undefined behaviour with no diagnostic required. 这种情况是不确定的行为,不需要诊断。

Consult your linker's documentation to see if it has any options to report multiple definition of functions. 请查阅链接器的文档,以查看它是否具有报告多个功能定义的选项。

As @Matt-McNabb says: consult your linker documentation. 正如@ Matt-McNabb所说:请查阅链接器文档。

The only other cause I can come up with is that the linker binary compares the two functions, finds they are idenical, and ignores one. 我唯一能想到的其他原因是链接程序二进制文件比较了两个函数,发现它们是同义的,而忽略了一个。 You can check this by slightly changing the code, for example by 'return y+x'. 您可以通过稍微更改代码来进行检查,例如通过“返回y + x”。

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

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