comp.lang.ada
 help / color / mirror / Atom feed
* linking with C++
@ 1998-04-20  0:00 Martin Klang
  1998-04-21  0:00 ` Robert Dewar
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Martin Klang @ 1998-04-20  0:00 UTC (permalink / raw)



lately i've been trying out ADA, and even though it's not by a long way as
esthetically pleasing as C++, i must confess it's a pretty useful little
language.

my problem (one of many, though the only one i'll cumber you with):

i cannot link to C++ with gnatlink.

even compiling the exact same code with g++ instead of gcc will produce an 
error with gnatlink, which claims the referenced function is undefined.

this is what happens:

//file cstuff.c
#include <stdio.h>
void c_test()
{
  printf("jah!");
}
//file cstuff.c

which i compile with:
>gcc -c cstuff.c -o c.o
>g++ -c cstuff.c -o cc.o

//file test.adb
procedure Test is
   procedure C_Test;
   pragma Import(CPP,C_Test,"c_test");
begin
   C_Test;
end Tester;        
//file test.adb

now, if i do:
>gnatmake test -largs c.o
it works, but if i do:
>gnatmake test -largs cc.o
it fails to link.

it seems to have no importance in this example whether i use pragma C or CPP
(what is the difference anyhow?)

comparing c.o and cc.o yields that g++ somewhere along the line renamned the
c_test-function to c_test__Fv. Why???

any help much appreciated,
martin


  "Disobedience is the true foundation of liberty.
   The obedient must be slaves."

--Henry David Thoreau, 1847




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: linking with C++
  1998-04-20  0:00 linking with C++ Martin Klang
@ 1998-04-21  0:00 ` Robert Dewar
  1998-04-21  0:00 ` Justin Braach
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 1998-04-21  0:00 UTC (permalink / raw)



Martin Klang said

<<lately i've been trying out ADA, and even though it's not by a long way as
esthetically pleasing as C++, i must confess it's a pretty useful little
language.
>>

That's certainly a collectoin of entertaining opinions on Ada

1. C++ is more aesthetically pleasing -- of course taste is indeed in the
 	eye of the beholder :-) :-)

2. Ada is a little language :-)

Still hopefully we can all agree that Ada is useful!






^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: linking with C++
  1998-04-20  0:00 linking with C++ Martin Klang
                   ` (2 preceding siblings ...)
  1998-04-21  0:00 ` Jerry van Dijk
@ 1998-04-21  0:00 ` Robert Dewar
  1998-04-23  0:00   ` [GNAT] Extending a C++ class Jacob Sparre Andersen
  3 siblings, 1 reply; 6+ messages in thread
From: Robert Dewar @ 1998-04-21  0:00 UTC (permalink / raw)



Martin, I think you have very little chance of succeeding with this attempt
at direct interfacing. If you want to pursue this, read carefully the
documentation on the special C++ interfacing pragmas in GNAT.

These C++ interfacing pragmas are very powerful, and are used for example
by SGI to give access to their C++ graphics packages like Inventor.

But you really have to know what you are doing to use them. Generally they
are intended for use by a binding generator, not by humans. They can be
used by humans, but you really need to understand a lot.

The easiest thing for most people is to write a C wrapper for their C++
code and to interface to the C wrapper. This can be done using only
standard features of the language as described in annex B of the RM.

Robert Dewar
Ada Core Technologies





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: linking with C++
  1998-04-20  0:00 linking with C++ Martin Klang
  1998-04-21  0:00 ` Robert Dewar
  1998-04-21  0:00 ` Justin Braach
@ 1998-04-21  0:00 ` Jerry van Dijk
  1998-04-21  0:00 ` Robert Dewar
  3 siblings, 0 replies; 6+ messages in thread
From: Jerry van Dijk @ 1998-04-21  0:00 UTC (permalink / raw)



Martin Klang (md4mars@mdstud.chalmers.se) wrote:

: lately i've been trying out ADA, and even though it's not by a long way as
: esthetically pleasing as C++, i must confess it's a pretty useful little
: language.

You just brightened up my day :-)

: i cannot link to C++ with gnatlink.

Well, actually your code says:

: //file cstuff.c
: #include <stdio.h>
: void c_test()
: {
:   printf("jah!");
: }

which is not C++ but C code. So instead of:

:    pragma Import(CPP,C_Test,"c_test");

use pragma Import(C, C_Test, "c_test); and build with

gcc -c cstuff.c
gnatmake test -largs cstuff.o

BTW remember that gcc is just a driver program, and the '.c'
extension will make it use the C, not the C++ compiler. The
same goes for g++.

BTW2 remember that there might already be a program called
'test' in your path.

To try out linking to C++ use a 

// file cppstuff.cc
#include <iostream.h>
void cppstuff()
{
   cout << "jah!" << endl;
}

with the CPP import. And do not forget to link in libstdc++.

Jerry.

-- 
-- Jerry van Dijk  | email: jdijk@acm.org
-- Leiden, Holland | member Team-Ada




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: linking with C++
  1998-04-20  0:00 linking with C++ Martin Klang
  1998-04-21  0:00 ` Robert Dewar
