9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: sqweek <sqweek@gmail.com>
To: "Fans of the OS Plan 9 from Bell Labs" <9fans@9fans.net>
Subject: Re: [9fans] 9P in C++
Date: Thu, 18 Dec 2008 16:16:06 +0900	[thread overview]
Message-ID: <140e7ec30812172316n9c7eb4coea4cfaa5e409c43a@mail.gmail.com> (raw)
In-Reply-To: <494979C5.9040604@kix.es>

On Thu, Dec 18, 2008 at 7:14 AM, Rodolfo kix Garcia <kix@kix.es> wrote:
> Pietro Gagliardi escribió:
>> extern "C"{
>> #include <9p.h> // or whatever you do
>> }
>>
>> you can link 9p into a C++ program easily.
>
> If I use the extern "C" it compiles :'-)
>
> Using "extern function" is not valid for linking C and C++


 extern works fine between C and C++, but maybe it doesn't do what you
think it does. extern assures the compiler that there is a symbol with
a certain type defined in some other object than the one currently
being compiled.
 What you're running into is a name mangling issue. C++ supports
overloaded functions, ie. functions with the same name but different
prototypes. This doesn't fly with the linker, because as far as it is
concerned each symbol has a unique type[1]. So, C++ mangles[2] the
function name by appending a string representation of the prototype.
int main(int, char**) maps to a symbol like main_i_i_ppc. The mangled
notation is compiler specific of course.
 This presents a problem for interoperation with C code. Any existing
library compiled by a C compiler has regular non-mangled names. But, a
C++ compiler will grab the prototypes from the library's header file,
and then tell the linker to look for mangled versions of the symbols.
Obviously it is not going to find them.
 So, as a workaround, C++ overloads the extern keyword to allow the form:
extern "C" {
...
}
 Which tells the compiler "the functions defined in this block were
compiled with a C compiler, don't mangle the symbols".

[1] Does the linker actually know about types or just sizes?
[2] "mangles" is official terminology, so at least they're honest about it. ;)

-sqweek

      reply	other threads:[~2008-12-18  7:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-15 15:45 Rodolfo kix García 
2008-12-15 16:56 ` sqweek
2008-12-15 16:59 ` Iruata Souza
2008-12-15 21:13 ` Pietro Gagliardi
2008-12-15 23:25   ` Rodolfo kix Garcia
2008-12-15 23:34     ` Pietro Gagliardi
2008-12-16  2:16       ` sqweek
2008-12-16  2:31         ` Pietro Gagliardi
2008-12-13 21:18           ` kix
2008-12-17 22:14   ` Rodolfo kix Garcia
2008-12-18  7:16     ` sqweek [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=140e7ec30812172316n9c7eb4coea4cfaa5e409c43a@mail.gmail.com \
    --to=sqweek@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).