一、安装方法:
sudo apt-add-repository ppa:octave/stable
sudo apt-get update
sudo apt-get install octave liboctave-dev octave-symbolic
安装的时候会把openJDK同时也装上,装完后删除
sudo apt-get purge java-common ca-certificates-java default-jre-headless openjdk-8-jre-headless
二、c++中嵌入octave函数(需要安装 liboctave-dev ):
例子1:embed.cpp
#include <iostream>
#include <octave/octave.h>
int main(int argc,char* argv[])
{
int embedded;
octave_main(argc,argv,embedded=0);
return embedded;
}
编译:
mkoctfile embed.cpp --link-stand-alone -o embed
例子2:test1.cpp(生成随机数、并打印输出)
#include <octave/oct.h>
#include <octave/parse.h>
#include <octave/octave.h>
#include <iostream>
int main(int argc, char **argv)
{
// Init Octave interpreter
if (!octave_main(argc, argv, true)) {
error("Octave interpreter initialization failed");
}
// x = rand(10,1)
ColumnVector sz(2);
sz(0) = 10; sz(1) = 1;
octave_value_list in = octave_value(sz);
octave_value_list out = feval("rand", in, 1);
// print random numbers
if (!error_state && out.length () > 0) {
Matrix x( out(0).matrix_value() );
std::cout << "x = \n" << x << std::endl;
}
return 0;
}
编译运行:
$ mkoctfile test1.cpp --link-stand-alone -o test1
$ ./test1
x =
0.0247371
0.479646
0.0559764
0.987954
0.998218
0.0805802
0.943379
0.545469
0.325143
0.756517
三、octave中使用c\c++
例子:$ cat oct_demo.cc
// oct_demo.cc -- example of a dynamically linked function for Octave.
// To use this file, your version of Octave must support dynamic
// linking. To find out if it does, type the command
//
// __octave_config_info__ ("ENABLE_DYNAMIC_LINKING")
//
// at the Octave prompt. Support for dynamic linking is included if
// this expression is true.
//
// To compile this file, type the command
//
// mkoctfile oct_demo.cc
//
// from within Octave or from the shell prompt. This will create a file
// called oct_demo.oct that can be loaded by Octave. To test the
// oct_demo.oct file, start Octave and type the command
//
// oct_demo ("easy as", 1, 2, 3)
//
// at the Octave prompt. Octave should respond by printing
//
// Hello, world!
// easy as
// 1
// 2
// 3
// ans = 3
// Additional samples of real dynamically loaded functions are available in
// the files of the libinterp/dldfcn directory of the Octave distribution.
// See also the chapter External Code Interface in the documentation.
#include <iostream>
#include <octave/oct.h>
// Every user function should include <octave/oct.h> which imports the
// basic set of Octave header files required. In particular this will define
// the DEFUN_DLD macro (defun-dld.h) which is used for every user function
// that is visible to Octave.
// The four arguments to the DEFUN_DLD macro are:
// 1) The function name as seen in Octave.
// 2) The variable to hold any inputs (of type octave_value_list)
// 3) The number of output arguments
// 4) A string to use as help text if 'help <function_name>' is entered.
//
// Note below that the third parameter (nargout) of DEFUN_DLD is not used.
DEFUN_DLD (oct_demo, args, /* nargout */,
"[...] = oct_demo (...)\n\
\n\
Print a greeting followed by the values of all input arguments.\n\
\n\
Return all arguments in reverse order.")
{
// The inputs to this are available in the variable named args.
int nargin = args.length ();
// The list of values to return. See the declaration in ovl.h.
octave_value_list retval;
// This stream is normally connected to the pager.
octave_stdout << "Hello, world!\n";
// The octave_value_list class is a zero-based array of octave_value objects.
// The declaration for the octave_value class is in the file ov.h.
// The print() method will send its output to octave_stdout,
// so it will also end up going through the pager.
for (int i = 0; i < nargin; i++)
{
octave_value tmp = args(i);
tmp.print (octave_stdout);
retval(nargin-i-1) = tmp;
}
return retval;
}
在octave命令行中:
>> mkoctfile oct_demo.cc
>> oct_demo ("easy as", 1, 2, 3)
Hello, world!
easy as
1
2
3
ans = 3
>>