@ 1998-04-21  0:00 ` Justin Braach
  1998-04-21  0:00 ` Jerry van Dijk
  1998-04-21  0:00 ` Robert Dewar
  3 siblings, 0 replies; 6+ messages in thread
From: Justin Braach @ 1998-04-21  0:00 UTC (permalink / raw)



Martin Klang wrote:
> 
> lately i've been trying out ADA, and even though it's not by a long way as
> esthetically pleasing as C++, i must confess it's a pretty useful little
> language.
> 
Heh... little... Maybe for an elephant.

<...>
>
> i cannot link to C++ with gnatlink.
> 
<...>
> 
> it seems to have no importance in this example whether i use pragma C or CPP
> (what is the difference anyhow?)
> 
> comparing c.o and cc.o yields that g++ somewhere along the line renamned the
> c_test-function to c_test__Fv. Why???

That has to do with C++'s wonderful name mangling stuff.  Don't ask,
'cause I
don't know.  Anyhow if you look in any Standard C header file, (not
C++), you 
_should_ see at the beginning somewhere a:

#ifdef __cplusplus 
extern "C" {
#endif

or something like it.  That funny little "extern "C"" forces the
compiler not
to screw up the names of functions and variables.  Anyhow, at the
beginnings
of any function prototypes, put that phrase, such as:

extern "C" void foo(void);

This way, you can import the function into Ada, or even C if it's being
compiled
as a separate object.  There is, (I believe), a "C++" version of that
phrase that
should work with the CPP Ada pragma, but I'm not sure, (never tried).  I
had much
fun linking Ada and C/C++ together recently, so I know how good a time
you are 
having.  I won't go on about my thoughts concerning the different
languages, so...
The GNAT manual also describes how to compile Ada modules for C/C++ main
functions.
You might wanna look at that too.

	Hope this helps,
	Justin

-- 
"Never solve puzzles that open the gates of Hell..."

fxhunter@cs.montana.edu
http://www.cs.montana.edu/~fxhunter




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [GNAT] Extending a C++ class
  1998-04-21  0:00 ` Robert Dewar
@ 1998-04-23  0:00   ` Jacob Sparre Andersen
  0 siblings, 0 replies; 6+ messages in thread
From: Jacob Sparre Andersen @ 1998-04-23  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1216 bytes --]


Robert Dewar (dewar@merv.cs.nyu.edu) wrote about the C++ interfacing pragmas:

: But you really have to know what you are doing to use them. Generally they
: are intended for use by a binding generator, not by humans.

In the GNAT Reference Manual there are references to "the C++ binding
generator tool". Where can I find/buy that tool?

: The easiest thing for most people is to write a C wrapper for their C++
: code and to interface to the C wrapper. This can be done using only
: standard features of the language as described in annex B of the RM.

This seems a bit silly if you want to extend a C++ class in Ada. - Is this
suggestion just intended for old fashioned procedural code, or can you
explain how you would do it with a C++ class?


Greetings,

Jacob

----------------------------------------------------------------------------
--  Jacob Sparre Andersen     --  E-mail: Jacob.Sparre.Andersen@risoe.dk  --
--  National Laboratory Ris�  --  Phone.: (+45) 46 77 51 23               --
--  Systems Analysis          --  Fax...: (+45) 46 77 51 99               --
----------------------------------------------------------------------------

                  Have you played with your LEGO today?




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1998-04-23  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-04-20  0:00 linking with C++ Martin Klang
1998-04-21  0:00 ` Robert Dewar
1998-04-21  0:00 ` Justin Braach
1998-04-21  0:00 ` Jerry van Dijk
1998-04-21  0:00 ` Robert Dewar
1998-04-23  0:00   ` [GNAT] Extending a C++ class Jacob Sparre Andersen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox