Friday, 30 August 2013

What is the difference between the global variables in C and C++?

What is the difference between the global variables in C and C++?

I have tested the following code:
in file "a.c/a.cpp"
int a;
in file "b.c/b.cpp"
int a;
int main() { return 0; }
When I compile the source files with "gcc *.c -o test", it succeeds. But
when I compile the source files with "g++ *.c -o test", it fails:
b.cpp:(.bss+0x0): multiple definition of 'a'
a.cpp:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
collect2.exe: error: ld returned 1 exit status
I'm really confused about this. Is there any difference between the global
variables in C and C++?

No comments:

Post a Comment