comp.lang.ada
 help / color / mirror / Atom feed
* Problem passing In-Out parameter from C++ to Ada
@ 2005-06-24 18:02 John Arnsparger
  2005-06-24 20:28 ` REH
  2005-06-25  6:03 ` Jeffrey Carter
  0 siblings, 2 replies; 3+ messages in thread
From: John Arnsparger @ 2005-06-24 18:02 UTC (permalink / raw)
  To: comp.lang.ada

Hi all,

I'm looking for some help with interfacing Ada with C++.  The GNAT
User's Guide includes "A Simple Example", which I have built and it
works except for the part in which Ada_Method2 uses the "this" class
pointer, passed as an in out parameter,  to write a value back to C++
class data member.  That program runs, but never sets the value of the
data member "a_value" to 3030 as it should.

I tried an even simpler example (see below) in which all I do is pass an
integer from C++ to Ada as an in out parameter, and I get segmentation
fault/core dump.

I have seen identical behavior on two gcc/gnat installations: one is a
Redhat Linux PC version 3.2.3 on a 64-bit Xeon PC and the other is using
Cygwin gcc 3.4.4 installed yesterday on my Pentium PC via Cygwin.com.
Does anyone know if this should work, or if it does in some other
environment? Or is there another approach that is required?
Any other info or advice would be greatly appreciated.

Regards,

John Arnsparger


Here is the code I'm using:

//cpp_main.C
#include <stdio.h>
extern "C" {
   void adainit (void);
   void adafinal (void);
   void var_test (int v);
}
int main ()
{
   adainit ();
   int v1 = 3030;
   printf ("In cpp_main, v1 = %d \n", v1);
   var_test (v1);
   printf ("In cpp_main, after var_test call, v1 = %d \n", v1);
   adafinal ();
}

-- simple_cpp_interface.ads
package Simple_Cpp_Interface is
   procedure Var_Test (V : in out Integer);
   pragma Export (C, Var_Test, "var_test");
end Simple_Cpp_Interface;

-- simple_cpp_interface.adb
with TEXT_IO;
use  TEXT_IO;
package body Simple_Cpp_Interface is

   package IO is new TEXT_IO.INTEGER_IO(Integer);

   procedure Var_Test (V : in out Integer) is
   V1 : Integer := V;
   begin
      Put ("In Var_Test, V1= "); IO.Put ( V1 ); New_Line;
   end Var_Test;

end Simple_Cpp_Interface;

-- Building it with the following commands:

g++ -c cpp_main.C
g++ -c simple_cpp_interface.adb
gnatbind -n simple_cpp_interface
gnatlink simple_cpp_interface -o cpp_main --LINK=g++ -lstdc++ cpp_main.o
cpp_main
In cpp_main, v1 = 3030
Segmentation fault





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

* Re: Problem passing In-Out parameter from C++ to Ada
  2005-06-24 18:02 Problem passing In-Out parameter from C++ to Ada John Arnsparger
@ 2005-06-24 20:28 ` REH
  2005-06-25  6:03 ` Jeffrey Carter
  1 sibling, 0 replies; 3+ messages in thread
From: REH @ 2005-06-24 20:28 UTC (permalink / raw)



"John Arnsparger" <johna42@houston.rr.com> wrote in message
news:mailman.93.1119636206.17633.comp.lang.ada@ada-france.org...
> Here is the code I'm using:
>
> //cpp_main.C
> #include <stdio.h>
> extern "C" {
>    void adainit (void);
>    void adafinal (void);
>    void var_test (int v);

try changing the above to:

void var_test(int* v);

> }
> int main ()
> {
>    adainit ();
>    int v1 = 3030;
>    printf ("In cpp_main, v1 = %d \n", v1);
>    var_test (v1);

var_test(&v1);

>    printf ("In cpp_main, after var_test call, v1 = %d \n", v1);
>    adafinal ();
> }
>
> -- simple_cpp_interface.ads
> package Simple_Cpp_Interface is
>    procedure Var_Test (V : in out Integer);
>    pragma Export (C, Var_Test, "var_test");
> end Simple_Cpp_Interface;
>
> -- simple_cpp_interface.adb
> with TEXT_IO;
> use  TEXT_IO;
> package body Simple_Cpp_Interface is
>
>    package IO is new TEXT_IO.INTEGER_IO(Integer);
>
>    procedure Var_Test (V : in out Integer) is
>    V1 : Integer := V;
>    begin
>       Put ("In Var_Test, V1= "); IO.Put ( V1 ); New_Line;
>    end Var_Test;
>
> end Simple_Cpp_Interface;
>
> -- Building it with the following commands:
>
> g++ -c cpp_main.C
> g++ -c simple_cpp_interface.adb
> gnatbind -n simple_cpp_interface
> gnatlink simple_cpp_interface -o cpp_main --LINK=g++ -lstdc++ cpp_main.o
> cpp_main
> In cpp_main, v1 = 3030
> Segmentation fault
>
>





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

* Re: Problem passing In-Out parameter from C++ to Ada
  2005-06-24 18:02 Problem passing In-Out parameter from C++ to Ada John Arnsparger
  2005-06-24 20:28 ` REH
@ 2005-06-25  6:03 ` Jeffrey Carter
  1 sibling, 0 replies; 3+ messages in thread
From: Jeffrey Carter @ 2005-06-25  6:03 UTC (permalink / raw)


John Arnsparger wrote:

I think I've found your problem. It's called C++ :)

>    void var_test (int v);

"int v" does not correspond to "V : in out Integer". It doesn't even 
correspond to "V : in out Interfaces.C.Int".

-- 
Jeff Carter
"No one is to stone anyone until I blow this whistle,
do you understand? Even--and I want to make this
absolutely clear--even if they do say, 'Jehovah.'"
Monty Python's Life of Brian
74



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

end of thread, other threads:[~2005-06-25  6:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-24 18:02 Problem passing In-Out parameter from C++ to Ada John Arnsparger
2005-06-24 20:28 ` REH
2005-06-25  6:03 ` Jeffrey Carter

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