Does this make sense? Calling C++ inside extern "C" block

Discussion in 'C++' started by terrencel@gmail.com, Sep 29, 2004.

  1. Guest

    I was told to look at some old C code that was ported to C++.

    One of the file is like:

    =========================================

    CPPClass* someCPPVar = NULL;

    extern "C"
    {

    void init()
    {
    someCPPVar = new CPPClass();
    }

    void somefunction()
    {
    bool (*callback)(...);
    callback = NULL;

    someCPPVar->someMethod((void (**)(...))&callback);

    }
    }

    =========================================

    My questions:
    1. Can I remove the 'extern "C"' block as I have the source code?
    I can just use a C++ compiler to compile it. Right?

    2. Currently, I got compile error as follows: (Sun Forte Developer 7 C++ 5.4)

    Error: Formal argument 1 of type void(*)(...)* in call to
    CPPClass::someMethod(void(*)(...)*) is being passed extern
    "C" void(*)(...)*.

    Does anyone know how to resolve this problem?

    Thanks in advance
    , Sep 29, 2004
    #1
    1. Advertising

  2. wrote:

    > I was told to look at some old C code that was ported to C++.
    >
    > One of the file is like:
    >
    > =========================================
    >
    > CPPClass* someCPPVar = NULL;
    >
    > extern "C"
    > {
    >
    > void init()
    > {
    > someCPPVar = new CPPClass();
    > }
    >
    > void somefunction()
    > {
    > bool (*callback)(...);
    > callback = NULL;
    >
    > someCPPVar->someMethod((void (**)(...))&callback);
    >
    > }
    > }
    >


    this is typical 'thunk' code so that a C language module (or sometimes assembly
    language) can indirectly
    invoke a C++ class member function. It is sometimes used in embedded systems to
    attach 'interrupts' to C++ member functions, but is also used to form a bridge
    between C language modules and C++ language modules.

    Mote that the 'extern "C"' does NOT say you can't use C++ code, what it says is
    that the name mangling for the functions in its scope is going to conform to C
    language usage rather than C++ language usage. What that implies indirectly is
    that you can't have overloaded functions using the extern "C" linkage but that if
    you have C language modules they can find the functions.

    If you remove the extern "C" you may run into link errors down the road... but
    that really depends on whether this code is still active or whether the original
    reason it was written is obsoleted by further enhancements to the code (e.g. did
    they go even further in turning this into native C++ code).

    As far as the other issue you are seeing, it looks like the 'callback' variable
    is being declared with extern "C" linkage but that since the class declaration
    was done with C++ linkage there is a mismatch in the
    linkage types. I'm not sure how to fix it... I'm not sure if I've seen a
    compiler care as long as you don't
    blatantly start assigning non-static class member functions to such variables...
    I would say use 'cdecl' in the member function argument declaration but I'm not
    100% sure that will fix it... or even if that is something that is part of the
    standard and will be in your compiler. Another possibility is to move the
    'callback' variable outside the 'extern "C"' block (make it global) but it
    depends on exactly how much of this patch code there is to change... and *that*
    may be an issue if the callbacks the code is trying to pass really do have C
    language linkage... another thing that *may* work is to remove the global nature
    of the extern "C" block and just qualify the individual functions as extern
    "C"... one other idea is to put another level of thunking in, have the extern
    "C" functions directly call C++ functions which declare the variables and do the
    thunking up to the class methods.

    Maybe someone else here will have other ideas or be able to tell you explicitly
    what your compiler will accept.

    David







    Error: Formal argument 1 of type void(*)(...)* in call to

    > CPPClass::someMethod(void(*)(...)*) is being passed extern
    > "C" void(*)(...)*.
    >
    > Does anyone know how to resolve this problem?
    >
    > Thanks in advance
    David Lindauer, Sep 30, 2004
    #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. Phil Sandler

    NULLs from SQL--does this make sense?

    Phil Sandler, Aug 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    333
    Phil Sandler
    Aug 19, 2004
  2. Jason
    Replies:
    0
    Views:
    348
    Jason
    Oct 12, 2004
  3. Timo Nentwig
    Replies:
    31
    Views:
    1,206
    Chris Smith
    May 13, 2004
  4. milkyway

    Does this make sense?

    milkyway, Dec 2, 2004, in forum: Java
    Replies:
    0
    Views:
    376
    milkyway
    Dec 2, 2004
  5. G Patel

    "extern" inside a block

    G Patel, Feb 23, 2005, in forum: C Programming
    Replies:
    12
    Views:
    584
    CBFalconer
    Feb 24, 2005
Loading...

Share This Page