Newbie quesiton: Mix C and C++

Discussion in 'C Programming' started by philbo30, Sep 16, 2007.

  1. philbo30

    philbo30 Guest

    I have an app that is written in C. Unfortunately there are things
    that C cannot do so I need to "call" some C++ functions in order to,
    for example, generate charts and graphs as .png files. Since these
    are two different languages, is this even realistic? Where's the best
    reference on mixing C, C++ ? In advance, thank you for any info
    provided.
    philbo30, Sep 16, 2007
    #1
    1. Advertising

  2. philbo30

    Guest

    On 16 Sep, 20:05, philbo30 <> wrote:
    > I have an app that is written in C. Unfortunately there are things
    > that C cannot do so I need to "call" some C++ functions in order to,
    > for example, generate charts and graphs as .png files. Since these
    > are two different languages, is this even realistic? Where's the best
    > reference on mixing C, C++ ? In advance, thank you for any info
    > provided.


    Mixing the two languages is possible. Alternatively, writing the whole
    thing in C++ may be a better option - most C code does not take too
    much re-writing to make it valid C++.

    If you read the C++ FAQ at http://www.parashift.com/c -faq-lite/
    there is a whole chapter about mixing the two languages.

    If you do hit problems, you may be better off asking questions in
    comp.lang.c++ .

    Hope this helps.
    Paul.
    , Sep 16, 2007
    #2
    1. Advertising

  3. philbo30

    philbo30 Guest

    On Sep 16, 3:57 pm, wrote:
    > On 16 Sep, 20:05, philbo30 <> wrote:
    >
    > > I have an app that is written in C. Unfortunately there are things
    > > that C cannot do so I need to "call" some C++ functions in order to,
    > > for example, generate charts and graphs as .png files. Since these
    > > are two different languages, is this even realistic? Where's the best
    > > reference on mixing C, C++ ? In advance, thank you for any info
    > > provided.

    >
    > Mixing the two languages is possible. Alternatively, writing the whole
    > thing in C++ may be a better option - most C code does not take too
    > much re-writing to make it valid C++.
    >
    > If you read the C++ FAQ athttp://www.parashift.com/c++-faq-lite/
    > there is a whole chapter about mixing the two languages.
    >
    > If you do hit problems, you may be better off asking questions in
    > comp.lang.c++ .
    >
    > Hope this helps.
    > Paul.


    Thanx for the info; I was hoping it was as simple as something like:

    extern
    call cppfunction()
    return to C

    Unfortunately it doesn't seem that simple :-(
    philbo30, Sep 16, 2007
    #3
  4. philbo30

    Guest

    On 16 Sep, 21:13, philbo30 <> wrote:
    > On Sep 16, 3:57 pm, wrote:
    > > On 16 Sep, 20:05, philbo30 <> wrote:

    >
    > > > I have an app that is written in C. Unfortunately there are things
    > > > that C cannot do so I need to "call" some C++ functions in order to,
    > > > for example, generate charts and graphs as .png files. Since these
    > > > are two different languages, is this even realistic? Where's the best
    > > > reference on mixing C, C++ ? In advance, thank you for any info
    > > > provided.

    >
    > > Mixing the two languages is possible. Alternatively, writing the whole
    > > thing in C++ may be a better option - most C code does not take too
    > > much re-writing to make it valid C++.

    >
    > > If you read the C++ FAQ athttp://www.parashift.com/c++-faq-lite/
    > > there is a whole chapter about mixing the two languages.

    >
    > > If you do hit problems, you may be better off asking questions in
    > > comp.lang.c++ .

    >
    > > Hope this helps.
    > > Paul.

    >
    > Thanx for the info; I was hoping it was as simple as something like:
    >
    > extern
    > call cppfunction()
    > return to C
    >
    > Unfortunately it doesn't seem that simple :-(


    It can be fairly simple - decide which C++ functions are going to be
    called from C functions, and put extern "C" in front of their
    prototypes. If I remember correctly.

    Paul.
    , Sep 16, 2007
    #4
  5. philbo30

    Ben Pfaff Guest

    philbo30 <> writes:

    > I have an app that is written in C. Unfortunately there are things
    > that C cannot do so I need to "call" some C++ functions in order to,
    > for example, generate charts and graphs as .png files.


    C code is perfectly able to generate charts and graphs as .png
    files. I don't know where you get the idea that it cannot.
    --
    Go not to Usenet for counsel, for they will say both no and yes.
    Ben Pfaff, Sep 16, 2007
    #5
  6. philbo30

    Ian Collins Guest

    Ben Pfaff wrote:
    > philbo30 <> writes:
    >
    >> I have an app that is written in C. Unfortunately there are things
    >> that C cannot do so I need to "call" some C++ functions in order to,
    >> for example, generate charts and graphs as .png files.

    >
    > C code is perfectly able to generate charts and graphs as .png
    > files. I don't know where you get the idea that it cannot.


    <OT>libgd</OT>

    --
    Ian Collins.
    Ian Collins, Sep 16, 2007
    #6
  7. philbo30

    Guest

    On 2007-09-16, Ben Pfaff <> wrote:
    > philbo30 <> writes:
    >
    >> I have an app that is written in C. Unfortunately there are things
    >> that C cannot do so I need to "call" some C++ functions in order to,
    >> for example, generate charts and graphs as .png files.

    >
    > C code is perfectly able to generate charts and graphs as .png
    > files. I don't know where you get the idea that it cannot.


    He probably already have library for doing that and it's coded in C++.
    , Sep 16, 2007
    #7
  8. philbo30 <> writes:
    > I have an app that is written in C. Unfortunately there are things
    > that C cannot do so I need to "call" some C++ functions in order to,
    > for example, generate charts and graphs as .png files. Since these
    > are two different languages, is this even realistic? Where's the best
    > reference on mixing C, C++ ? In advance, thank you for any info
    > provided.


    C++ provides facilities for calling C from C++ and/or vice versa; C
    does not.

    The comp.lang.c++ FAQ probably has the information you need. If not,
    try the newsgroup itself.

    Hint: extern "C".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 16, 2007
    #8
  9. philbo30 <> wrote:
    > I have an app that is written in C. Unfortunately there are things
    > that C cannot do so I need to "call" some C++ functions in order to,
    > for example, generate charts and graphs as .png files. Since these
    > are two different languages, is this even realistic? Where's the best
    > reference on mixing C, C++ ? In advance, thank you for any info
    > provided.


    Off the top of my head,
    1) Create an interface to the C++ library, written in C++
    but declared with extern "C" bindings, so it can be
    called from C. This will require some creative work
    if the C++ is heavily object-oriented.
    2) You may have to write main() in C++; if so, change
    the original C main() to something like C_main() and
    call it from the C++ main().
    3) You may have to link with the C++ compiler.
    4) If your C compiler supports C++ exceptions, enable it.
    Otherwise, you have to catch all exceptions in your C++
    interface layer.


    --
    pa at panix dot com
    Pierre Asselin, Sep 17, 2007
    #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. Yamin

    deep copy quesiton

    Yamin, Nov 30, 2004, in forum: Java
    Replies:
    9
    Views:
    598
    charely
    Dec 4, 2004
  2. Hunter
    Replies:
    16
    Views:
    818
    Michael
    Jan 10, 2004
  3. JustSomeGuy

    Newbie quesiton.

    JustSomeGuy, Dec 5, 2004, in forum: XML
    Replies:
    3
    Views:
    398
    Bjoern Hoehrmann
    Dec 5, 2004
  4. Tobias
    Replies:
    1
    Views:
    146
    Tobias
    Jan 21, 2007
  5. DIAMOND Mark R.
    Replies:
    5
    Views:
    104
    DIAMOND Mark R.
    Aug 10, 2004
Loading...

Share This Page