 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joseph Seigh Guest
|
Posted: Thu Jan 20, 2005 1:01 am Post subject: Calling C++ from C |
|
|
I'm trying to write some C++ library routines callable by C.
The functions are declared extern "C" but I'm getting the
loader complaining it can't resolve new() and delete(). Am I
just missing a library or is the C++ runtime environment
also an issue? Compiling the C program as a C++ program
seems to work but I'd rather have the library look like
a plain C library.
--
Joe Seigh
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jan 20, 2005 1:31 am Post subject: Re: Calling C++ from C |
|
|
"Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
| Quote: | I'm trying to write some C++ library routines callable by C.
The functions are declared extern "C" but I'm getting the
loader complaining it can't resolve new() and delete(). Am I
just missing a library or is the C++ runtime environment
also an issue?
|
Run-time is probably not an issue, but a C++ library is probably
missing. If it's gcc/g++ you're using, try linking using g++.
| Quote: | Compiling the C program as a C++ program
seems to work but I'd rather have the library look like
a plain C library.
|
Why?
|
|
| Back to top |
|
 |
Joseph Seigh Guest
|
Posted: Thu Jan 20, 2005 1:54 am Post subject: Re: Calling C++ from C |
|
|
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
| Quote: | "Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
Compiling the C program as a C++ program
seems to work but I'd rather have the library look like
a plain C library.
Why?
So the library can have a C api. It's easier to write it in C++ |
since I have the C++ classes to do it are already written. I could
rewrite the C++ classes in pseudo object oriented C code but that
would be pretty tedious considering the C++ classes are smart pointer
classes.
--
Joe Seigh
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jan 20, 2005 2:17 am Post subject: Re: Calling C++ from C |
|
|
"Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
| Quote: | On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
[email]v.Abazarov (AT) comAcast (DOT) net[/email]> wrote:
"Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
Compiling the C program as a C++ program
seems to work but I'd rather have the library look like
a plain C library.
Why?
So the library can have a C api. It's easier to write it in C++
since I have the C++ classes to do it are already written. I could
rewrite the C++ classes in pseudo object oriented C code but that
would be pretty tedious considering the C++ classes are smart pointer
classes.
|
Let me get this straight. The library has a C interface. But it'll
be impossible to use from C due to the same problem you yourself
encountered (missing libraries when linking). So, it will only be
useful within a C++ project/program. Why not do the users of it
a favour and leave it C++?
|
|
| Back to top |
|
 |
Joseph Seigh Guest
|
Posted: Thu Jan 20, 2005 3:35 am Post subject: Re: Calling C++ from C |
|
|
On Wed, 19 Jan 2005 21:17:46 -0500, Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
| Quote: | "Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
[email]v.Abazarov (AT) comAcast (DOT) net[/email]> wrote:
Why?
So the library can have a C api. It's easier to write it in C++
since I have the C++ classes to do it are already written. I could
rewrite the C++ classes in pseudo object oriented C code but that
would be pretty tedious considering the C++ classes are smart pointer
classes.
Let me get this straight. The library has a C interface. But it'll
be impossible to use from C due to the same problem you yourself
encountered (missing libraries when linking). So, it will only be
useful within a C++ project/program. Why not do the users of it
a favour and leave it C++?
So the library can have wider application. The implication here is |
I should write libraries in C to begin with since I can use them
to write either C or C++ applications or libraries, but the converse
is not true. Which is too bad since I tend towards OO in design and
would rather not write everything twice, once in C and once in C++.
--
Joe Seigh
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jan 20, 2005 3:38 am Post subject: Re: Calling C++ from C |
|
|
"Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
| Quote: | [..] The implication here is
I should write libraries in C to begin with since I can use them
to write either C or C++ applications or libraries, but the converse
is not true. Which is too bad since I tend towards OO in design and
would rather not write everything twice, once in C and once in C++.
|
I sympathise, believe me. But it seems that even if you "limit"
yourself to doing only C++-targeted libraries, you're not going
to miss too big a segment of the market.
|
|
| Back to top |
|
 |
