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

Discussion in 'C Programming' started by Joe.pHsiao@gmail.com, Feb 12, 2008.

  1. Guest

    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.
    , Feb 12, 2008
    #1
    1. Advertising

  2. 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.com/c++-faq-lite/>, particularly section 32.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 12, 2008
    #2
    1. Advertising

  3. Default User Guest

    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
    Default User, Feb 12, 2008
    #3
  4. 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.
    Anand Hariharan, Feb 13, 2008
    #4
  5. Ian Collins Guest

    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.
    Ian Collins, Feb 13, 2008
    #5
  6. <> wrote 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.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 13, 2008
    #6
  7. Ian Collins Guest

    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.
    Ian Collins, Feb 13, 2008
    #7
    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. Replies:
    2
    Views:
    552
    klaus hoffmann
    Feb 22, 2006
  2. kris
    Replies:
    5
    Views:
    393
    Joachim Schmitz
    Jun 22, 2007
  3. grbgooglefan
    Replies:
    6
    Views:
    431
    Christian Heimes
    Dec 7, 2007
  4. Default User
    Replies:
    6
    Views:
    2,850
  5. Sammy
    Replies:
    1
    Views:
    309
    NET_NET_2003
    Aug 18, 2003
Loading...

Share This Page