On Fri, Jul 25, 2014 at 6:47 AM, Nick <nospam@xxxxxxxxxxxxxxx> wrote: > On Thu, 2014-07-24 at 09:19 +0100, Jonathan Wakely wrote: >> That is valid C++ code. C linkage doesn't mean the function has to >> actually be usable from C. >> >> You can also declare functions with C linkage that take reference >> parameters or throw exceptions. > > I didn't know that. Out of curiosity...I assumed that one of the > ramifications of declaring some code w/ C linkage was that it emitted > symbols that have C mangling. Is that true? If so, how are C++ antics > handled in this scenario? Look at this: [fedora@facs-bugtracker ~]$ cat y.cc #include <string> extern "C" { std::string GetVersion() { return "1"; } } [fedora@facs-bugtracker ~]$ objdump -t y.o y.o: file format elf64-x86-64 SYMBOL TABLE: [....] 0000000000000000 g F .text 0000000000000061 GetVersion [....] Name GetVersion is not mangled, so it's a bare name. Note, that if you want to use it elsewhere, you'd have to declare it somehow. So if GetVersion returns std::string, your header would wound up with "#include <string>" and it won't be understood by C compiler, or you could declare it as something else, but then you'd be lying to the compiler, and then you're on your own. But if it's included in C++ sources, it will be ok, since function would be declared with proper return value and it will be annotated as having C linkage, so C++ expects something that is called 'GetVersion' and returns std::string - so everything is ok. Of course it won't work if GetVersion is overloaded: ^ [fedora@facs-bugtracker ~]$ cat y.cc #include <string> extern "C" { std::string GetVersion() { return "1"; } int GetVersion(bool as_int) { return 1; } } [fedora@facs-bugtracker ~]$ g++ y.cc -c y.cc: In function ‘int GetVersion(bool)’: y.cc:5:28: error: declaration of C function ‘int GetVersion(bool)’ conflicts with int GetVersion(bool as_int) { return 1; } ^ y.cc:4:14: error: previous declaration ‘std::string GetVersion()’ here std::string GetVersion() { return "1"; } Please forgive me the most idiotic overload ever. -- Jędrzej Dudkiewicz I really hate this damn machine, I wish that they would sell it. It never does just what I want, but only what I tell it.