Re: syntax question

Discussion in 'C Programming' started by Nobody, Jun 7, 2013.

  1. Nobody

    Nobody Guest

    On Fri, 07 Jun 2013 14:37:31 +0100, lipska the kat wrote:

    > I'm trying to get my head around Linux shared libraries


    That's off-topic for this group.

    > extern "C" void help();


    This is C++, not C, so also off-topic.

    > I've never seen this syntax before
    > what is the meaning of "C"?


    The C++ syntax 'extern "C" ...' indicates that a function has C linkage,
    i.e. it must use the platform's C calling convention, have an unmangled
    name (C++ can have multiple functions with the same name but different
    argument types, so the exported symbol must contain type information as
    well as the function's name), cannot throw exceptions, cannot have
    parameters or a return value with C++-specific types (references,
    pointer-to-member), and so on.

    You can see the effect of name mangling by running "nm -D ..." on your
    shared library with and without the 'extern "C" ...'. Given a mangled
    name, you can use the c++filt program (installed along with g++) to
    generate the corresponding C++ function prototype (minus the return type,
    which doesn't play a part in overloading and so isn't part of the name).

    The point of defining a C++ function with C linkage is so that it can be
    called from C.

    The point of declaring a function as having C linkage is so that the C++
    compiler knows that it has C linkage (by default, functions will be
    assumed to have C++ linkage). If you look at the headers for a typical
    C-based library, they will normally look something like:

    #ifdef __cplusplus
    extern "C" {
    #endif

    /* declarations here */

    #ifdef __cplusplus
    }
    #endif

    This allows the library to be used in either C or C++ code. Without it,
    a C++ compiler would assume that the declared functions have C++ linkage,
    resulting in linking errors.
    Nobody, Jun 7, 2013
    #1
    1. Advertising

  2. Nobody

    James Kuyper Guest

    On 06/07/2013 11:40 AM, lipska the kat wrote:
    > On 07/06/13 15:43, Nobody wrote:
    >> On Fri, 07 Jun 2013 14:37:31 +0100, lipska the kat wrote:

    ....
    >>> extern "C" void help();

    >>
    >> This is C++, not C, so also off-topic.
    >>
    >>> I've never seen this syntax before
    >>> what is the meaning of "C"?

    >>
    >> The C++ syntax 'extern "C" ...' indicates that a function has C linkage,

    >
    > ... well no so off topic then.


    Not really, the concept of "C linkage" is strictly a C++ concept; it
    doesn't exist in C. The C standard doesn't address linkage to other
    languages, and therefore doesn't need the concept. The C++ standard does
    have the concept of language linkage, and mandates support for C
    linkage, but any question involving how that works is best addressed in
    a C++ forum.
    Note: there need not be any C compiler that a given C++ compiler is
    capable of linking with - but if you write C++ code that defines
    identifiers or functions as having C linkage, the C++ compiler is
    required to support that, even if those functions are actually compiled
    using C++.
    James Kuyper, Jun 7, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabriele renzi
    Replies:
    2
    Views:
    193
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    193
  3. Good Night Moon
    Replies:
    9
    Views:
    266
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    304
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    303
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page