(OT?) addressing and calling convention

Discussion in 'C++' started by cpisztest@gmail.com, May 30, 2014.

  1. Guest

    Not sure if this is OT or not, but I am getting unresolved externals which I suspect to be from addressing and calling convention differences.

    I am not really sure how linkage works when a library is compiled with cdecl vs stdcall and is attempted to be linked into an executable that uses the other.

    Is there a tool somewhere that can be used to examine addressing and calling convention for static libs?
     
    , May 30, 2014
    #1
    1. Advertisements

  2. Ian Collins Guest

    wrote:
    > Not sure if this is OT or not, but I am getting unresolved externals
    > which I suspect to be from addressing and calling convention
    > differences.
    >
    > I am not really sure how linkage works when a library is compiled
    > with cdecl vs stdcall and is attempted to be linked into an
    > executable that uses the other.
    >
    > Is there a tool somewhere that can be used to examine addressing and
    > calling convention for static libs?


    You should ask on a windows focused group.

    The standard C++ answer is to use functions declared extern "C" for any
    code shared between C++ and C or built with different C++ compilers.

    --
    Ian Collins
     
    Ian Collins, May 30, 2014
    #2
    1. Advertisements

  3. Dombo Guest

    Op 30-May-14 1:02, schreef:
    > Not sure if this is OT or not, but I am getting unresolved externals
    > which I suspect to be from addressing and calling convention
    > differences.


    It might be just a matter of a missing extern "C" declaration around the
    C function declarations. To support function overloading most, if not
    all, C++ compilers mangle the function names. When calling a C function
    from C++ code, the C++ compiler has to know that it shouldn't mangle the
    names, otherwise the linker would look for a mangled name in the C
    library which doesn't exist, which in turn would lead to a "unresolved
    external" error.

    > I am not really sure how linkage works when a library is compiled
    > with cdecl vs stdcall and is attempted to be linked into an
    > executable that uses the other.


    I'm not entirely sure, but I don't think the calling convention affects
    linking. I.e. if the calling side is compiled with the cdecl calling
    convention whereas the library implementing the function uses the
    stdcall calling convention I would expect that the linker happily links
    those. Not that that is a good thing as it would result in obscure
    behavior when actually running the code.

    > Is there a tool somewhere that can be used to examine addressing and
    > calling convention for static libs?


    Since your are talking about stdcall and cdecl calling conventions
    (which is not topical for this newsgroup), I assume you are using the
    Microsoft development environment. In that case the dumpbin tool might
    give you a clue what is going on.
     
    Dombo, May 30, 2014
    #3
  4. Nobody Guest

    On Fri, 30 May 2014 22:30:38 +0200, Dombo wrote:

    >> I am not really sure how linkage works when a library is compiled with
    >> cdecl vs stdcall and is attempted to be linked into an executable that
    >> uses the other.

    >
    > I'm not entirely sure, but I don't think the calling convention affects
    > linking. I.e. if the calling side is compiled with the cdecl calling
    > convention whereas the library implementing the function uses the stdcall
    > calling convention I would expect that the linker happily links those. Not
    > that that is a good thing as it would result in obscure behavior when
    > actually running the code.


    It most definitely affects linking.

    stdcall functions have their symbols decorated, e.g. "func@4", where the
    number is the size of the argument list in bytes. If the call was compiled
    on the assumption that the function uses cdecl but it actually uses
    stdcall (or vice versa), linking will fail.

    Normally this should be taken care of by the headers; stdcall functions
    will have a "__stdcall" qualifier between the return type and the function
    name. However, this can be buried beneath multiple layers of macros,
    meaning that both sides need the same preprocessor definitions.

    If you try to provide your own declarations instead of using the library's
    associated headers, you're asking for trouble.
     
    Nobody, May 31, 2014
    #4
    1. Advertisements

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. Asapi
    Replies:
    1
    Views:
    545
    osmium
    Dec 13, 2003
  2. Mike
    Replies:
    3
    Views:
    2,447
    Victor Bazarov
    Oct 28, 2004
  3. Sriniv

    Calling convention --- what is it?

    Sriniv, Mar 24, 2006, in forum: C Programming
    Replies:
    1
    Views:
    475
    Vladimir S. Oka
    Mar 24, 2006
  4. priyanka
    Replies:
    6
    Views:
    529
    Stephen Sprunk
    Jul 17, 2006
  5. bg-greece
    Replies:
    1
    Views:
    638
    mlimber
    Nov 16, 2005
  6. RB Smissaert

    Bad DLL calling convention

    RB Smissaert, Apr 16, 2006, in forum: C++
    Replies:
    6
    Views:
    2,369
    RB Smissaert
    Apr 17, 2006
  7. Ivan Vecerina

    C/C++ calling convention

    Ivan Vecerina, Jun 7, 2006, in forum: C++
    Replies:
    2
    Views:
    557
    Geler
    Jun 7, 2006
  8. DrTodd13
    Replies:
    5
    Views:
    1,993
    Kaz Kylheku
    Nov 30, 2009
Loading...