help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Persistent dara across .oct-files


From: Carlo De Falco
Subject: Re: Persistent dara across .oct-files
Date: Fri, 10 Aug 2018 14:39:28 +0000


> On 10 Aug 2018, at 10:54, Felix Wolf <address@hidden> wrote:
> 
> Hello everyone,
> 
> my colleague and I are working on a larger academic C++ code, which we
> want to equip with octave wrappers such that it can be used by students.
> For multiple reasons, we cannot port certain data structures to octave,
> and we cannot pass certain data as copy between functions.
> 
> We found out, that under Ubuntu (bionic, Octave 4.2.2), this behavior
> can be achieved via global variables in .oct-files, as can be seen from
> the following minimal example:
> 
> init.cpp:
> 
> - - - - -
> 
> #include <octave/oct.h>
> 
> double my_const;
> 
> DEFUN_DLD(init, args, nargout, "init") {
>     octave_stdout << &my_const << "\n";
>     my_const = 42;
>     octave_stdout << my_const << "\n";
>   return octave_value_list();
> }
> 
> extract.cpp:
> 
> - - - - -
> 
> #include <octave/oct.h>
> 
> extern double my_const;
> 
> DEFUN_DLD(extract, args, nargout, "extract") {
>     octave_stdout << &my_const << "\n";
>     octave_stdout << my_const << "\n";
>     return octave_value(1);
> }
> 
> - - - - -
> 
> These functions compile and run by calling
> 
> mkoctfile -std=c++11 init.cpp
> mkoctfile -std=c++11 extract.cpp
> init()
> extract()
> 
> and, when called, return the same value and the same adress, for example
> 
> 0x7f5b429c40c8
> 42
> 0x7f5b429c40c8
> 42
> 
> However, these files compile neither under Mac nor Windows. On Windows,
> one cannot compile and run them as mentioned above, but by calling an
> .m-file of the form
> 
> mkoctfile init.cpp
> mkoctfile extract.cpp init.cpp
> init()
> extract()
> 
> but they do not yield the desired output. This brings me to the two
> underlying questions we have.
> 
> 1. Is the behavior of the example above under Ubuntu intended or a bug,
> i.e., can we depend on it to be reproducable in upcoming versions?
> 2. Is there an elegant solution to make certain data structures persist
> in memory across multiple .oct-files (for example, by passing a
> reference/pointer to octave)?
> 
> Thank you for your time and assistance,
> 
> Felix Wolf



Hi Felix,

This behavious is not specific to Octave but related to how dynamic linking 
differs among platforms.
A workaround to make this working on macos is the following : 

create a file named PKG_ADD with the following contents:

------
autoload init bundle.oct
autoload extract bundle.oct
------

then build your code with this command

------
mkoctfile init.cc extract.cc -o bundle.oct
------


then in Octave you get (tested with the development branch)

>> init
0x1149c0290
42
>> extract
0x1149c0290
42
ans =  1


HTH,
c.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]