comp.lang.ada
 help / color / mirror / Atom feed
* Interface Ada string with C++ std::string
@ 2018-07-03 12:59 Roger
  2018-07-03 13:13 ` Dan'l Miller
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Roger @ 2018-07-03 12:59 UTC (permalink / raw)


I am need to interface an Ada procedure to a C++ function that takes a  std::string as an input parameter.
Can someone advise me how to do this?


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

* Re: Interface Ada string with C++ std::string
  2018-07-03 12:59 Interface Ada string with C++ std::string Roger
@ 2018-07-03 13:13 ` Dan'l Miller
  2018-07-03 13:16   ` Roger
  2018-07-03 16:56 ` Lucretia
  2018-07-05 23:32 ` Roger
  2 siblings, 1 reply; 13+ messages in thread
From: Dan'l Miller @ 2018-07-03 13:13 UTC (permalink / raw)


On Tuesday, July 3, 2018 at 7:59:56 AM UTC-5, Roger wrote:
> I am need to interface an Ada procedure to a C++ function that takes a  std::string as an input parameter.
> Can someone advise me how to do this?

Is GNAT your Ada compiler?  Is GCC your C++ compiler?

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

* Re: Interface Ada string with C++ std::string
  2018-07-03 13:13 ` Dan'l Miller
@ 2018-07-03 13:16   ` Roger
  2018-07-03 16:34     ` Jacob Sparre Andersen
  0 siblings, 1 reply; 13+ messages in thread
From: Roger @ 2018-07-03 13:16 UTC (permalink / raw)


On Tuesday, July 3, 2018 at 11:13:33 PM UTC+10, Dan'l Miller wrote:
> On Tuesday, July 3, 2018 at 7:59:56 AM UTC-5, Roger wrote:
> > I am need to interface an Ada procedure to a C++ function that takes a  std::string as an input parameter.
> > Can someone advise me how to do this?
> 
> Is GNAT your Ada compiler?  Is GCC your C++ compiler?

Mac OSX 10.13.5
Xcode 9.4.1
GNAT 2018

I config GNAT to use LLVM compilers

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

* Re: Interface Ada string with C++ std::string
  2018-07-03 13:16   ` Roger
@ 2018-07-03 16:34     ` Jacob Sparre Andersen
  2018-07-03 16:52       ` Roger
  0 siblings, 1 reply; 13+ messages in thread
From: Jacob Sparre Andersen @ 2018-07-03 16:34 UTC (permalink / raw)


Roger <rogermcm2@gmail.com> writes:

> Mac OSX 10.13.5
> Xcode 9.4.1
> GNAT 2018
>
> I config GNAT to use LLVM compilers

I don't think GNAT can use LLVM as its backend.

Have you checked if the GNAT Ada and C++ ABI is compatible with the LLVM
C++ ABI?

Greetings,

Jacob
-- 
"Genes don't matter. It's all physics."

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

* Re: Interface Ada string with C++ std::string
  2018-07-03 16:34     ` Jacob Sparre Andersen
@ 2018-07-03 16:52       ` Roger
  0 siblings, 0 replies; 13+ messages in thread
From: Roger @ 2018-07-03 16:52 UTC (permalink / raw)


On Wednesday, July 4, 2018 at 2:34:53 AM UTC+10, Jacob Sparre Andersen wrote:
>
> I don't think GNAT can use LLVM as its backend.
> 
> Have you checked if the GNAT Ada and C++ ABI is compatible with the LLVM
> C++ ABI?
> 
> Greetings,
> 
> Jacob
> -- 
> "Genes don't matter. It's all physics."

gprconfig provides selection of LLVM for C and C++
Regards
Roger

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

* Re: Interface Ada string with C++ std::string
  2018-07-03 12:59 Interface Ada string with C++ std::string Roger
  2018-07-03 13:13 ` Dan'l Miller
@ 2018-07-03 16:56 ` Lucretia
  2018-07-04  3:33   ` Roger
  2018-07-05 23:32 ` Roger
  2 siblings, 1 reply; 13+ messages in thread
From: Lucretia @ 2018-07-03 16:56 UTC (permalink / raw)


On Tuesday, 3 July 2018 13:59:56 UTC+1, Roger  wrote:
> I am need to interface an Ada procedure to a C++ function that takes a  std::string as an input parameter.
> Can someone advise me how to do this?

See https://github.com/Lucretia/test_binding

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

* Re: Interface Ada string with C++ std::string
  2018-07-03 16:56 ` Lucretia
