Calling C++ from C

J

Joseph Seigh

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.
 
V

Victor Bazarov

Joseph Seigh said:
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++.
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?
 
J

Joseph Seigh

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.
 
V

Victor Bazarov

Joseph Seigh said:
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++?
 
J

Joseph Seigh

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++.
 
V

Victor Bazarov

Joseph Seigh said:
[..] 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.
 
E

E. Robert Tisdale

Joseph said:
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.
 
J

Joseph Seigh

Joseph Seigh said:
[..] 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.
 
D

Dietmar Kuehl

Joseph said:
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.
 
K

Karl Heinz Buchegger

Joseph said:
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.
 
J

Joseph Seigh

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.
 
M

msalters

Joseph said:
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
 
M

Markus Elfring

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
 

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
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top