Ok, what the guide and stackoverflow answer give is that you can create a standalone application using C++ that has access to Octave libraries and functions, because by itself octave isn’t a language that can compile it’s code to create a executable program, because using octave what you create are scripts that are interpreted.
then it gives you the example:
std::cout << "Hello Octave world!\n";
int n = 2;
Matrix a_matrix = Matrix (n, n);
for (octave_idx_type i = 0; i < n; i++)
for (octave_idx_type j = 0; j < n; j++)
a_matrix(i,j) = (i + 1) * 10 + (j + 1);
std::cout << a_matrix;
all of that was written in C++.
is a part where the C++ program specifies that will be using a Octave header file called
oct.h that will allow the C++ program to call the functions and libraries from Octave, then the standalone file is created by running a command to compile in the command line or terminal called
mkoctfile, everything after
mkoctfile are “the instructions to create” the application called “standalone”:
$ mkoctfile --link-stand-alone standalone.cc -o standalone
Then they test the standlone application by “opening it” in the terminal:
The program works because the terminal showed:
Hello Octave world!
Then the program ends and terminal is available to write commands again:
but this “standalone application” needs octave installed and only offers access to its Math libraries, because as the guide says it this only dynamically links the octave libraries to the C++ program.
It does not, however, allow the script files, oct-files, or built-in functions of Octave to be used by the application. **To do that, the Octave interpreter needs to be initialized first.** An example of how to do this can then be seen in the code.
Then it procceds to show you how you write a C++ program that includes other important octave files and intializes the octave interpreter to work with it:
// Create interpreter.
// Inhibit reading history file by calling
// interpreter.initialize_history (false);
// Set custom load path here if you wish by calling
// interpreter.initialize_load_path (false);
// Perform final initialization of interpreter, including
// executing commands from startup files by calling
// interpreter.initialize ();
// if (! interpreter.initialized ())
// std::cerr << "Octave interpreter initialization failed!"
// << std::endl;
// exit (status);
// You may skip this step if you don't need to do anything
// between reading the startup files and telling the interpreter
// that you are ready to execute commands.
// Tell the interpreter that we're ready to execute commands:
int status = interpreter.execute ();
if (status != 0)
std::cerr << "creating embedded Octave interpreter failed!"
octave_idx_type n = 2;
for (octave_idx_type i = 0; i < n; i++)
in(i) = octave_value (5 * (i + 2));
octave_value_list out = octave::feval ("gcd", in, 1);
if (out.length () > 0)
std::cout << "GCD of ["
<< in(0).int_value ()
<< ", "
<< in(1).int_value ()
<< "] is " << out(0).int_value ()
std::cout << "invalid\n";
catch (const octave::exit_exception& ex)
std::cerr << "Octave interpreter exited with status = "
<< ex.exit_status () << std::endl;
catch (const octave::execution_exception&)
std::cerr << "error encountered in Octave evaluator!" << std::endl;
// Shutdown the interpreter which cleanly releases all memory.
Again it creates the program with
mkoctfile and tests it by running the program name
embedded in the terminal:
$ mkoctfile --link-stand-alone embedded.cc -o embedded
GCD of [10, 15] is 5
Since i have never try it, i’m not sure about the stackoverflow answer, but it seems logical in it’s reasoning:
Step 1: Create a stand-alone executable, with C++ as the guide explained it, specially the
embedded program part
Step 2: Create a distribution folder
You will need to create a copy of many of the Octave files. I suggest a directory specifically for this. At a minimum, you will need a copy of all or most of the DLLs in \bin. Additionally, place your distributable executable in this directory.
- Step 3: Other files whack-a-mole
You will now need to find out what other files will be necessary to run your .m script. You can simplify this step by copying \oct\i686-pc-mingw32*.oct and \share\octave\3.2.4\m**.m to the distribution directory, although this will be overkill, and will not actually prevent the whack-a-mole step.
That is, what he is doing is copying the octave files in the octave directory to a folder where the main executable of his standalone application would be.
It certainly would be better if the creation of standalone programs could be made simpler and automated, i’ve looked and there were some proposals years ago to make translators/compilers to take the octave source and convert it to C++ code and then compile to make executables, it would certainly be great since by itself octave offers, clean simple solutions for scripting numerical and linear algebra calculations.