unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* good tutorial on extending a c++ project with Guile ?
@ 2017-06-26 12:45 Samuel Barreto
  2017-06-26 13:03 ` Panicz Maciej Godek
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Samuel Barreto @ 2017-06-26 12:45 UTC (permalink / raw)
  To: guile-user

Hi everyone,

I want to create a Guile extension to a big C++-based project called
Bio++ (dedicated to bioinformatics and computational biology).

However I failed to find a good and simple example on how to extend a
C++ program with Guile. My idea was to create a shared library that can
be called from Guile and embedded in a module. I followed instructions
on the Guile reference manual but all of them are related to C, not C++.
I then looked at the source code of LilyPond and OpenCog but was not
able to extract signal from software idiosyncratic noise.

So can anyone point me to a good example or a simple tutorial on how to
extend C++ with Guile ? I think the main confusion point to me is the
compilation danse between g++ and gcc.

Thank you for your help !

Samuel



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

* Re: good tutorial on extending a c++ project with Guile ?
  2017-06-26 12:45 good tutorial on extending a c++ project with Guile ? Samuel Barreto
@ 2017-06-26 13:03 ` Panicz Maciej Godek
  2017-06-26 13:08   ` Panicz Maciej Godek
  2017-06-26 16:09 ` Hans Åberg
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Panicz Maciej Godek @ 2017-06-26 13:03 UTC (permalink / raw)
  To: samuel.barreto8; +Cc: guile-user@gnu.org

2017-06-26 14:45 GMT+02:00 Samuel Barreto <samuel.barreto8@gmail.com>:

> Hi everyone,
>
> I want to create a Guile extension to a big C++-based project called
> Bio++ (dedicated to bioinformatics and computational biology).
>
> However I failed to find a good and simple example on how to extend a
> C++ program with Guile. My idea was to create a shared library that can
> be called from Guile and embedded in a module. I followed instructions
> on the Guile reference manual but all of them are related to C, not C++.
> I then looked at the source code of LilyPond and OpenCog but was not
> able to extract signal from software idiosyncratic noise.
>
> So can anyone point me to a good example or a simple tutorial on how to
> extend C++ with Guile ? I think the main confusion point to me is the
> compilation danse between g++ and gcc.
>
>
Hi Samuel,
I think that the tutorial from the Guile reference manual should be
sufficient,
as C++ is mostly a superset of C -- you only need to be careful about
making the
loadable functions binary-compatible with C, by marking them
extern "C". You can do this per function, by writing, say

extern "C" void init_bessel();

or per module, as in

extern "C" {
  // ...
  void init_bessel()
  // ...
}


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

* Re: good tutorial on extending a c++ project with Guile ?
  2017-06-26 13:03 ` Panicz Maciej Godek
@ 2017-06-26 13:08   ` Panicz Maciej Godek
  0 siblings, 0 replies; 6+ messages in thread
From: Panicz Maciej Godek @ 2017-06-26 13:08 UTC (permalink / raw)
  To: samuel.barreto8; +Cc: guile-user@gnu.org

2017-06-26 15:03 GMT+02:00 Panicz Maciej Godek <godek.maciek@gmail.com>:

>
>
> 2017-06-26 14:45 GMT+02:00 Samuel Barreto <samuel.barreto8@gmail.com>:
>
>> Hi everyone,
>>
>> I want to create a Guile extension to a big C++-based project called
>> Bio++ (dedicated to bioinformatics and computational biology).
>>
>> However I failed to find a good and simple example on how to extend a
>> C++ program with Guile. My idea was to create a shared library that can
>> be called from Guile and embedded in a module. I followed instructions
>> on the Guile reference manual but all of them are related to C, not C++.
>> I then looked at the source code of LilyPond and OpenCog but was not
>> able to extract signal from software idiosyncratic noise.
>>
>> So can anyone point me to a good example or a simple tutorial on how to
>> extend C++ with Guile ? I think the main confusion point to me is the
>> compilation danse between g++ and gcc.
>>
>>
> Hi Samuel,
> I think that the tutorial from the Guile reference manual should be
> sufficient,
> as C++ is mostly a superset of C -- you only need to be careful about
> making the
> loadable functions binary-compatible with C, by marking them
> extern "C". You can do this per function, by writing, say
>
> extern "C" void init_bessel();
>
> or per module, as in
>
> extern "C" {
>   // ...
>   void init_bessel()
>   // ...
> }
>
>
> I also have a working example of a library made to cooperate with Guile
using C++ here
(these are some bindings to the Open Dynamics Engine; I wrote them in C++
because this
way I could use some of the STL collections)

https://bitbucket.org/panicz/slayer/src/26a8b3ff05ad9d34a98a636d771e3875496f2d69/demos/scum/physics/?at=default


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

* Re: good tutorial on extending a c++ project with Guile ?
  2017-06-26 12:45 good tutorial on extending a c++ project with Guile ? Samuel Barreto
  2017-06-26 13:03 ` Panicz Maciej Godek
@ 2017-06-26 16:09 ` Hans Åberg
  2017-06-26 16:54 ` David Kastrup
  2017-06-26 18:42 ` Chris Vine
  3 siblings, 0 replies; 6+ messages in thread
From: Hans Åberg @ 2017-06-26 16:09 UTC (permalink / raw)
  To: samuel.barreto8; +Cc: guile-user


> On 26 Jun 2017, at 14:45, Samuel Barreto <samuel.barreto8@gmail.com> wrote:

> So can anyone point me to a good example or a simple tutorial on how to
> extend C++ with Guile ?

Long ago, I wrote [1], which includes a parser generated with Flex/Bison, and examples how to convert between Guile and C++ exceptions. According to discussions here before, it is bad idea passing C++ exceptions through C code.

