Topic: calling C++ from a C filie


Author: comeau@panix.com (Greg Comeau)
Date: 2000/07/29
Raw View
Martijn Lievaart wrote:
> "Victor Bazarov" <vAbazarov@dAnai.com> wrote in message
> news:8lonso$7tq$1@bob.news.rcn.net...
>> "Martijn Lievaart" <news-from@greebo.orion.nl> wrote...
>>> No, no, it is not about the proper prototype for main, main() must be
>>> compiled as C++, not C. E.g. you need main.cpp, not main.c.

Well, .cpp and .c are just file name conventions, not languages,
but yes, the C++ Standard specically talks about main() for
C++ programs, and the C Standard specifically talks about main()
for C programs.

>>> Let me put it another way. Both C and C++ require their
>>> respective startup code to be executed before main()
>>> is called. All C++ compilers I know of
>>> will initialise the C runtime

...really as just an implementation detail though...

>>>but no C compiler will initialise the C++ runtime.

This is not quite true.  That is, although that Standards
don't address this per se, some implementation do do it.
For instance, Comeau C++ has hook in it to do this
(since Comeau C++ is also Comeau C) although it's a very
rare feature for somebody to be using.

>> So unless you don't need the runtime, or are very sure of what
>>> you are doing, make sure your main() is compiled as C++.

Generally smoother, yes.

>>> I don't think the standard actually requires this,
>>> but it is true of any system I know of.

The Standards specify their respective standards, so right,
they would not require it.

- Greg
--
Comeau Computing / Comeau C/C++ 4.2.42 (4.2.44 expected soon)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/07/29
Raw View
In article <3980D18B.7EA89C12@wizard.net>,
James Kuyper  <kuyper@wizard.net> wrote:
>Martijn Lievaart wrote:
>> ---- A.cpp
>> static class X
>> {
>> public:
>>     X() {}
>> } x;
>>
>> Does x get initialised when main is written in C?

In short, we don't know, as Standard C talk about C
and Standard C++ talks about C++.

>> I think that the standard
>> doesn't require it, and that not all implementations will.

The problem is that there is no "the standard", there is two of them.

>> I'll test later today if I can find the time what the
>> compilers I use have to say about this.

Some compilers do allow it is they are "similar enough"(my words)
but this is not always the case.

>Section 3.6.2 p1 of the C++ standard says that "The storage for objects
>with static storage duration (3.7.1) shall be zero-initialized (8.5)
>before any other initialization takes place. ... Objects of POD types
>(3.9) with static storage duration initialized with constant expressions
>(5.19) shall be initialized before any dynamic initialization takes
>place. ..."

True, but his example was that main() is written in C, and
Standard C++ does not talk about that.

>The zero-initialization of x has to occur at the very beginning.

"at the beginning" == "at init time", whenever that is.
Anyway, since main is in C, we're talking about whatver happens then.

>Section 3.6.2 p3 of the C++ standard says "It is implementation-defined
>whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an
>object of namespace scope is done before the first statement of main. If
>the initialization is deferred to some point in time after the first
>statement of main, it shall occur before the first use of any function
>or object defined in the same translation unit as the object to be
>initialized."
>
>Whatever mechanism is used to achieve that objective, I think it is
>likely to be equally useable in mixed C/C++ code as in pure C++.

Since it's beyond the scope of the standard, it's up to the
compilers, and yes, in some cases this does work.

