Linking a C++ library to a C program.

Discussion in 'C Programming' started by a2x, Jul 8, 2005.

  1. a2x

    a2x Guest

    Hi,

    I am writing a C program which interfaces with a C++ library. The C++
    library works fine with C. I was wondering if I need to use a C++
    compiler (g++ in my case) to compile the C program or can I use a C
    compiler (gcc).

    Thanks.
    a2x, Jul 8, 2005
    #1
    1. Advertising

  2. a2x

    Jack Klein Guest

    On 8 Jul 2005 15:57:50 -0700, "a2x" <> wrote
    in comp.lang.c:

    > Hi,
    >
    > I am writing a C program which interfaces with a C++ library. The C++
    > library works fine with C. I was wondering if I need to use a C++
    > compiler (g++ in my case) to compile the C program or can I use a C
    > compiler (gcc).
    >
    > Thanks.


    C does not define linkage to any other language at all. C++, which is
    off-topic in comp.lang.c, does not define the mechanics of linkage.
    You need to ask on either one of the gnu.gcc.* support groups, or one
    specific to you platform.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jul 9, 2005
    #2
    1. Advertising

  3. a2x

    Guest

    a2x wrote:
    > I am writing a C program which interfaces with a C++ library. The C++
    > library works fine with C. I was wondering if I need to use a C++
    > compiler (g++ in my case) to compile the C program or can I use a C
    > compiler (gcc).


    Ok, first, learn what extern "C" means, and learn how to use and apply
    it to interface C and C++. Then consider that you have two options: 1)
    You can simply put extern "C" around every interface that you wish to
    share with C and 2) You can compile the C code with your C++ compiler
    and actually make everying actually C++. The first is the cleanest way
    but might require more intrusive source modifications. The second is
    easiest so long as you aren't doing weird things in C like making
    declarations like: struct foo foo;

    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
    , Jul 9, 2005
    #3
  4. a2x

    Malcolm Guest

    "a2x" <> wrote
    >
    > I am writing a C program which interfaces with a C++ library. The C++
    > library works fine with C. I was wondering if I need to use a C++
    > compiler (g++ in my case) to compile the C program or can I use a C
    > compiler (gcc).
    >

    If you want to call a function written in C++ from a C file your best bet is
    to write the C code in the common subset of the two languages and run it
    through a C++ compiler. The alternative is to mess about with C++ name
    mangling and linkage conventions.
    Malcolm, Jul 11, 2005
    #4
  5. a2x

    CBFalconer Guest

    Malcolm wrote:
    > "a2x" <> wrote
    >>
    >> I am writing a C program which interfaces with a C++ library. The
    >> C++ library works fine with C. I was wondering if I need to use a
    >> C++ compiler (g++ in my case) to compile the C program or can I
    >> use a C compiler (gcc).

    >
    > If you want to call a function written in C++ from a C file your
    > best bet is to write the C code in the common subset of the two
    > languages and run it through a C++ compiler. The alternative is to
    > mess about with C++ name mangling and linkage conventions.


    For which alternative, AFAICT, there exists nothing even remotely
    resembling a standard, so that everything is totally non-portable.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jul 11, 2005
    #5
  6. a2x

    Default User Guest

    CBFalconer wrote:
    > Malcolm wrote:
    > > "a2x" <> wrote
    > >>
    > >> I am writing a C program which interfaces with a C++ library. The
    > >> C++ library works fine with C. I was wondering if I need to use a
    > >> C++ compiler (g++ in my case) to compile the C program or can I
    > >> use a C compiler (gcc).

    > >
    > > If you want to call a function written in C++ from a C file your
    > > best bet is to write the C code in the common subset of the two
    > > languages and run it through a C++ compiler. The alternative is to
    > > mess about with C++ name mangling and linkage conventions.

    >
    > For which alternative, AFAICT, there exists nothing even remotely
    > resembling a standard, so that everything is totally non-portable.



    Correct, but there is a C++ standard way to specify C-style linkage
    (extern "C"). That prevents you from calling overloaded functions from
    the C module, of course.

    Whether a C object file can actually be linked with a C++ object file
    or library is still implementation dependent. In practice it works
    pretty well.

    All of this should be discussed on comp.lang.c++, not here.



    Brian
    Default User, Jul 12, 2005
    #6
  7. a2x

    CBFalconer Guest

    Default User wrote:
    > CBFalconer wrote:
    >> Malcolm wrote:
    >>> "a2x" <> wrote
    >>>>
    >>>> I am writing a C program which interfaces with a C++ library. The
    >>>> C++ library works fine with C. I was wondering if I need to use a
    >>>> C++ compiler (g++ in my case) to compile the C program or can I
    >>>> use a C compiler (gcc).
    >>>
    >>> If you want to call a function written in C++ from a C file your
    >>> best bet is to write the C code in the common subset of the two
    >>> languages and run it through a C++ compiler. The alternative is to
    >>> mess about with C++ name mangling and linkage conventions.

    >>
    >> For which alternative, AFAICT, there exists nothing even remotely
    >> resembling a standard, so that everything is totally non-portable.

    >
    > Correct, but there is a C++ standard way to specify C-style linkage
    > (extern "C"). That prevents you from calling overloaded functions
    > from the C module, of course.
    >
    > Whether a C object file can actually be linked with a C++ object
    > file or library is still implementation dependent. In practice it
    > works pretty well.
    >
    > All of this should be discussed on comp.lang.c++, not here.


    Not quite. I am quite willing for C++ users to call my code, so I
    habitually insert the #ifdef __cplusplus__ guards for the 'extern
    "C" {}' in the header files. That __cplusplus__ is not allowed to
    be defined in the C system, for just this purpose.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jul 12, 2005
    #7
  8. a2x

    S.Tobias Guest

    CBFalconer <> wrote:

    > Not quite. I am quite willing for C++ users to call my code, so I
    > habitually insert the #ifdef __cplusplus__ guards for the 'extern
    > "C" {}' in the header files. That __cplusplus__ is not allowed to
    > be defined in the C system, for just this purpose.


    ITYM: __cplusplus

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jul 12, 2005
    #8
  9. a2x

    CBFalconer Guest

    "S.Tobias" wrote:
    > CBFalconer <> wrote:
    >
    >> Not quite. I am quite willing for C++ users to call my code, so I
    >> habitually insert the #ifdef __cplusplus__ guards for the 'extern
    >> "C" {}' in the header files. That __cplusplus__ is not allowed to
    >> be defined in the C system, for just this purpose.

    >
    > ITYM: __cplusplus


    Yup, I always have to check that when I write a header file. At
    least I know I am usually wrong.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jul 12, 2005
    #9
    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. Roland Raschke
    Replies:
    1
    Views:
    1,086
    Kevin Goodsell
    Sep 22, 2003
  2. Albert
    Replies:
    4
    Views:
    518
    Mike Wahler
    Sep 29, 2003
  3. Replies:
    1
    Views:
    312
    Rolf Magnus
    Jul 9, 2005
  4. Replies:
    6
    Views:
    406
    Ian Collins
    Feb 13, 2008
  5. Default User
    Replies:
    6
    Views:
    2,850
Loading...

Share This Page