Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;“价值的多重定义”;在编译带有未初始化全局变量的C程序时++;但不是gcc_C++_C_Linker_Compiler Errors_Global Variables - Fatal编程技术网

C++ &引用;“价值的多重定义”;在编译带有未初始化全局变量的C程序时++;但不是gcc

C++ &引用;“价值的多重定义”;在编译带有未初始化全局变量的C程序时++;但不是gcc,c++,c,linker,compiler-errors,global-variables,C++,C,Linker,Compiler Errors,Global Variables,我试图理解头文件中extern和全局变量声明的用法,所以我提出了以下用C编写的测试程序 主.c文件 //main.c #include "global.h" #include <stdio.h> int nExternValue = 6; int main(int argc, char* argv[]) { printf("%d \n", nValue); printf("%d \n", nExternValue); AddToValue();

我试图理解头文件中extern和全局变量声明的用法,所以我提出了以下用C编写的测试程序

主.c文件

//main.c
#include "global.h"
#include <stdio.h>

int nExternValue = 6;

int main(int argc, char* argv[])
{
    printf("%d \n", nValue);
    printf("%d \n", nExternValue);

    AddToValue();

    printf("%d \n", nValue);
    printf("%d \n", nExternValue);
}
以及一个实现AddToValue函数的AddValue.c文件

#include "global.h"

int AddToValue() {
    nValue++;
    nExternValue++;
}
我使用gcc编译了应用程序,并运行了它:

$ gcc main.c AddValue.c -o test
$./test
0 
6 
1 
7 
我使用g++编写了应用程序,并出现以下链接器错误:

$ g++ main.c AddValue.c -o test
/tmp/ccFyGDYM.o:(.bss+0x0): multiple definition of `nValue'
/tmp/cc3ixXdu.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
为什么gcc链接器不产生错误?我认为nValue变量会被声明多次,这会产生一个错误

$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

< P> C和C++是不同的语言。如前所述,上述程序是一个有效的C程序,但在不完善的C++程序中。您违反了C++的一个定义规则。C中没有相应的规则


在使用gcc编译时,您将上述文本编译为C程序。编译时,将以上文本编译为C++程序。

< P>编译时,将未初始化的全局变量(如NValk)作为一个公共符号。不同编译单元中出现的相同公共符号将在链接期间合并。如果用G++编译(这意味着源程序将被视为C++程序),则未初始化的全局变量隐式初始化为默认值0。由于全局.h包含在多个源文件中,编译器将考虑多次定义的符号n值。

请也看看这个帖子:

当您在serveral*.c文件中包含一个*.h文件时,您将多次声明同一变量。为了避免这种情况,最好不要在*.h文件中声明全局变量,而是在*.c文件中声明。@Kira我认为这里的问题是为什么gcc链接器不将多重声明作为错误生成?@shinkou确实,但这就是为什么我做了一个评论,而不是一个答案,以防他在等待真实答案时需要解决问题。谢谢Kira,但我认为你不应该在.c文件中声明全局变量。如果声明的变量在.c文件中,其他文件如何知道它,除非您建议#包含.c文件?@ArmenB。--查阅“暂定声明”。这应该可以回答你的问题。AnT说它也是无效的C,尽管附录J:C中提到的一个通用扩展有一个定义规则,并且该程序也不是有效的C程序。(对于
nValue
,有两种定义)。
$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.