@ 2018-07-04  3:33   ` Roger
  2018-07-04  4:13     ` Roger
  2018-07-04 11:23     ` Lucretia
  0 siblings, 2 replies; 13+ messages in thread
From: Roger @ 2018-07-04  3:33 UTC (permalink / raw)


On Wednesday, July 4, 2018 at 2:56:15 AM UTC+10, Lucretia wrote:
> On Tuesday, 3 July 2018 13:59:56 UTC+1, Roger  wrote:
> > I am need to interface an Ada procedure to a C++ function that takes a  std::string as an input parameter.
> > Can someone advise me how to do this?
> 
> See https://github.com/Lucretia/test_binding

Thanks for this, it looks promising.
I have cloned test_binding, built and run it and am now using it as a basis for my own situation.


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

* Re: Interface Ada string with C++ std::string
  2018-07-04  3:33   ` Roger
@ 2018-07-04  4:13     ` Roger
  2018-07-04 11:23     ` Lucretia
  1 sibling, 0 replies; 13+ messages in thread
From: Roger @ 2018-07-04  4:13 UTC (permalink / raw)


On Wednesday, July 4, 2018 at 1:33:59 PM UTC+10, Roger wrote:
> On Wednesday, July 4, 2018 at 2:56:15 AM UTC+10, Lucretia wrote:
> > On Tuesday, 3 July 2018 13:59:56 UTC+1, Roger  wrote:
> > > I am need to interface an Ada procedure to a C++ function that takes a  std::string as an input parameter.
> > > Can someone advise me how to do this?
> > 
> > See https://github.com/Lucretia/test_binding
> 
> Thanks for this, it looks promising.
> I have cloned test_binding, built and run it and am now using it as a basis for my own situation.

Having said that; it is true for one of my Macs but not the other.
On the other linking fails with numerous errors which I have been unable to fix:
Undefined symbols for architecture x86_64:
  "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in libtest_binding.a(b_base.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in libtest_binding.a(b_base.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in libtest_binding.a(b_derived1.o)

etc.


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

* Re: Interface Ada string with C++ std::string
  2018-07-04  3:33   ` Roger
  2018-07-04  4:13     ` Roger
@ 2018-07-04 11:23     ` Lucretia
  2018-07-04 11:45       ` Roger
  1 sibling, 1 reply; 13+ messages in thread
From: Lucretia @ 2018-07-04 11:23 UTC (permalink / raw)


On Wednesday, 4 July 2018 04:33:59 UTC+1, Roger  wrote:
> On Wednesday, July 4, 2018 at 2:56:15 AM UTC+10, Lucretia wrote:
> > On Tuesday, 3 July 2018 13:59:56 UTC+1, Roger  wrote:
> > > I am need to interface an Ada procedure to a C++ function that takes a  std::string as an input parameter.
> > > Can someone advise me how to do this?
> > 
> > See https://github.com/Lucretia/test_binding
> 
> Thanks for this, it looks promising.
> I have cloned test_binding, built and run it and am now using it as a basis for my own situation.

All you probably need is a C function which returns the string c_str() to Ada which Ada can then use, wrap that in Ada. Just define a private type for the string class which is an address and pass that to the C function.

package CPP_Strings is
   type CPP_String is private;

   function Get (S : in CPP_String) return String; -- Wrap the C->Ada stuff inside here.
private
   type CPP_String is System.Address;
end CPP_Strings;

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

* Re: Interface Ada string with C++ std::string
  2018-07-04 11:23     ` Lucretia
@ 2018-07-04 11:45       ` Roger
  2018-07-04 11:52         ` Lucretia
  0 siblings, 1 reply; 13+ messages in thread
From: Roger @ 2018-07-04 11:45 UTC (permalink / raw)


On Wednesday, July 4, 2018 at 9:23:23 PM UTC+10, Lucretia wrote:

> All you probably need is a C function which returns the string c_str() to Ada which Ada can then use, wrap that in Ada. Just define a private type for the string class which is an address and pass that to the C function.
> 
> package CPP_Strings is
>    type CPP_String is private;
> 
>    function Get (S : in CPP_String) return String; -- Wrap the C->Ada stuff inside here.
> private
>    type CPP_String is System.Address;
> end CPP_Strings;

Thanks,
I was playing around today modifying your test_binding code then managed to accidentally delete everything I'd done!
I think I'll have another go at it as it is an educational experience.
Once I've done that, I'll be in a better position to understand and implement what you've just suggested.
Everything is pretty much in line with my thinking. My main problem is my minimal experience with C++ so your helping hand is very welcome.
Once I found it impossible to directly interface std::string to Ada, I thought the way ahead would be to write a C interface to C++ and then wrap the C interface to Ada as you have suggested.
My original post was due to not being sure how to do it. So your advice has been a tremendous help and makes me confident that I'll get there eventually.


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

* Re: Interface Ada string with C++ std::string
  2018-07-04 11:45       ` Roger
