Re: Warning for incompatible functions declared 'extern "C"'?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2014-07-25 at 07:26 +0200, Jędrzej Dudkiewicz wrote:
> 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"; }
> 

Makes sense.  Thanks for laying it out.







[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux