473,508 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing C++ from C

in the September issue of C/C++ Users Journal, there was a technique
mentioned in "Conversations" which suggested how to provide generic
access to C++ classes from C by wrapping your C++ in C. My problem is
that I can't figure otu how this is done. My gcc linker keeps
complaining that it can't find object code for the STL library members.
Has anyone else tried to do this and gotten it to work? if so, could you
let me know how?

Thanks,
Evan Carew

Jul 22 '05 #1
9 1819
Evan Carew wrote:
in the September issue of C/C++ Users Journal, there was a technique
mentioned in "Conversations" which suggested how to provide generic
access to C++ classes from C by wrapping your C++ in C. My problem is
that I can't figure otu how this is done. My gcc linker keeps
complaining that it can't find object code for the STL library
members.


This is actually off-topic here, and should be asked in gnu.gcc.help or
gnu.g++.help, but anyway, you need to use g++ for linking if your
program contains c++ code.

Jul 22 '05 #2
On Wed, 07 Jan 2004 18:36:00 -0500, Evan Carew wrote:
in the September issue of C/C++ Users Journal, there was a technique
mentioned in "Conversations" which suggested how to provide generic
access to C++ classes from C by wrapping your C++ in C. My problem is
that I can't figure otu how this is done. My gcc linker keeps
complaining that it can't find object code for the STL library members.
Has anyone else tried to do this and gotten it to work? if so, could you
let me know how?


Tricky at best. The linking issue can be easily solved, include the right
library path (or use g++, it'll compile C code fine, it differs from gcc
in what include and library paths it adds, not how it looks at source code).

Another issue is the C++ RTL. That probably should be initialised, which
is system dependent and may or may not be necessary. The easiest solution
to this is to rename the C main to C_main, write your main in C++ that
only calls this C_main.

HTH,
M4

Jul 22 '05 #3
Martijn Lievaart wrote:
On Wed, 07 Jan 2004 18:36:00 -0500, Evan Carew wrote:
in the September issue of C/C++ Users Journal, there was a technique
mentioned in "Conversations" which suggested how to provide generic
access to C++ classes from C by wrapping your C++ in C. My problem is
that I can't figure otu how this is done. My gcc linker keeps
complaining that it can't find object code for the STL library
members. Has anyone else tried to do this and gotten it to work? if
so, could you let me know how?
Tricky at best. The linking issue can be easily solved, include the
right library path (or use g++, it'll compile C code fine, it differs
from gcc in what include and library paths it adds, not how it looks
at source code).


That's not quite true. g++ will assume your code is C++, while gcc will
try to auto-detect it from the file name. For _linking_ however, g++
will add anything to the linker command line that is needed for C++
code to work.
Another issue is the C++ RTL.
What is a C++ RTL?
That probably should be initialised, which is system dependent and may
or may not be necessary. The easiest solution to this is to rename the
C main to C_main, write your main in C++ that only calls this C_main.


I don't see why this would be needed.

Jul 22 '05 #4
On Thu, 08 Jan 2004 11:36:52 +0100, Rolf Magnus wrote:
Tricky at best. The linking issue can be easily solved, include the
right library path (or use g++, it'll compile C code fine, it differs
from gcc in what include and library paths it adds, not how it looks
at source code).
That's not quite true. g++ will assume your code is C++, while gcc will
try to auto-detect it from the file name. For _linking_ however, g++
will add anything to the linker command line that is needed for C++
code to work.


My! Learned something. You're right, although this is pretty stupid.
Another issue is the C++ RTL.
What is a C++ RTL?


Run time library. More specifically the start-up code. The thing that
calls main and is responsible for initialising all kind of stuff. I'm not
sure about C++ implementations, but all C implementations I've linked from
other languages so far had a RTL that had to be initialised before one
could use non-trivial stuf from the standard library. I've seen this for
file I/O, atexit, floating point calculations and probably some I forgot.

In C++, initialisation of globals is most probably handled from the RTL.
That probably should be initialised, which is system dependent and may
or may not be necessary. The easiest solution to this is to rename the
C main to C_main, write your main in C++ that only calls this C_main.