@ 2018-07-04 11:52         ` Lucretia
  2018-07-05  1:08           ` Roger
  0 siblings, 1 reply; 13+ messages in thread
From: Lucretia @ 2018-07-04 11:52 UTC (permalink / raw)


On Wednesday, 4 July 2018 12:45:30 UTC+1, Roger  wrote:
> On Wednesday, July 4, 2018 at 9:23:23 PM UTC+10, Lucretia wrote:
> 
> > All you probably need is a C function which returns the string c_str() to Ada which Ada can then use, wrap that in Ada. Just define a private type for the string class which is an address and pass that to the C function.
> > 
> > package CPP_Strings is
> >    type CPP_String is private;
> > 
> >    function Get (S : in CPP_String) return String; -- Wrap the C->Ada stuff inside here.
> > private
> >    type CPP_String is System.Address;
> > end CPP_Strings;
> 
> Thanks,
> I was playing around today modifying your test_binding code then managed to accidentally delete everything I'd done!
> I think I'll have another go at it as it is an educational experience.
> Once I've done that, I'll be in a better position to understand and implement what you've just suggested.
> Everything is pretty much in line with my thinking. My main problem is my minimal experience with C++ so your helping hand is very welcome.
> Once I found it impossible to directly interface std::string to Ada, I thought the way ahead would be to write a C interface to C++ and then wrap the C interface to Ada as you have suggested.
> My original post was due to not being sure how to do it. So your advice has been a tremendous help and makes me confident that I'll get there eventually.

The test_binding is the extreme way to do it.

The simpler, kind of, and preferred way would be to work out what you need from the string class. Pass around the C++ pointer inside Ada. Then have C functions which are imported to Ada which handle the messy C/C++ stuff, then wrap those in Ada handling the Messy Ada/C stuff there.

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

* Re: Interface Ada string with C++ std::string
  2018-07-04 11:52         ` Lucretia
@ 2018-07-05  1:08           ` Roger
  0 siblings, 0 replies; 13+ messages in thread
From: Roger @ 2018-07-05  1:08 UTC (permalink / raw)


On Wednesday, July 4, 2018 at 9:52:35 PM UTC+10, Lucretia wrote:

> The test_binding is the extreme way to do it.
> 
> The simpler, kind of, and preferred way would be to work out what you need from the string class. Pass around the C++ pointer inside Ada. Then have C functions which are imported to Ada which handle the messy C/C++ stuff, then wrap those in Ada handling the Messy Ada/C stuff there.

Thanks for all your advice.
It turns out that my original effort was almost there and now works.
As you advise, it just needs a C function between Ada and C++.

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

* Re: Interface Ada string with C++ std::string
  2018-07-03 12:59 Interface Ada string with C++ std::string Roger
  2018-07-03 13:13 ` Dan'l Miller
  2018-07-03 16:56 ` Lucretia
@ 2018-07-05 23:32 ` Roger
  2 siblings, 0 replies; 13+ messages in thread
From: Roger @ 2018-07-05 23:32 UTC (permalink / raw)


Just to complete the story, the solution was extremely simple, just a C++ wrapper in which a std::string constructor is used to generate a std::string from a char* string which is then used in calling the required C++ routine.


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

end of thread, other threads:[~2018-07-05 23:32 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-03 12:59 Interface Ada string with C++ std::string Roger
2018-07-03 13:13 ` Dan'l Miller
2018-07-03 13:16   ` Roger
2018-07-03 16:34     ` Jacob Sparre Andersen
2018-07-03 16:52       ` Roger
2018-07-03 16:56 ` Lucretia
2018-07-04  3:33   ` Roger
2018-07-04  4:13     ` Roger
2018-07-04 11:23     ` Lucretia
2018-07-04 11:45       ` Roger
2018-07-04 11:52         ` Lucretia
2018-07-05  1:08           ` Roger
2018-07-05 23:32 ` Roger

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