-
July 17th, 2008, 01:59 AM
#1
Use of extern "C" with global variables
Hi,
Over the years had thought that extern "C" was used to prevent C++ compilers from mangling function names - to allow calls from C code to C++ code. Name mangling was introduced in C++ compilers for the purpose of support for function over-riding.
However, I have come across code where extern "C" has been used to declare global variables in a file. I thought global variables had nothing to do with name mangling - besides I am used to seeing only the extern keyword to declare global variables.
Can you please let me understand the reason why extern "C" would need to be used with global data variables too ?
-
July 17th, 2008, 02:35 AM
#2
Re: Use of extern "C" with global variables
Variables, global or not, can be mangled too... think of those belonging to a namespace! When you use extern keyword, the variable is being shared across translation units. When you use __dllspec(dllexport), it is being exported by the module. When mixed C / C++ implementations, extern "C" would be required.
Best regards,
-
July 17th, 2008, 02:45 AM
#3
Re: Use of extern "C" with global variables
Thank you Bornish. The concept of namespaces also introduces name mangling - missed that point.
-
July 17th, 2008, 02:57 AM
#4
Re: Use of extern "C" with global variables
-
July 17th, 2008, 09:35 AM
#5
Re: Use of extern "C" with global variables
It is to signal the compiler that the symbol (the variable) has external linkage, that it may be being used in other compilation units.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|