I don't see why this would be needed.


See above. BTW, as C++ includes the C standard library, and implementors
should allow linkage to C code, using the C++ startup code ensures that
the C startup code is executed as well.

HTH,
M4

Jul 22 '05 #5
Rolf, (and all others who replied with advice of varying degrees of
usefullness)

Thanks for your replies. While I generally don't like people who answer
their own questions, I have a feeling that this question is important
enough to the C++ and C community that it needs to be answered. I say
this because if C developers (or C++ developers) don't have a way to
gradually migrate their legacy code (C) to C++ then we will continue in
our current state of affairs we are in today where people like the
developers of GNOME continue to develop in C. With this technique,
someone could refactor their libraries in C++ and provide a C wrapper
for those still using legacy techniques, while newer developers could go
on to use C++.

Yesterday, I sent a message to the author of C/C++ Users Journal article
I mentioned in my query & he replied with the answer last night. It
turns out to be rather easy. The deal is that while the C compiler is
more than happy to compile the legacy code, and the C++ compiler
likewise happy to compile the wrapper code, the C linker simply wont
link the program. The strange fix is to compile the legacy code with
the C compiler (without linking), then link with g++. Voila, you have a
C front end to C++ library code.

So now, whenever I talk to some C jock who swears there is no way for C
to converse with C++, I'll simply point them to my interface code.

P.S.
If you are interested in a working swample project, I can send you a
gziped tar file which you can compile yourself.

Evan

Rolf Magnus wrote:
Evan Carew wrote:

[snip]

This is actually off-topic here, and should be asked in gnu.gcc.help or
gnu.g++.help, but anyway, you need to use g++ for linking if your
program contains c++ code.


Jul 22 '05 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message news:bt*************@news.t-online.com...
).

That's not quite true. g++ will assume your code is C++, while gcc will
try to auto-detect it from the file name. For _linking_ however, g++
will add anything to the linker command line that is needed for C++
code to work.
Both gcc and g++ recognize from the file names whether to compile
as C or C++ (or Fortran and probably some other languages as well).

The only only different is the inclusion of the C++ libraries with G++.
Another issue is the C++ RTL.
What is a C++ RTL?


Run time library. All of the stuff in the standard library plus miscellaneous glue
routines that the implementation needs (like the thing that runs around and does the
pre-mail dynamic initializations etc...).
should be initialised, which is system dependent and may
or may not be necessary. The easiest solution to this is to rename the
C main to C_main, write your main in C++ that only calls this C_main.


I don't see why this would be needed.


It's not needed in G++/GCC. Even with a C main function, gcc inserts code
that invokes the pre-main startup stuff and if you're linked with G++ libraries
it will work.

However, that's a nice feature of GCC/G++ only. The language doesn't
make any guarantees about C++ programs that don't have a C++ main.

Jul 22 '05 #7
Ron Natalie wrote:
Both gcc and g++ recognize from the file names whether to compile
as C or C++ (or Fortran and probably some other languages as well).
Well, I don't know which version you have, but 2.95 as well as all 3.x
versions I ever used don't do it like that, and I acually tried it with
my current 3.3.2 version to be sure before posting. I tried the
following program:

#include <stdlib.h>

int main()
{
int* x = malloc(sizeof *x);
}

and gcc said nothing, but g++ said:

c_code.c: In function `int main()':
c_code.c:5: error: invalid conversion from `void*' to `int*'

since in C++, that conversion cannot be done implicitly. Note that the
file name ends in '.c'.
The only only different is the inclusion of the C++ libraries with
G++.


You mean the linking to them when using g++ for linking.

Jul 22 '05 #8
Evan Carew wrote:
Rolf, (and all others who replied with advice of varying degrees of
usefullness)

