Callling C++ code from C

A

Alexei Betin

C++ FAQ 32.1 (http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.1)
says:
<quote>
- You must use your C++ compiler when compiling main() (e.g., for
static initialization)
</quote>

And what if my C++ library does not have any static objects which
require constructors called (it might define some static C++ object
pointers but initializes them to 0)?

Is there any reason that main() has to be compiled with C++ compiler
besides static initialization? I cannot think of any...

Thanks,
~Alexei
 
J

John Harrison

Alexei Betin said:
C++ FAQ 32.1 (http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.1)
says:
<quote>
- You must use your C++ compiler when compiling main() (e.g., for
static initialization)
</quote>

And what if my C++ library does not have any static objects which
require constructors called (it might define some static C++ object
pointers but initializes them to 0)?

Is there any reason that main() has to be compiled with C++ compiler
besides static initialization? I cannot think of any...

Thanks,
~Alexei

What about the standard C++ library? That contains some static objects (e.g.
cin, cout, cerr, clog, maybe other implementation defined ones).

But I think this is just a case where you have to find what works for your
particular compiler.

john
 
M

Mike Wahler

Alexei Betin said:
C++ FAQ 32.1 (http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.1)
says:
<quote>
- You must use your C++ compiler when compiling main() (e.g., for
static initialization)
</quote>

And what if my C++ library does not have any static objects which
require constructors called

But it does (e.g. std::cin, std::cout).
(it might define some static C++ object
pointers but initializes them to 0)?

Is there any reason that main() has to be compiled with C++ compiler
besides static initialization? I cannot think of any...

Sounds like a good enough reason to me. :)

There's also the "name mangling issue".
AFAIK, C doesn't have 'extern C++'.

-Mike
 
A

Alexei Betin

Mike Wahler said:
But it does (e.g. std::cin, std::cout).

Fair enough, thanks, guys!
Sounds like a good enough reason to me. :)

There's also the "name mangling issue".
AFAIK, C doesn't have 'extern C++'.

That I am aware of, I was assuming there is a C API already compiled
with C++. Sorry, I was not clear.

Overall, I think John is right in that it might work depending on
compiler - because it does work for me with CC on Solaris. Also, looks
like on Solaris you can call dl_open()/dl_close() and those will run
static initializers for you.

I just wanted to make sure there're no standard answer or rule that I
was missing.

Thanks,
~Alexei
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Alexi,

Actually, this does work with all compilers (with some caviots) in that
you first write a C wrapper for your C++ library, wrap the C wrapper in
an Extern "C"{} statement & compile it with your C++ compiler, then
write your C code to access the C/C++ wrapper, and finally, link it all
with your C++ linker.

If you want an example project (in a TGZ package) I can ship you one off
line. Just let me know.

Evan Carew
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAKvTWoo/Prlj9GScRAkeMAJ9Athoiqri3J56kL9yO6GdaYchGvACffg65
VOl8HvNJRm7kYpBRxx74/7o=
=FHeT
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top