One difference is that in general there is no way one can guarantee in a C++ interface that an integer staying the same when passed through Guile, as Scheme does not have a static integer and rational number types, but a single number type. So there need to be checks there.

> I think the main confusion point to me is the
> compilation [dance] between g++ and gcc.

C++ names are mangled, and 'extern "C"' drops that, so that the names can be combined with C. If you compile C with g++, I think it will assume it is compile it as C++ with name mangling then.

The Makefile of [1] compiles C++ with g++, but links using gcc with the option '-lstdc++', but perhaps it works using g++, as long it involves no C file compiling.

1. https://secure2.storegate.com/Shares/Home.aspx?ShareID=56659ce2-127e-455e-ba1f-e6cbc82ba973





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

* Re: good tutorial on extending a c++ project with Guile ?
  2017-06-26 12:45 good tutorial on extending a c++ project with Guile ? Samuel Barreto
  2017-06-26 13:03 ` Panicz Maciej Godek
  2017-06-26 16:09 ` Hans Åberg
@ 2017-06-26 16:54 ` David Kastrup
  2017-06-26 18:42 ` Chris Vine
  3 siblings, 0 replies; 6+ messages in thread
From: David Kastrup @ 2017-06-26 16:54 UTC (permalink / raw)
  To: guile-user

Samuel Barreto <samuel.barreto8@gmail.com> writes:

> Hi everyone,
>
> I want to create a Guile extension to a big C++-based project called
> Bio++ (dedicated to bioinformatics and computational biology).
>
> However I failed to find a good and simple example on how to extend a
> C++ program with Guile. My idea was to create a shared library that can
> be called from Guile and embedded in a module. I followed instructions
> on the Guile reference manual but all of them are related to C, not C++.
> I then looked at the source code of LilyPond and OpenCog but was not
> able to extract signal from software idiosyncratic noise.
>
> So can anyone point me to a good example or a simple tutorial on how to
> extend C++ with Guile ? I think the main confusion point to me is the
> compilation danse between g++ and gcc.

You could take a look at LilyPond (which still uses Guile-1.8 but the
1.8/2.x difference is not really relevant to the basic approaches used
for interfacing), is written in C++ and interfaces to Guile (with Guile
being precompiled as a C library and used as system library if
available).

In particular, it might make sense to look at the files lily/smobs.cc,
lily/include/smobs.hh, lily/include/smobs.tcc,
lily/include/small-smobs.hh since LilyPond routes all its Scheme-wrapped
data structures through these constructs.

A repository viewer can be found at
<http://git.savannah.gnu.org/gitweb/?p=lilypond.git;a=tree>

-- 
David Kastrup




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

* Re: good tutorial on extending a c++ project with Guile ?
  2017-06-26 12:45 good tutorial on extending a c++ project with Guile ? Samuel Barreto
                   ` (2 preceding siblings ...)
  2017-06-26 16:54 ` David Kastrup
@ 2017-06-26 18:42 ` Chris Vine
  3 siblings, 0 replies; 6+ messages in thread
From: Chris Vine @ 2017-06-26 18:42 UTC (permalink / raw)
  To: guile-user; +Cc: samuel.barreto8

On Mon, 26 Jun 2017 14:45:06 +0200
Samuel Barreto <samuel.barreto8@gmail.com> wrote:
> Hi everyone,
> 
> I want to create a Guile extension to a big C++-based project called
> Bio++ (dedicated to bioinformatics and computational biology).
> 
> However I failed to find a good and simple example on how to extend a
> C++ program with Guile. My idea was to create a shared library that
> can be called from Guile and embedded in a module. I followed
> instructions on the Guile reference manual but all of them are
> related to C, not C++. I then looked at the source code of LilyPond
> and OpenCog but was not able to extract signal from software
> idiosyncratic noise.
> 
> So can anyone point me to a good example or a simple tutorial on how
> to extend C++ with Guile ? I think the main confusion point to me is
> the compilation danse between g++ and gcc.

Using guile with C++ is pretty much the same as using it with C except
that there is a problem: exceptions.  guile exceptions are in effect
implemented using setjmp/longjmp or its cognates.  C++ exceptions are
not.

This means that guile exceptions do not take account of the fact that
C++ objects in local scope may have user-provided (non-trivial)
destructors - if the scope of such objects is jumped out of, there is
undefined behaviour.  Furthermore C++ exception unwinding does not take
account of guile dynamic extents. 

This means you have to follow two rules:

(i) you should not allow a guile exception arising from calls to
libguile made by C++ to exit a C++ scope in which there is a local
(auto) C++ object which is not trivially destructible;

(ii) no C++ exception should transit out of a scm_dynwind_begin()/
scm_dynwind_end() pair.

Note also that C and C++ code should not store SCM objects (which are
pointers to guile scheme objects) in memory blocks allocated by
malloc() or the new expression, or they will not be seen by the garbage
collector used by libguile (which is the gc library) and therefore may
be prematurely deallocated.  To keep such items alive in custom
allocators, SCM variables should be kept as local variables or
parameter names in functions (and so stored on the stack or in
registers, where they will be seen by the garbage collector), or in
memory allocated with scm_gc_malloc(), where they will also be seen by
the garbage collector.

You can see one approach to this at
http://cxx-gtk-utils.sourceforge.net/2.2/namespaceCgu_1_1Extension.html .

Chris



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

end of thread, other threads:[~2017-06-26 18:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-26 12:45 good tutorial on extending a c++ project with Guile ? Samuel Barreto
2017-06-26 13:03 ` Panicz Maciej Godek
2017-06-26 13:08   ` Panicz Maciej Godek
2017-06-26 16:09 ` Hans Åberg
2017-06-26 16:54 ` David Kastrup
2017-06-26 18:42 ` Chris Vine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).