473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Jul 22 '05 #1
12 1800
"Joseph Seigh" <js*******@xemaps.com> wrote...
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?
Jul 22 '05 #2
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov <v.********@comAcast.net> wrote:
"Joseph Seigh" <js*******@xemaps.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
Jul 22 '05 #3
"Joseph Seigh" <js*******@xemaps.com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:
"Joseph Seigh" <js*******@xemaps.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++?
Jul 22 '05 #4
On Wed, 19 Jan 2005 21:17:46 -0500, Victor Bazarov <v.********@comAcast.net> wrote:
"Joseph Seigh" <js*******@xemaps.com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
<v.********@comAcast.net> 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
Jul 22 '05 #5
"Joseph Seigh" <js*******@xemaps.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.
Jul 22 '05 #6
Joseph Seigh wrote:
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.
Jul 22 '05 #7
On Wed, 19 Jan 2005 22:38:59 -0500, Victor Bazarov <v.********@comAcast.net> wrote:
"Joseph Seigh" <js*******@xemaps.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
Jul 22 '05 #8
Joseph Seigh wrote:
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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #9
Joseph Seigh wrote:

On Wed, 19 Jan 2005 21:17:46 -0500, Victor Bazarov <v.********@comAcast.net> wrote:
"Joseph Seigh" <js*******@xemaps.com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
<v.********@comAcast.net> 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
kb******@gascad.at
Jul 22 '05 #10
On Thu, 20 Jan 2005 14:28:42 +0100, Karl Heinz Buchegger <kb******@gascad.at> wrote:
Joseph Seigh wrote:

On Wed, 19 Jan 2005 21:17:46 -0500, Victor Bazarov <v.********@comAcast.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
Jul 22 '05 #11
Joseph Seigh wrote:
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

Jul 22 '05 #12
> 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
Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.