- Greg
--
Comeau Computing / Comeau C/C++ 4.2.42 (4.2.44 expected soon)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Martijn Lievaart" <news.from.xs4all.home@greebo.orion.nl>
Date: 2000/08/01
Raw View
"James Kuyper" <kuyper@wizard.net> wrote in message
news:3980D18B.7EA89C12@wizard.net...
> Martijn Lievaart wrote:
> ....
> > ---- A.cpp
> >
> > static class X
> > {
> > public:
> >     X() {}
> > } x;
> >
> > Does x get initialised when main is written in C? I think that the
standard
> > doesn't require it, and that not all implementations will. I'll test
later
> > today if I can find the time what the compilers I use have to say about
> > this.
>
> Section 3.6.2 p1 of the C++ standard says that "The storage for objects
> with static storage duration (3.7.1) shall be zero-initialized (8.5)
> before any other initialization takes place. ... Objects of POD types
> (3.9) with static storage duration initialized with constant expressions
> (5.19) shall be initialized before any dynamic initialization takes
> place. ..."
>
> The zero-initialization of x has to occur at the very beginning.
> However, there's no constant expression provided to initialize x, so the
> second part of that quote doesn't seem to apply. Therefore, we go on to
> dynamic initialization.
>
> Section 3.6.2 p3 of the C++ standard says "It is implementation-defined
> whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an
> object of namespace scope is done before the first statement of main. If
> the initialization is deferred to some point in time after the first
> statement of main, it shall occur before the first use of any function
> or object defined in the same translation unit as the object to be
> initialized."
>
> Whatever mechanism is used to achieve that objective, I think it is
> likely to be equally useable in mixed C/C++ code as in pure C++.
>

But what /guarentees/ does the standard make that this is the case? Being
familiar with some c++ startup environments I think you are overly
optimistic here, f.i. Borland C++ (any version I know off) will not
initialise statics in this way.

M4
--
Save electrons, recycle your email!



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/08/01
Raw View
In article <8lsm7m$er6$1@news1.xs4all.nl>,
Martijn Lievaart <news.reply.xs4all.home@greebo.orion.nl> wrote:
>"James Kuyper" <kuyper@wizard.net> wrote in message
>news:3980D18B.7EA89C12@wizard.net...
>> Section 3.6.2 p1 of the C++ standard says....
>> The zero-initialization of x has to occur at the very beginning.....
>> Section 3.6.2 p3 of the C++ standard says ....
>>
>> Whatever mechanism is used to achieve that objective, I think it is
>> likely to be equally useable in mixed C/C++ code as in pure C++.
>
>But what /guarentees/ does the standard make that this is the case?

Well, James made many points.  Anyway, so long as we're talking
about mixing C and C++ there is no guarantee.

>Being
>familiar with some c++ startup environments I think you are overly
>optimistic here, f.i. Borland C++ (any version I know off) will not
>initialise statics in this way.

Note that there are a few levels of rules, and within that,
different ways to implement them.

But can you give a small example?  Are you saying that for this:

int i;
int main() {}

that i will be garbage with Borland C++?

- Greg
--
Comeau Computing / Comeau C/C++ 4.2.42 (4.2.44 expected soon)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Martijn Lievaart" <news-from@greebo.orion.nl>
Date: 2000/08/01
Raw View
"Greg Comeau" <comeau@panix.com> wrote in message
news:8m46um$90a$1@panix3.panix.com...
> In article <8lsm7m$er6$1@news1.xs4all.nl>,
> Martijn Lievaart <news.reply.xs4all.home@greebo.orion.nl> wrote:
> >"James Kuyper" <kuyper@wizard.net> wrote in message
> >news:3980D18B.7EA89C12@wizard.net...
> >> Section 3.6.2 p1 of the C++ standard says....
> >> The zero-initialization of x has to occur at the very beginning.....
> >> Section 3.6.2 p3 of the C++ standard says ....
> >>
> >> Whatever mechanism is used to achieve that objective, I think it is
> >> likely to be equally useable in mixed C/C++ code as in pure C++.
> >
> >But what /guarentees/ does the standard make that this is the case?
>
> Well, James made many points.  Anyway, so long as we're talking
> about mixing C and C++ there is no guarantee.
>
> >Being
> >familiar with some c++ startup environments I think you are overly
> >optimistic here, f.i. Borland C++ (any version I know off) will not
> >initialise statics in this way.
>
> Note that there are a few levels of rules, and within that,
> different ways to implement them.
>
> But can you give a small example?  Are you saying that for this:
>
> int i;
> int main() {}
>
> that i will be garbage with Borland C++?
>

