473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Callling C++ code from C

C++ FAQ 32.1 (http://www.parashift.com/c++-faq-lit....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
Jul 22 '05 #1
4 1086

"Alexei Betin" <ab****@mail.ru> wrote in message
news:75**************************@posting.google.c om...
C++ FAQ 32.1 (http://www.parashift.com/c++-faq-lit....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
Jul 22 '05 #2

"Alexei Betin" <ab****@mail.ru> wrote in message
news:75**************************@posting.google.c om...
C++ FAQ 32.1 (http://www.parashift.com/c++-faq-lit....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
Jul 22 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<x8***************@newsread1.news.pas.earthli nk.net>...
"Alexei Betin" <ab****@mail.ru> wrote in message
news:75**************************@posting.google.c om...
C++ FAQ 32.1 (http://www.parashift.com/c++-faq-lit....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).


Fair enough, thanks, guys!
(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++'.


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
-Mike

Jul 22 '05 #4
-----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-----
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
17
by: sagar | last post by:
Hi, I have a C file(add.c) in which i have a function called add.now i want to call the same add function from another file sub.c .Can any1 tell how to do that... Thanks in advance Mark
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.