combining c and c++ source trees

Discussion in 'C++' started by Vikram, Jun 27, 2003.

  1. Vikram

    Vikram Guest

    * i have a file foo.cpp ; it has a single function called
    "call_me(char*)". the function does some stuff and uses some other large
    libraries written in C++.
    * i also have bar.c ; i need to call this single function [call_me] from
    the bar.c code. bar.c is a part of a large C code base.

    how do i do this ? as far as i understand i did the following:

    1) extern "C" {void call_me(char*);} for the header foo.h and then
    included foo.h in bar.c.
    2) then i compiled foo.cpp into foo.o using g++ - this works
    3) then i compiled bar.c and foo.o using gcc but i get a ton of errors at
    this stage. all relating to c++ stl stuff. what is wrong here ?

    and in general what is the correct/easy way of mixing C and C++ code -
    ideally i would like to call functions from both sides into the other.

    hope i was clear. thanks
    --
    vv
    Vikram, Jun 27, 2003
    #1
    1. Advertising

  2. Vikram

    Rolf Magnus Guest

    Vikram wrote:

    > * i have a file foo.cpp ; it has a single function called
    > "call_me(char*)". the function does some stuff and uses some other
    > large libraries written in C++.
    > * i also have bar.c ; i need to call this single function [call_me]
    > from the bar.c code. bar.c is a part of a large C code base.
    >
    > how do i do this ? as far as i understand i did the following:
    >
    > 1) extern "C" {void call_me(char*);} for the header foo.h and then
    > included foo.h in bar.c.


    Yes, though you can leave out the curly braces:

    extern "C" void call_me(char*);

    They are only needed if you want more than one extern declaration and
    don't want to repeat the extern keyword all the time.

    > 2) then i compiled foo.cpp into foo.o using g++ - this works
    > 3) then i compiled bar.c and foo.o using gcc but i get a ton of errors
    > at this stage. all relating to c++ stl stuff. what is wrong here ?


    How would we know? "i compiled... but i get a ton of errors" doesn't
    really qualify as a useful error description. Anyway, just a wild
    guess: you said you "compiled" bar.c and foo.o using gcc. That looks to
    me as if you compiled bar.c _and_ in the same stage linked the
    resulting object together with foo.o into an executable using gcc. But
    if you have c++ sources, you must link with g++. If not, the C++
    standard library will not be linked in. So do something like:

    g++ -c foo.cpp -o foo.o
    gcc -c bar.c -o bar.o
    g++ bar.o foo.o -o foobar

    > and in general what is the correct/easy way of mixing C and C++ code -
    > ideally i would like to call functions from both sides into the other.


    You just have to make a C interface. In the header, write something
    like:

    #ifdef __cplusplus
    extern "C" {
    #endif

    //your declarations

    #ifdef __cplusplus
    }
    #endif

    Then you can #include it from C sources as well as from C++ sources.
    And when using gcc/g++, you _must_ link with g++.
    Rolf Magnus, Jun 27, 2003
    #2
    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. Jean-Marie Condom

    JAXB : comparing and updating trees

    Jean-Marie Condom, Dec 28, 2004, in forum: Java
    Replies:
    0
    Views:
    366
    Jean-Marie Condom
    Dec 28, 2004
  2. Peter Saffrey

    Xerces C++ and DOM trees

    Peter Saffrey, Jan 6, 2005, in forum: XML
    Replies:
    1
    Views:
    833
    Martin Honnen
    Jan 6, 2005
  3. Peter Tkacz

    STL and Balanced Trees???

    Peter Tkacz, Oct 23, 2003, in forum: C++
    Replies:
    7
    Views:
    450
    Jerry Coffin
    Oct 24, 2003
  4. Replies:
    3
    Views:
    245
    Duncan Booth
    Dec 15, 2006
  5. jacob navia

    Binary search trees (AVL trees)

    jacob navia, Jan 3, 2010, in forum: C Programming
    Replies:
    34
    Views:
    1,411
    Dann Corbit
    Jan 8, 2010
Loading...

Share This Page