Thanks for your replies. While I generally don't like people who
answer their own questions, I have a feeling that this question is
important enough to the C++ and C community that it needs to be
answered. I say this because if C developers (or C++ developers) don't
have a way to gradually migrate their legacy code (C) to C++ then we
will continue in our current state of affairs we are in today where
people like the developers of GNOME continue to develop in C.
Actually, there are C++ wrappers for the GNOME libs (e.g. gtkmm) and
some gnome programs are actually written in C++. There are also quite
some Un*x libs that are written in C++ and used from C programs, or
even plugin systems where the plugins and the main program are not
written in the same language, so glueing C code and C++ code together
isn't actually that uncommon.
With this technique, someone could refactor their libraries in C++ and
provide a C wrapper for those still using legacy techniques, while
newer developers could go on to use C++.
Some developers want to use C rather than C++.
Yesterday, I sent a message to the author of C/C++ Users Journal
article I mentioned in my query & he replied with the answer last
night. It turns out to be rather easy. The deal is that while the C
compiler is more than happy to compile the legacy code, and the C++
compiler likewise happy to compile the wrapper code, the C linker
simply wont link the program. The strange fix is to compile the legacy
code with the C compiler (without linking), then link with g++. Voila,
you have a C front end to C++ library code.


That's what I told you. Just look yourself at what happens. When trying
to link with gcc and with g++, add the -v command line option, and the
compiler will tell you what it does and what command line it passes to
the linker.

Jul 22 '05 #9
Rolf Magnus wrote:
Evan Carew wrote:

[snip]

Actually, there are C++ wrappers for the GNOME libs (e.g. gtkmm) and
some gnome programs are actually written in C++. There are also quite
some Un*x libs that are written in C++ and used from C programs, or
even plugin systems where the plugins and the main program are not
written in the same language, so glueing C code and C++ code together
isn't actually that uncommon. I've seen the fine job the gtkmm folks did with the C++ wrapper, I've
also been stepping through the underlying gtk code as of late & that is
exactly why I am here talking about this technique. In other words, it
sure would be nice to be able to refactor this code in an OO environment.

As for gluing C to C++, yes, it is done, but what I have been seeing up
to now has involved use of knowledge in how C++ mangles its names & then
tying that into the C environment. Not what I would call generic
programming (or version stable for that matter).

I've also been informed recently that there are some projects as you
describe which utilize this technique, however, for some reason its use
hasn't been documented in any texts, or manuals I have seen. Even the
article in C/C++ Users Journal I saw didn't discuss the issues related
to getting your compiler to actually link all the code together. Perhaps
this subject could stand some dissemination.
With this technique, someone could refactor their libraries in C++ and
provide a C wrapper for those still using legacy techniques, while
newer developers could go on to use C++.

Some developers want to use C rather than C++.

Yep, especially if they are embedded programmers like myself. There is a
place for C++ and typically, embedded programming isn't it. On the other
hand, GUI programming is perfect for C++ & I can't understand why some
projects persist in having their core development done in C (like
GNOME). KDE for example is done (mostly) in standard C++ & is rather
pleasant to program in. Gnome on the other hand is done entirely in C as
are most of its core components & working with those is ... well... lets
just say that it could be better. While the C++ wrapper libraries for
GNOME are fine, it is a little like kissing through saran wrap. If you
need to change anything, you still need to drop into C.

Jul 22 '05 #10

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

Similar topics

1
4230
by: Amy Tseng | last post by:
Hi, I am having a problem accessing SQL Server 2000 via UNIX. I am accessing SQL Server 2000 from Solaris using Sybase Open Client (CT-Lib). Here is the error message: CT-LIBRARY error:...
5
2396
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
6
2716
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
3
4303
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
47
5221
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
3
3336
by: AdamM | last post by:
Hi all, When I run my VbScript, I get the error: "ActiveX component can't create object: 'getobject'. Error 800A01AD". Any ideas what I did wrong? Here's my VBScript: dim o set...
1
3931
by: Eirik Brattbakk | last post by:
Hi I have some problems accessing a soap service made in c# using an ATL/MFC client over SSL. I have tried both CSoapMSXMLInetClient and CSoapWininetClient as template arguments with my stub...
1
3108
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
3
1338
by: niju | last post by:
Hi there, I have three web pages (A,B,C). I need to prevent users accessing page B and C without accessing A. What would be the best way to achieve this rule? Many Thanks Niju
5
3034
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
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
7391
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...
1
7054
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...
0
4713
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...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
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...

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.