Linking a C program to a C++ library which uses STL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe.pHsiao@gmail.com

    Linking a C program to a C++ library which uses STL

    Hi,

    I tried to link a C program to a library which is written by me in C+
    +.
    I read some posts about linking a C program to C++ libraries. It seems
    doable by adding extern "C" to the C++ head file and to the function
    body modifier. However, in my test, it still doesn't work. The linking
    error messages are like undefined reference for new operator, and
    undefined reference from dequeue.tcc. ( sorry I don't know how to copy
    lines from terminals under solaris.)

    I used STL vector and queue in the library, and I guess they uses new
    operator to put items inside them.
    The C program fails to link if gcc is used.
    If I use g++ to build the file, there's no problem.

    So I thought the code in the C++ library should also be recognized in
    C. Keywords like "new," " delete," and "vector" are not allowed.
    But then I thought if the code is recognized in C is the first place,
    what is extern "C" for?

    Can somebody explain this to me? Thanks.

  • Keith Thompson

    #2
    Re: Linking a C program to a C++ library which uses STL

    Joe.pHsiao@gmai l.com writes:
    I tried to link a C program to a library which is written by me in C++.
    I read some posts about linking a C program to C++ libraries.
    [...]

    The C++ standard defines mechanisms for calling C from C++,
    and for calling C++ from C. The C standard does not define any
    such mechanisms. Therefore, your question is more appropriate to
    comp.lang.c++ than to comp.lang.c.

    You'll find, not surprisingly, that you're not the first person to
    have asked this kind of question. So your first stop, before posting
    to comp.lang.c++, should be the C++ FAQ,
    <http://www.parashift.c om/c++-faq-lite/>, particularly section 32.

    --
    Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • Default User

      #3
      Re: Linking a C program to a C++ library which uses STL

      Joe.pHsiao@gmai l.com wrote:
      Hi,
      >
      I tried to link a C program to a library which is written by me in C+
      +.
      I read some posts about linking a C program to C++ libraries. It seems
      doable by adding extern "C" to the C++ head file and to the function
      body modifier. However, in my test, it still doesn't work. The linking
      error messages are like undefined reference for new operator, and
      undefined reference from dequeue.tcc. ( sorry I don't know how to copy
      lines from terminals under solaris.)
      extern "C" doesn't magically allow you to compile C++ code as C. You
      need to have the library compiled with a C++ compiler, with a C-style
      API, and link with the C code.

      The newsgroup comp.lang.c++ is the correct one for this question. I
      have crossposted this reply and set follow-ups.




      Brian

      Comment

      • Anand Hariharan

        #4
        Re: Linking a C program to a C++ library which uses STL

        On Tue, 12 Feb 2008 15:03:27 -0800, Joe.pHsiao wrote:
        ( sorry I don't know how to copy
        lines from terminals under solaris.)
        >
        <ot>
        Just select the text you want copied. Where you want it pasted, click the
        middle mouse button. No keyboard interaction is necessary for
        copy-pasting.
        </ot>

        - Anand

        --
        ROT-13 email address to reply.

        Comment

        • Ian Collins

          #5
          Re: Linking a C program to a C++ library which uses STL

          Joe.pHsiao@gmai l.com wrote:
          Hi,
          >
          I tried to link a C program to a library which is written by me in C+
          +.
          I read some posts about linking a C program to C++ libraries. It seems
          doable by adding extern "C" to the C++ head file and to the function
          body modifier. However, in my test, it still doesn't work. The linking
          error messages are like undefined reference for new operator, and
          undefined reference from dequeue.tcc. ( sorry I don't know how to copy
          lines from terminals under solaris.)
          >
          The short answer is you have to link with the C++ compiler, otherwise
          you won't get the required C++ libraries.

          --
          Ian Collins.

          Comment

          • Malcolm McLean

            #6
            Re: Linking a C program to a C++ library which uses STL


            <Joe.pHsiao@gma il.comwrote in message
            Hi,
            >
            I tried to link a C program to a library which is written by me in C+
            +.
            I read some posts about linking a C program to C++ libraries. It seems
            doable by adding extern "C" to the C++ head file and to the function
            body modifier. However, in my test, it still doesn't work. The linking
            error messages are like undefined reference for new operator, and
            undefined reference from dequeue.tcc. ( sorry I don't know how to copy
            lines from terminals under solaris.)
            >
            I used STL vector and queue in the library, and I guess they uses new
            operator to put items inside them.
            The C program fails to link if gcc is used.
            If I use g++ to build the file, there's no problem.
            >
            So I thought the code in the C++ library should also be recognized in
            C. Keywords like "new," " delete," and "vector" are not allowed.
            But then I thought if the code is recognized in C is the first place,
            what is extern "C" for?
            >
            Can somebody explain this to me? Thanks.
            >
            extern "C" allows you to define a C-style wrapper for your functions that is
            callable from both C and C++.
            What you cannot do is include C++ only syntax like templates in the
            C-callable prototypes. C simply won't recognise them.

            --
            Free games and programming goodies.


            Comment

            • Ian Collins

              #7
              Re: Linking a C program to a C++ library which uses STL

              Malcolm McLean wrote:
              extern "C" allows you to define a C-style wrapper for your functions
              that is callable from both C and C++.
              What you cannot do is include C++ only syntax like templates in the
              C-callable prototypes. C simply won't recognise them.
              >
              And C++ won't compile them!

              --
              Ian Collins.

              Comment

              Working...