[racket] C++ Extensions and Classes

From: Jon Zeppieri (zeppieri at gmail.com)
Date: Thu Feb 6 22:20:16 EST 2014

Okay. If you want one module to include code from the other, you can
simply include it. If you're worried about multiple definitions of
scheme_initialize, scheme_reload, and scheme_module_name, why wouldn't
you just pull all of the rest of the code -- the common code -- into a
separate file and have both modules include it? I figure I still must
be missing something.

On Thu, Feb 6, 2014 at 7:38 PM, Cody Eilar <cody.eilar at gmail.com> wrote:
> Hmmm, I haven't had to wrap anything using extern C because all the function
> prototypes are in C already I believe (the three I mentioned above). The
> main idea here is that I want to reuse my extensions in the same way that my
> classes are constructed. I want to write extensions separately for each
> class that I have already written in C++. I have already successfully
> written my extension for Foo. I can load Foo_ext.rkt in my racket script and
> execute all the functions that I need. The issue is that I have another
> class, Bar, that inherits from Foo. So as it stands now, I can't figure out
> how to write my Bar extension such that I don't have to rewrite (or copy and
> paste) all the code from my Foo extension. I'd like to leverage the fact
> that I already wrote the extension for Foo and Bar is just a few extra
> variables and functions. I hope this clearly illustrates my goal.
>
> Thanks :-)
>
>
> On Thu, Feb 6, 2014 at 12:17 PM, Jon Zeppieri <zeppieri at gmail.com> wrote:
>>
>> I'm not entirely sure that I follow, but if you're writing these
>> extension functions in C++, I believe you'll need to wrap their
>> prototypes in an extern(C) { ... } block so that they'll be given
>> un-mangled names for the linker to use.
>>
>> On Thu, Feb 6, 2014 at 4:56 PM, Cody Eilar <cody.eilar at gmail.com> wrote:
>> > I understand that, the issue is that I have to provide three functions
>> > that
>> > are not static so that I can make my c++ code known to scheme. Those
>> > functions are:
>> >
>> > scheme_reload(Scheme_Env *env)
>> > scheme_initialize(Scheme_Env *env)
>> > scheme_module_name()
>> >
>> > Each one of these functions are known to the linker. Inside the
>> > Foo_ext.cpp
>> > file, I have these three functions implemented. But in order to use
>> > Bar_ext.cpp I have to implement them as well, which means I have
>> > multiple
>> > declarations to the same function. So my Bar_ext.cpp file would Ideally
>> > look
>> > something like this:
>> >
>> > Bar_ext.cpp
>> > scheme_reload(Scheme_Env *env)
>> > {
>> >  /* Bar _ext reload stuff */
>> >     Foo_ext.scheme_reload(env)
>> > }
>> >
>> > Currently, the only way I see to solve this is to use a bunch of ifdefs,
>> > but
>> > I was curious if there was a more elegant solution.
>> >
>> > Thanks!
>> >
>> > On Thu, Feb 6, 2014 at 2:48 AM, David T. Pierson <dtp at mindstory.com>
>> > wrote:
>> >>
>> >> On Wed, Feb 05, 2014 at 02:16:54PM -1000, Cody Eilar wrote:
>> >> > > But what I really want is:
>> >> > >
>> >> > > Foo_ext.h:
>> >> > >
>> >> > > func1_racket_ext() { /*... do racket stuff and run func1() */}
>> >> > >
>> >> > > /* Scheme initializes etc... */
>> >> > >
>> >> > >
>> >> > > Bar_ext.cpp:
>> >> > > #include "Foo_ext.h"
>> >> > >
>> >> > > func2_racket_ext() {/* ... do more racket stuff and run func2() /*}
>> >>
>> >> Disclaimer: I know nothing about Racket extensions.
>> >>
>> >> In C++, if you want to be able to call a function from multiple .cpp
>> >> files, you typically put the *declaration* in a header:
>> >>
>> >>   void func1_ext(void);
>> >>
>> >> The definition would still go in a .cpp file:
>> >>
>> >>   void func1_ext(void)
>> >>   {
>> >>     /*...*/
>> >>   }
>> >>
>> >> You typically only put the definition in a header if you want to make
>> >> an
>> >> inline function.  It doesn't seem like you want an inline function
>> >> here.
>> >>
>> >> Does that help?
>> >>
>> >> David
>> >
>> >
>> >
>> > ____________________
>> >   Racket Users list:
>> >   http://lists.racket-lang.org/users
>> >
>
>

Posted on the users mailing list.