how to call C++ from C?

From: Digital Puer (digital_puer_at_hotjail.comet)
Date: 01/10/04


Date: Fri, 09 Jan 2004 16:28:17 -0800

I have some C++ code that I would like to call from within a C program
(main() is within the C code). How do I go about doing this portably?
I know that I can find the mangled name of the C++ functions, but
this is kind of weird.

I am using gcc and g++ on Linux, if that matters.

Here is an example of what I'm doing:

---------------------------------------------------
a.c:
---------------------------------------------------
#include "b.h"

int main()
{
   foo();
}

---------------------------------------------------
b.h
---------------------------------------------------
int foo();

---------------------------------------------------
b.cpp
---------------------------------------------------
#include <map>
int foo()
{
   map<int, int> m;

}

---------------------------------------------------
Makefile
---------------------------------------------------
a.out: a.o b.o
         g++ a.o b.o

a.o: a.c
         gcc -c a.c

b.o: b.cpp
         g++ -c b.cpp



Relevant Pages

  • Re: [C] strcat() question (ongoing)
    ... > At the machine level, yes, basically (depending on your definition ... We can only "PUT" data in / or assign data to variables such as an int or ... foo is an object? ... > house and look on the microwave. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: call of variadic function
    ... arguments that should be passed to this function are of type int. ... You call foo with more arguments than are ... which is the standard way to access arguments of a variadic function ... Here the else clause of my sentence specifies one of the numerous non- ...
    (comp.lang.c)
  • Re: RISC OS modules with stock gcc?
    ... int bar; ... DCD &ff000004 ... IMPORT bar ... EXPORT foo ...
    (comp.sys.acorn.programmer)
  • Re: call of variadic function
    ... arguments that should be passed to this function are of type int. ... call and the definition of function foo itself does NOT produce any ... which is the standard way to access arguments of a variadic function ... Here the else clause of my sentence specifies one of the numerous non- ...
    (comp.lang.c)
  • Re: Dive Into Java?
    ... So it acts as a casting operator. ... class Foo { ... int _arg; ... Foo(int arg) { ...
    (comp.lang.python)