No, sorry for the confusion. I didn't say what I wanted to say.

I wanted to say is that the Borland C++ startup code (and most compilers I
know, I just happened to have delved into some Borland startup code),
explicitely call constructors for global objects before main() is ever
executed. This is a function of their /c++/ startup code, so it wont be
called when you link with a different startup.

But I think I have to take the implications of that back, on retrospect, the
C++ startup code is probably the same as the C startup code, so there is a
fair chance that things do work out. And this probably will hold for most
compilers, where I thought it wouldn't.

But lets leave it at that. The main question I asked -- does the standard
say anything about this -- has been answered. It doesn't and it shouldn't.

One last thing though, it is something that producers of compilers that can
be used for both languages could document. That /is/ a QoI question
ofcourse.

M4
--
Contrary to popular belief, the number of the beast is not 666,
it's 555-37689.
Please post replies to this newsgroup. If you must reach
me by email, use <newsgroup-name> at greebo.orion in nl.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/08/01
Raw View
In article <8m5utr$1d2e$1@buty.wanadoo.nl>,
Martijn Lievaart <news-reply@greebo.orion.nl> wrote:
>I wanted to say is that the Borland C++ startup code (and most compilers I
>know, I just happened to have delved into some Borland startup code),
>explicitely call constructors for global objects before main() is ever
>executed. This is a function of their /c++/ startup code, so it wont be
>called when you link with a different startup.
>
>But I think I have to take the implications of that back, on retrospect, the
>C++ startup code is probably the same as the C startup code, so there is a
>fair chance that things do work out. And this probably will hold for most
>compilers, where I thought it wouldn't.

It really depends upon the compiler.  For instance, with Comeau C++
it actually depends upon the operating system what we do.

>But lets leave it at that. The main question I asked -- does the standard
>say anything about this -- has been answered. It doesn't and it shouldn't.
>
>One last thing though, it is something that producers of compilers that can
>be used for both languages could document. That /is/ a QoI question
>ofcourse.

Probably.

- Greg
--
Comeau Computing / Comeau C/C++ 4.2.42 (4.2.44 expected soon)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Martijn Lievaart" <news-from@greebo.orion.nl>
Date: 2000/07/27
Raw View
[ Cross posted to comp.std.c++, I want to know what the gurus say. M4 ]

"Victor Bazarov" <vAbazarov@dAnai.com> wrote in message
news:8lonso$7tq$1@bob.news.rcn.net...
> "Martijn Lievaart" <news-from@greebo.orion.nl> wrote...
> > [...]
> > No, no, it is not about the proper prototype for main, main() must be
> > compiled as C++, not C. E.g. you need main.cpp, not main.c.
> >
> > Let me put it another way. Both C and C++ require their respective
startup
> > code to be executed before main() is called. All C++ compilers I know
of,
> > will initialise the C runtime, but no C compiler will initialise the C++
> > runtime. So unless you don't need the runtime, or are very sure of what
> you
> > are doing, make sure your main() is compiled as C++.
> >
> > I don't think the standard actually requires this, but it is true of any
> > system I know of.
>
> What EXACTLY are you talking about?  What C++ runtime?
> Static variables get initialised OK.  What else?  Exceptions?

Exceptions might be. But statics don't get initialised OK, well they might
they are not required to.

---- A.cpp

static class X
{
public:
    X() {}
} x;

Does x get initialised when main is written in C? I think that the standard
doesn't require it, and that not all implementations will. I'll test later
today if I can find the time what the compilers I use have to say about
this.

M4
--
Contrary to popular belief, the number of the beast is not 666,
it's 555-37689.
Please post replies to this newsgroup. If you must reach
me by email, use <newsgroup-name> at greebo.orion in nl.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "blinky" <blinky@nospam.com>
Date: 2000/07/28
Raw View
"Martijn Lievaart" <news-from@greebo.orion.nl> wrote in message
news:8loom1$30dl$1@buty.wanadoo.nl...
> "Victor Bazarov" <vAbazarov@dAnai.com> wrote in message
<snip - discussion about main being written in a C language module>


