[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Fwd: C program and mkoctfile problem
From: |
Cecconello |
Subject: |
Re: Fwd: C program and mkoctfile problem |
Date: |
Wed, 22 May 2019 09:45:39 -0500 (CDT) |
The C++ wrapper with some comments
/* Example on how to call a C function from Octave using mkoctfile and a C++
wrapper.
In this example, the C function is defined in the file: simple.c
The function "simple" takes two inputs of type int and returns their sum.
The C++ wrapper is defined in the file: simple_wrap.cc
The function and its wrapper are compiled by issuing the command:
mkoctfile -o simple.oct simple_wrap.cpp simple.c
Once compiled, the function is called from within Octave as any other
function:
octave:19> [a, b, c] = simple(5,15);
The function has 2 input arguments and 3 output arguments.
Input a = 5
Input b = 15
The sum is 20
Furhter calls to the function "simple" from within Octave after the
source codes have been recompiled
must be proceed by the clear instructions as shown in the example below:
octave:20> [a, b, c] = simple(5,15);
warning: library /home/marco/Documents/C Programming Tests/simple.oct
not reloaded due to existing references
octave:21> clear simple
octave:22> [a, b, c] = simple(5,15);
The function has 2 input arguments and 3 output arguments.
Input a = 5
Input b = 15
The sum is 20
*/
// C++ header necessary for the mkoctfile
#include <octave/oct.h>
// Declare the C funtion "simple"
extern "C" int simple (int a, int b);
/* The macro DEFUN_DLD creates a dynamically loaded function where:
simple is the name of the C function
args contains the input arguments of the C function
nargout is the number of output arguments of the C function
string the text that will displayed when issuing the command "help
simple" from within Octave
*/
DEFUN_DLD (simple, args, nargout, "Simple test of calling a C function from
Octave with a C++ wrapper")
{
// Number of input arguments of the C function
int nargin = args.length();
// Declare the array "retval" which contains the return values of the C++
wrapper function
octave_value_list retval;
// Declares the variables to which the input arguments to the C function
are passed to
int a, b;
// Declares the return variable of the C function
int c;
// Assign the values to the variables
a = args(0).int_value();
b = args(1).int_value();
// Call the C function and assing the returned variable to c
c = simple(a, b);
// Some printout:
octave_stdout << "The function has " << args.length () << " input
arguments and " << nargout << " output arguments.\n";
printf("Input a = %d\n", a);
printf("Input b = %d\n", b);
printf("The sum is %d\n", c);
// Assign the C++ wrapper return values to retval
retval(0) = a;
retval(1) = b;
retval(2) = c;
// Return value
return retval;
}
The C function:
int simple (int a, int b)
{
// Declare the variable of type int
int c;
// Assing the sum of the function input arguments to the variable
c = a + b;
// Return the sum
return c;
}
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html