Link C++ object with C object

Discussion in 'C++' started by Dominik Werder, Sep 9, 2004.

  1. Hi All,

    I've compiled a cpp file with g++ without linking it and got an object
    file.
    Then I've compiled a C file with gcc and got an object file too.
    Now if I try to link it to an executable (the main method is in the C
    object) the linker complains about not finding the symbol for a
    function declared in the C++ object.
    Somebody told me that the two compilers use different symbol names for
    the same function names.
    He said also it would exist something like a "export this function as
    C style" keyword but I didn't find anything.
    Could somebody help me with this?

    bye!
    Dominik
     
    Dominik Werder, Sep 9, 2004
    #1
    1. Advertising

  2. "Dominik Werder" <> wrote in message
    news:...
    > Hi All,
    >
    > I've compiled a cpp file with g++ without linking it and got an object
    > file.
    > Then I've compiled a C file with gcc and got an object file too.
    > Now if I try to link it to an executable (the main method is in the C
    > object) the linker complains about not finding the symbol for a
    > function declared in the C++ object.
    > Somebody told me that the two compilers use different symbol names for
    > the same function names.
    > He said also it would exist something like a "export this function as
    > C style" keyword but I didn't find anything.
    > Could somebody help me with this?
    >
    > bye!
    > Dominik


    In your C++ code

    extern "C" int my_function()
    {
    ...
    }

    john
     
    John Harrison, Sep 9, 2004
    #2
    1. Advertising

  3. Dominik Werder

    David Harmon Guest

    On 9 Sep 2004 03:05:06 -0700 in comp.lang.c++, (Dominik
    Werder) wrote,
    >Hi All,
    >
    >I've compiled a cpp file with g++ without linking it and got an object
    >file.
    >Then I've compiled a C file with gcc and got an object file too.
    >Now if I try to link it to an executable (the main method is in the C
    >object)


    No, that won't work, main must be in the C++ part.

    >the linker complains about not finding the symbol for a
    >function declared in the C++ object.
    >Somebody told me that the two compilers use different symbol names for
    >the same function names.


    You must use a compiler or pair of compilers that explicitly support C
    and C++ comparability. I guess that gcc would do that.

    >He said also it would exist something like a "export this function as
    >C style" keyword but I didn't find anything.


    Yes, of course you need that part most.

    This issue is covered in Marshall Cline's C++ FAQ. See section
    "[32] How to mix C and C++"It is always good to check the FAQ before
    posting. You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
     
    David Harmon, Sep 9, 2004
    #3
  4. Dominik Werder

    Greg Comeau Guest

    In article <>,
    Dominik Werder <> wrote:
    >I've compiled a cpp file with g++ without linking it and got an object
    >file.
    >Then I've compiled a C file with gcc and got an object file too.
    >Now if I try to link it to an executable (the main method is in the C
    >object) the linker complains about not finding the symbol for a
    >function declared in the C++ object.
    >Somebody told me that the two compilers use different symbol names for
    >the same function names.
    >He said also it would exist something like a "export this function as
    >C style" keyword but I didn't find anything.
    >Could somebody help me with this?


    Check out http://www.comeaucomputing.com/techtalk/#externc
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Sep 10, 2004
    #4
    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. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    891
    Kevin Spencer
    May 17, 2005
  2. Graham Thomson
    Replies:
    3
    Views:
    487
    Eki Y. Baskoro
    Dec 18, 2003
  3. Dan M
    Replies:
    5
    Views:
    440
  4. kolesdz
    Replies:
    9
    Views:
    547
    Blinky the Shark
    Aug 22, 2007
  5. Marina Limeira

    LINK with another link

    Marina Limeira, Feb 7, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    124
    Marina Limeira
    Feb 7, 2006
Loading...

Share This Page