Does the language of the module containing the "main" function matter to
initializing library startup code ? (eg, initializing cout, setting head of
atexit chain, whatever)

I would guess not, myself ?




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/07/29
Raw View
Martijn Lievaart wrote:
>
> [ Cross posted to comp.std.c++, I want to know what the gurus say. M4 ]
>
> "Victor Bazarov" <vAbazarov@dAnai.com> wrote in message
> news:8lonso$7tq$1@bob.news.rcn.net...
> > "Martijn Lievaart" <news-from@greebo.orion.nl> wrote...
> > > [...]
> > > No, no, it is not about the proper prototype for main, main() must be
> > > compiled as C++, not C. E.g. you need main.cpp, not main.c.
> > >
> > > Let me put it another way. Both C and C++ require their respective
> startup
> > > code to be executed before main() is called. All C++ compilers I know
> of,
> > > will initialise the C runtime, but no C compiler will initialise the C++
> > > runtime. So unless you don't need the runtime, or are very sure of what
> > you
> > > are doing, make sure your main() is compiled as C++.
> > >
> > > I don't think the standard actually requires this, but it is true of any
> > > system I know of.
> >
> > What EXACTLY are you talking about?  What C++ runtime?
> > Static variables get initialised OK.  What else?  Exceptions?
>
> Exceptions might be. But statics don't get initialised OK, well they might
> they are not required to.

Whether you can have C++ code in a program whose main program is in
some other language depends on the implementation. The C++ standard
does not address the issue.  It can't, really.  It would have to
place requirements on other languages.

Some implementations require that the main program be compiled as C++.
There might conceivably be other requirements, but I don't know of any.

Some implementations have no requirements. For example, using Sun
compilers on Solaris, you can freely mix C, C++, and Fortran in
a single program, and write the main program in any of the languages.
The initialization and finalization appropriate to each language
happens automatically, not depending on the main program at all.
The languages share startup and finalization code in common libraries.

Sun is certainly not the only vendor providing this freedom.  Standard
Unix conventions make it fairly simple to accomplish.  VAX/VMS also
allows free mixing of languages.

The most portable way to mix C and C++ is to compile function "main"
as C++. You can always do that. If your main program is in C, and
will not easily compile as C++, you can simply rename it to C_main,
and do this in C++:

 extern "C" int C_main(int, char**);

 int main(int argc, char** argv)
 {
  return C_main(argc, argv);
 }

If you are using a non-standard system where main is declared with
a void return type, modify the declarations accordingly, and the
body of C++ main becomes just this:
 C_main(argc, argv);

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/07/29
Raw View
Martijn Lievaart wrote:
....
> ---- A.cpp
>
> static class X
> {
> public:
>     X() {}
> } x;
>
> Does x get initialised when main is written in C? I think that the standard
> doesn't require it, and that not all implementations will. I'll test later
> today if I can find the time what the compilers I use have to say about
> this.

Section 3.6.2 p1 of the C++ standard says that "The storage for objects
with static storage duration (3.7.1) shall be zero-initialized (8.5)
before any other initialization takes place. ... Objects of POD types
(3.9) with static storage duration initialized with constant expressions
(5.19) shall be initialized before any dynamic initialization takes
place. ..."

The zero-initialization of x has to occur at the very beginning.
However, there's no constant expression provided to initialize x, so the
second part of that quote doesn't seem to apply. Therefore, we go on to
dynamic initialization.

Section 3.6.2 p3 of the C++ standard says "It is implementation-defined
whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an
object of namespace scope is done before the first statement of main. If
the initialization is deferred to some point in time after the first
statement of main, it shall occur before the first use of any function
or object defined in the same translation unit as the object to be
initialized."

Whatever mechanism is used to achieve that objective, I think it is
likely to be equally useable in mixed C/C++ code as in pure C++.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]