Clear doubts regarding the C++ Runtime

R

ritesh

Hi,

I have been reading some text on C and C++ (i.e advanced books). One
of the books mentioned that C++ requires a runtime support whereas C
does not - what the author was trying to say was that once you compile
a C program the executable created is all that is needed whereas if you
compile a C++ program the executable created requires a C++ runtime
installed on your system to run the program.

Can someone please provide more information on this or could provide a
link to some webiste where this concept is explained in detail. I
would like to know the differences in C and C++ compiled code which
makes a runtime necessary for one and not for the other.

Thanks
Ritesh
 
T

Tim Prince

ritesh said:
Hi,

I have been reading some text on C and C++ (i.e advanced books). One
of the books mentioned that C++ requires a runtime support whereas C
does not - what the author was trying to say was that once you compile
a C program the executable created is all that is needed whereas if you
compile a C++ program the executable created requires a C++ runtime
installed on your system to run the program.

Can someone please provide more information on this or could provide a
link to some webiste where this concept is explained in detail. I
would like to know the differences in C and C++ compiled code which
makes a runtime necessary for one and not for the other.
If you mean that it is usual to link shared libraries (such as .so or .dll),
that is true of both C and C++, but C++ requires more of them. 20 years
ago, C implementations which supported only static linking were more common
than now. Static linking will inflate the size of a C++ build much more
than a C build. Reasons for the differences are surely not topical on
c.l.c.
 
C

Chris Torek

If you mean that it is usual to link shared libraries (such as .so or .dll),
that is true of both C and C++, but C++ requires more of them. ...

I suspect the real question was more about RTTI and/or exception
handling, both of which complicate C++ enormously (compared to C).
Of course, comp.lang.c is the wrong newsgroup on which to discuss
either one.
... Static linking will inflate the size of a C++ build much more
than a C build. Reasons for the differences are surely not topical on
c.l.c.

Indeed.

(In theory, of course, a C++ program that avoids using the RTTI
and exception features -- and much of the rest of C++ along with
them, since, e.g., "new" now throws exceptions -- could compile to
similary-small static-linked binaries. Of course, if one is going
to avoid all these C++ features, I would wonder why that one is
writing C++ code at all. If one simply wants, e.g., the convenience
of variable declaration at point of first use, C99 supports that
already; and quite a few C compilers, while not yet "full C99",
include this now as well.)

Last, a note to the original poster: the phrase "clear doubts", in
English, as used in the subject line means approximately: "these
are things I am pretty sure are wrong", not "could you expound on
something so as to clear up some questions I have". The word
"clear" here takes the role of an adjective, modifying the noun
"doubts", and means "these doubts are clear (i.e., should be obvious
to all)". The word "doubt", in turn, does not translate directly
to "question", but rather to "questionable items" or "dubious
things". The phrase "clear up", in which clear is a verb (rather
than an adjective), *does* mean "answer questions", but the word
"up" is usually required.

English, she is a funny old language. :)
 
C

CBFalconer

Chris said:
I suspect the real question was more about RTTI and/or exception
handling, both of which complicate C++ enormously (compared to C).
Of course, comp.lang.c is the wrong newsgroup on which to discuss
either one.


Indeed.

(In theory, of course, a C++ program that avoids using the RTTI
and exception features -- and much of the rest of C++ along with
them, since, e.g., "new" now throws exceptions -- could compile to
similary-small static-linked binaries. Of course, if one is going
to avoid all these C++ features, I would wonder why that one is
writing C++ code at all. If one simply wants, e.g., the convenience
of variable declaration at point of first use, C99 supports that
already; and quite a few C compilers, while not yet "full C99",
include this now as well.)

I think the primary conclusion is that the original "advanced" book
author is woefully misinformed. In fact I would characterize him
as "having his head up where the sun don't shine".
 
R

REH

ritesh said:
Hi,

I have been reading some text on C and C++ (i.e advanced books). One
of the books mentioned that C++ requires a runtime support whereas C
does not - what the author was trying to say was that once you compile
a C program the executable created is all that is needed whereas if you
compile a C++ program the executable created requires a C++ runtime
installed on your system to run the program.

Can someone please provide more information on this or could provide a
link to some webiste where this concept is explained in detail. I
would like to know the differences in C and C++ compiled code which
makes a runtime necessary for one and not for the other.

Thanks
Ritesh

That's wholly dependent on the implementation. Both languages require
support of a library. Whether that library is statically linked into the
executable or not, depends on your system, not the language. Also,
depending on the platform you are building for, the library may or not
require additional support from the (if existing) OS.
 
M

Malcolm

ritesh said:
I have been reading some text on C and C++ (i.e advanced books). One
of the books mentioned that C++ requires a runtime support whereas C
does not - what the author was trying to say was that once you compile
a C program the executable created is all that is needed whereas if you
compile a C++ program the executable created requires a C++ runtime
installed on your system to run the program.

Can someone please provide more information on this or could provide a
link to some webiste where this concept is explained in detail. I
would like to know the differences in C and C++ compiled code which
makes a runtime necessary for one and not for the other.
If I want to create an array in C I would normally do it like this

int *array = malloc(sizeof(int) * N);

in C++ I would do it like this

int *array = new int[N];

(Or these days I would use the standard template library).

So the C code is calling malloc explictly, whilst C++ is probably calling
the same operating system function underneath, but has folded the call into
the language.

Now let's say that I want to port my code to a games console that doesn't
have a malloc() or any OS-defined memory management system.

The C code I can port by writing my own malloc(), and calling that. Or I can
write the code more carefully so that it uses fixed size arrays and doesn't
call malloc() at all.
The C++ code is a bit more difficult. The call to malloc() is hidden in the
language itself. Even if we get rid of all the news, the compiler will still
probably want to know how to implement the keyword. That is probably what
your author means.

However C++ is cleverer than that. It is possible to globally overload the
new and delete operators, so that they call your custom allocation system.
So I suspect your authors are not entirely accurate.
 
G

Gordon Burditt

I have been reading some text on C and C++ (i.e advanced books). One
of the books mentioned that C++ requires a runtime support whereas C
does not - what the author was trying to say was that once you compile
a C program the executable created is all that is needed whereas if you
compile a C++ program the executable created requires a C++ runtime
installed on your system to run the program.

It is possible for both C and C++ to compile with shared libraries
that must be present on the execution system at runtime for the
program to work. This is often the default behavior (e.g. on
FreeBSD and probably on most UNIX systems where shared libraries
are implemented). On most of these systems, it is also possible
to compile static so the executable alone will run when it is copied
to another system. That executable is likely to be a bigger file.

What the author might have meant is that the C libraries are commonly
distributed with the operating system, whereas the C++ aren't. If
so, I'm not sure what platform he's talking about. It may also be
that the author's C++ compiler requires run-time licensing, enforced
by code generated by the C++ compiler.
Can someone please provide more information on this or could provide
a >link to some webiste where this concept is explained in detail.
I >would like to know the differences in C and C++ compiled code
which >makes a runtime necessary for one and not for the other.

MySQL client libraries are not normally distributed with FreeBSD.
If you wish to use C with MySQL on FreeBSD you need to do one of
(a) compile the executable static, or (b) copy the client shared
library to the runtime machine and install it. The same sort of
thing applies to other database client libraries and a lot of other
code that isn't bundled with the OS.

Gordon L. Burditt
 

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
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top