473,667 Members | 2,616 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 "Conversati ons" 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 1825
Evan Carew wrote:
in the September issue of C/C++ Users Journal, there was a technique
mentioned in "Conversati ons" 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 "Conversati ons" 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 "Conversati ons" 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
4237
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: ct_connect(): network packet layer: internal net library error: Net-Library operation terminated due to disconnect
5
2408
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
2736
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 called "form1", and I have selects called "PORTA", "PORTB" ... etc...
3
4313
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 geographical locations. I have been completely unsucessful in acheiving this goal so far however. Things I have tried: Create a shortcut to ftp sight via browser then tried to map local drive to
47
5269
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 work. I want to output a bit pattern from base 10 input. All I get is a zero after the input...I've looked over the code but can't see the problem...any ideas? /* display the bit pattern corresponding to a signed decimal integer */
3
3339
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 o=getobject(,"ConsoleApplication2.Program") msgbox o.TestString
1
3950
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 class. The service is returning with the error code: -2147467259. I have not succeeded to find any additional information about the error. The "SoapFault" method seems to return only a bunch of question marks.
1
3120
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 elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
3
1347
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
3051
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 explorer would do in this case. The headers I am getting are: Headers {Content-Disposition: attachment; filename="dynamic_file.mdb" Connection: close Cache-Control: private Content-Type: application/octet-stream
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8367
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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...
0
8790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8570
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
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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 project—planning, coding, testing, and deployment—without 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
2781
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
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.