E. Robert Tisdale Guest
|
Posted: Thu Jan 20, 2005 3:42 am Post subject: Re: Calling C++ from C |
|
|
Joseph Seigh wrote:
| Quote: | Victor Bazarov wrote:
Joseph Seigh wrote:
Victor Bazarov wrote:
Why?
So the library can have a C api. It's easier to write it in C++
since I have the C++ classes to do it are already written. I could
rewrite the C++ classes in pseudo object oriented C code but that
would be pretty tedious considering the C++ classes are smart pointer
classes.
Let me get this straight. The library has a C interface. But it'll
be impossible to use from C due to the same problem you yourself
encountered (missing libraries when linking).
So, it will only be useful within a C++ project/program.
Why not do the users of it a favour and leave it C++?
So the library can have wider application. The implication here is
I should write libraries in C to begin with since I can use them
to write either C or C++ applications or libraries, but the converse
is not true. Which is too bad since I tend towards OO in design and
would rather not write everything twice, once in C and once in C++.
|
I think that you may be confused.
You can write a C library that you can compile
with either a C compiler or a C++ compiler.
You could have one object code library archive for C programmers
and a different object code library archive for C++ programmers
both of which are derived from exactly the same source code repository.
|
|
| Back to top |
|
 |
Joseph Seigh Guest
|
Posted: Thu Jan 20, 2005 4:10 am Post subject: Re: Calling C++ from C |
|
|
On Wed, 19 Jan 2005 22:38:59 -0500, Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
| Quote: | "Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
[..] The implication here is
I should write libraries in C to begin with since I can use them
to write either C or C++ applications or libraries, but the converse
is not true. Which is too bad since I tend towards OO in design and
would rather not write everything twice, once in C and once in C++.
I sympathise, believe me. But it seems that even if you "limit"
yourself to doing only C++-targeted libraries, you're not going
to miss too big a segment of the market.
It's multithreading stuff which mostly gets done in C at this point. |
In this case, it's form of proxy GC based on atomic_ptr and I was
trying to put the api for it in the same form as the RCU for preemptive
user threads api so you could use either without changing code logic.
Let's you do things like AB testing. The atomic_ptr stuff runs faster
but that's on a uniprocessor. Actually, it's an ABC test, C is using
mutexes which is way slower.
--
Joe Seigh
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Thu Jan 20, 2005 7:27 am Post subject: Re: Calling C++ from C |
|
|
Joseph Seigh wrote:
| Quote: | I'm trying to write some C++ library routines callable by C.
The functions are declared extern "C" but I'm getting the
loader complaining it can't resolve new() and delete(). Am I
just missing a library or is the C++ runtime environment
also an issue?
|
You need to perform the link step with a C++ aware tool,
typically with the C++ compiler. This has the fact of linking
the C++ standard library and possibly other stuff like
instantiating necessary templates.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Thu Jan 20, 2005 1:28 pm Post subject: Re: Calling C++ from C |
|
|
Joseph Seigh wrote:
| Quote: |
On Wed, 19 Jan 2005 21:17:46 -0500, Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
"Joseph Seigh" <jseigh_01 (AT) xemaps (DOT) com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
[email]v.Abazarov (AT) comAcast (DOT) net[/email]> wrote:
Why?
So the library can have a C api. It's easier to write it in C++
since I have the C++ classes to do it are already written. I could
rewrite the C++ classes in pseudo object oriented C code but that
would be pretty tedious considering the C++ classes are smart pointer
classes.
Let me get this straight. The library has a C interface. But it'll
be impossible to use from C due to the same problem you yourself
encountered (missing libraries when linking). So, it will only be
useful within a C++ project/program. Why not do the users of it
a favour and leave it C++?
So the library can have wider application.
|
But it can't.
There is no way around it. If it is implemented in C++, then the user
of your library has no other choice then to link the C++ runtime in,
even if he is programming in C.
Here is the deal: What if I have a C compiler only? Then you sold
me a library which is useless for me because I cannot link to it
because of the missing C++ runtime library. So it turns out that
I need to by the C++ compiler (from the same vendor and hopefully
their C library and C++ library are compatible) also, just in order
to link to your library. But then: what is hindering me to use the
C++ compiler in the first place? But then the next question is:
If I am limited to C++ anyway, why did you restrict yourself to
a C API only?
See. When using C++ as implementation language with a C style
interface you have gained nothing et all. I am still forced
to use a C++ compiler.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Joseph Seigh Guest
|
Posted: Thu Jan 20, 2005 3:05 pm Post subject: Re: Calling C++ from C |
|
|
On Thu, 20 Jan 2005 14:28:42 +0100, Karl Heinz Buchegger <kbuchegg (AT) gascad (DOT) at> wrote:
| Quote: | Joseph Seigh wrote:
On Wed, 19 Jan 2005 21:17:46 -0500, Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
Let me get this straight. The library has a C interface. But it'll
be impossible to use from C due to the same problem you yourself
encountered (missing libraries when linking). So, it will only be
useful within a C++ project/program. Why not do the users of it
a favour and leave it C++?
So the library can have wider application.
But it can't.
There is no way around it. If it is implemented in C++, then the user
of your library has no other choice then to link the C++ runtime in,
even if he is programming in C.
Here is the deal: What if I have a C compiler only? Then you sold
me a library which is useless for me because I cannot link to it
because of the missing C++ runtime library. So it turns out that
I need to by the C++ compiler (from the same vendor and hopefully
their C library and C++ library are compatible) also, just in order
to link to your library. But then: what is hindering me to use the
C++ compiler in the first place? But then the next question is:
If I am limited to C++ anyway, why did you restrict yourself to
a C API only?
See. When using C++ as implementation language with a C style
interface you have gained nothing et all. I am still forced
to use a C++ compiler.
|
You're missing the point. The bulk of the applications that would
use the api are written in C. If I restrict the api to C++, I lose
most of those applications since most multi-threading applications
are written in C.
The stuff I'm planning to do is here
http://atomic-ptr-plus.sourceforge.net/
subject to change. I'll have to seriously consider rewriting atomic_ptr
in C. It'd be a lot more awkard to use but it might be worth it to
avoid C++ controversy.
--
Joe Seigh
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Thu Jan 20, 2005 4:13 pm Post subject: Re: Calling C++ from C |
|
|
Joseph Seigh wrote:
| Quote: | I'm trying to write some C++ library routines callable by C.
The functions are declared extern "C" but I'm getting the
loader complaining it can't resolve new() and delete(). Am I
just missing a library or is the C++ runtime environment
also an issue? Compiling the C program as a C++ program
seems to work but I'd rather have the library look like
a plain C library.
|
If you're compiling C++, you'll have to include the C++ Standard
at some point. Now, you're getting close to off-topic because
you're building something without a main(), but anyway: when
compiling a DLL you end up with two link steps. If you can't
include the C++ lib in the second (dynamic) link fase, you
must link it in the first (static) link fase.
Of course, no guarantees yet, libraries are not yet in ISO C++,
and mixed-lang programming is always implementation-defined.
Regards,
Michiel Salters
|
|
| Back to top |
|
 |
Markus Elfring Guest
|
Posted: Fri Feb 11, 2005 9:12 pm Post subject: Re: Calling C++ from C |
|
|
| Quote: | I'm trying to write some C++ library routines callable by C.
The functions are declared extern "C" but I'm getting the
loader complaining it can't resolve new() and delete(). Am I
just missing a library or is the C++ runtime environment
also an issue? Compiling the C program as a C++ program
seems to work but I'd rather have the library look like
a plain C library.
|
Would you like to apply the techniques that are described by Matthew Wilson in the
sections "7.4 I Can C Clearly Now" and "7.4.4 Getting a Handle on C++ Classes" of the book
"Imperfect C++" (ISBN 0-321-22877-4) for your evolving library?
http://atomic-ptr-plus.sourceforge.net/
Regards,
Markus
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|