linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* C or C++
@ 2007-05-02 14:20 Shriramana Sharma
  0 siblings, 0 replies; 6+ messages in thread
From: Shriramana Sharma @ 2007-05-02 14:20 UTC (permalink / raw)
  To: Linux C Programming List

Hello.

I am writing a library and wondering whether to write it in C or C++. 
Previously I knew only C but now I got introduced to the many 
conveniences of C++ I am loth to give them up. I also feel C++ is a 
cleaner language than C (for whatever reason).

OTOH I am thinking maybe if I write in C many more programmers will be 
able to use it - C programmers as well as C++ programmers, whereas if I 
write in C++ only C++ programmers can use it (i.e. if the API contains 
any C++-specific items [or even if otherwise?]).

I would like the list's opinion on whether it is worth sacrificing the 
advantages of C++ to capture more "clients".

A voice inside tells me -- Qt and KDE are pure C++, yet they are among 
the hugest-used (if there is such a word) libraries... But another voice 
tells me -- there is GTK, GNOME and so many other libraries which I do 
not know which may be having many users precisely because they are in 
pure C...

So I don't want to be like the frog in the well (who did not know there 
was such a thing as an ocean) and so I am asking you.

Namaste.

Shriramana Sharma.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* C or C++
@ 2007-05-04 11:00 Shriramana Sharma
  2007-05-04 11:17 ` leslie.polzer
  2007-05-04 13:46 ` Glynn Clements
  0 siblings, 2 replies; 6+ messages in thread
From: Shriramana Sharma @ 2007-05-04 11:00 UTC (permalink / raw)
  To: Linux C Programming List

Hello.

I am writing a library and wondering whether to write it in C or C++.
Previously I knew only C but now I got introduced to the many
conveniences of C++ I am loth to give them up. I also feel C++ is a
cleaner language than C (for whatever reason).

OTOH I am thinking maybe if I write in C many more programmers will be
able to use it - C programmers as well as C++ programmers, whereas if I
write in C++ only C++ programmers can use it (i.e. if the API contains
any C++-specific items [or even if otherwise?]).

I would like the list's opinion on whether it is worth sacrificing the
advantages of C++ to capture more "clients".

A voice inside tells me -- Qt and KDE are pure C++, yet they are among
the hugest-used (if there is such a word) libraries... But another voice
tells me -- there is GTK, GNOME and so many other libraries which I do
not know which may be having many users precisely because they are in
pure C...

So I don't want to be like the frog in the well (who did not know there
was such a thing as an ocean) and so I am asking you.

Namaste.

Shriramana Sharma.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C or C++
  2007-05-04 11:00 C or C++ Shriramana Sharma
@ 2007-05-04 11:17 ` leslie.polzer
  2007-05-04 13:46 ` Glynn Clements
  1 sibling, 0 replies; 6+ messages in thread
From: leslie.polzer @ 2007-05-04 11:17 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: Linux C Programming List

[-- Attachment #1: Type: text/plain, Size: 832 bytes --]

On Fri, May 04, 2007 at 04:30:20PM +0530, Shriramana Sharma wrote:

> A voice inside tells me -- Qt and KDE are pure C++, yet they are among
> the hugest-used (if there is such a word) libraries... But another
> voice tells me -- there is GTK, GNOME and so many other libraries
> which I do not know which may be having many users precisely because
> they are in pure C...

I choose C or C++ depending on the approach I plan to use.  If it's
system programming, or anything procedural, I'm most likely going to use
C.  If it's some high-level modeling that excellently suits OOP, I'm
going for C++.

I can therefore only answer your question if you tell me what the
library will do.

  Leslie

-- 
Personal homepage: https://viridian.dnsalias.net/~sky/homepage/
gpg --keyserver pgp.mit.edu --recv-keys DD4EBF83

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C or C++
  2007-05-04 11:00 C or C++ Shriramana Sharma
  2007-05-04 11:17 ` leslie.polzer
@ 2007-05-04 13:46 ` Glynn Clements
  2007-05-16 13:44   ` Shriramana Sharma
  1 sibling, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2007-05-04 13:46 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: Linux C Programming List


Shriramana Sharma wrote:

> I am writing a library and wondering whether to write it in C or C++.
> Previously I knew only C but now I got introduced to the many
> conveniences of C++ I am loth to give them up. I also feel C++ is a
> cleaner language than C (for whatever reason).
> 
> OTOH I am thinking maybe if I write in C many more programmers will be
> able to use it - C programmers as well as C++ programmers, whereas if I
> write in C++ only C++ programmers can use it (i.e. if the API contains
> any C++-specific items [or even if otherwise?]).
> 
> I would like the list's opinion on whether it is worth sacrificing the
> advantages of C++ to capture more "clients".
> 
> A voice inside tells me -- Qt and KDE are pure C++, yet they are among
> the hugest-used (if there is such a word) libraries... But another voice
> tells me -- there is GTK, GNOME and so many other libraries which I do
> not know which may be having many users precisely because they are in
> pure C...
> 
> So I don't want to be like the frog in the well (who did not know there
> was such a thing as an ocean) and so I am asking you.

It depends upon what you want to do with the library.

If it's likely to be useful to other libraries, there's a strong
incentive to use C, or at least to provide a C API (the standard
implementation of GLU is written in C++, although the public API only
uses C).

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C or C++
  2007-05-04 13:46 ` Glynn Clements
@ 2007-05-16 13:44   ` Shriramana Sharma
  2007-05-22 18:10     ` Glynn Clements
  0 siblings, 1 reply; 6+ messages in thread
From: Shriramana Sharma @ 2007-05-16 13:44 UTC (permalink / raw)
  To: Glynn Clements; +Cc: Linux C Programming List

Glynn Clements wrote:
> If it's likely to be useful to other libraries, there's a strong
> incentive to use C, or at least to provide a C API (the standard
> implementation of GLU is written in C++, although the public API only
> uses C).

Would it be enough if I ensure that the API header file does not contain 
any C++-only usages for such a library internally implemented in C++ to 
be linkable from a C program or library?

Or would there be problems with the C linker not being able to resolve 
references from the C object code to C++ symbols in the library? (I hope 
I got that terminology correct.)

Thanks again.

Shriramana Sharma.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C or C++
  2007-05-16 13:44   ` Shriramana Sharma
@ 2007-05-22 18:10     ` Glynn Clements
  0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2007-05-22 18:10 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: Linux C Programming List


Shriramana Sharma wrote:

> > If it's likely to be useful to other libraries, there's a strong
> > incentive to use C, or at least to provide a C API (the standard
> > implementation of GLU is written in C++, although the public API only
> > uses C).
> 
> Would it be enough if I ensure that the API header file does not contain 
> any C++-only usages for such a library internally implemented in C++ to 
> be linkable from a C program or library?
> 
> Or would there be problems with the C linker not being able to resolve 
> references from the C object code to C++ symbols in the library? (I hope 
> I got that terminology correct.)

You can't link C code against C++ symbols.

If you want the library to be usable from C, the public interface must
consist of functions (not methods) which are declared with C linkage
(extern "C" ...). Those functions can call C++ code.

If you need to pass pointers to objects between C++ and C, you need to
cast them to/from something which is valid in C (e.g. "void *").

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-05-22 18:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-04 11:00 C or C++ Shriramana Sharma
2007-05-04 11:17 ` leslie.polzer
2007-05-04 13:46 ` Glynn Clements
2007-05-16 13:44   ` Shriramana Sharma
2007-05-22 18:10     ` Glynn Clements
  -- strict thread matches above, loose matches on Subject: below --
2007-05-02 14:20 Shriramana Sharma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).