473,705 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1829
"Joseph Seigh" <js*******@xema ps.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.********@com Acast.net> wrote:
"Joseph Seigh" <js*******@xema ps.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*******@xema ps.com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
<v.********@com Acast.net> wrote:
"Joseph Seigh" <js*******@xema ps.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.********@com Acast.net> wrote:
"Joseph Seigh" <js*******@xema ps.com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
<v.********@com Acast.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*******@xema ps.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.********@com Acast.net> wrote:
"Joseph Seigh" <js*******@xema ps.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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #9
Joseph Seigh wrote:

On Wed, 19 Jan 2005 21:17:46 -0500, Victor Bazarov <v.********@com Acast.net> wrote:
"Joseph Seigh" <js*******@xema ps.com> wrote...
On Wed, 19 Jan 2005 20:31:59 -0500, Victor Bazarov
<v.********@com Acast.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

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

Similar topics

1
2319
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 static and non-static class member function? 4. Could it be possible to specify different calling convention for each "global" or each class member function? Even in a single file scope?
8
2961
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 p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
7
6604
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 written in C++ and it's calling a java application. This application displays a window and pushing a button is calling back the c++-plugin again.
5
3427
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
3
9097
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. If the web service is taking a long time to complete, the aspx page returns a ‘The operation has timed-out.’ Message to the web browser after 100 seconds. I’ve added: <httpRuntime executionTimeout="300" /> to the web.config files
2
3146
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 the stack pointer and it does it by the command "add esp,8" after returning from the called function. My questions: 1. Is the stack pointer common in a certain thread(or process)? 2. How does the called function get the parameters, is it by...
47
4962
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 and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
7
2685
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
10
3256
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 programming system places on how a procedure is called and how data is passed between a calling program and procedures are called calling conventions"
0
9273
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9030
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7893
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5933
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3137
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2081
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.