help-octave
[Top][All Lists]
Advanced

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

Info on calling C from octave


From: John W. Eaton
Subject: Info on calling C from octave
Date: Wed, 19 Sep 2007 13:12:21 -0400

On 19-Sep-2007, James Haefner wrote:

| Hi, I'm new to calling C (not C++) functions from octave and need some 
| basic help:
| 
| (1) Is there a good tutorial that describes the uses/differences between
| mex and mkoctfile and the general strategy for calling C functions in 
| external libraries?

Write a C++ wrapper the way you normally do with DEFUN_DLD and call
you C functions from it.

| (2) In the mean time, can anyone explain these results from octave_2.9.6 
| on a linux (ubuntu 6.10) machine:
| 
| The following C++ file (hello2oct.cc) works correctly when operated on 
| by mkoctfile on the system command line:  "mkoctfile hello2oct.cc"
| #include <octave/oct.h>
| DEFUN_DLD (hello2oct, args, nargout,
|         "Hello World Help String")
|       {
|         int nargin = args.length ();
|         octave_stdout << "Hello World has " << nargin
|               << " input arguments and "
|               << nargout << " output arguments.\n";
|         return octave_value_list ();
|       }
| 
| The following C file (foobar.c) fails ("mkoctfile foobar.c") with the 
| appended error messages:
| #include <octave/oct.h>
| DEFUN_DLD (foobar, args, nargout,
|         "Hello World Help String")
|       {
|         return octave_value_list ();
|       }
| 
| (same result if I remove the return line).
| 
| Errors:
| In file included from /usr/include/octave-2.9.6/octave/mx-base.h:29,
|                   from /usr/include/octave-2.9.6/octave/Matrix.h:31,
|                   from /usr/include/octave-2.9.6/octave/oct.h:34,
|                   from foobar.c:1:
| /usr/include/octave-2.9.6/octave/MatrixType.h:26: error: expected '=', 
| ',', ';', 'asm' or '__attribute__' before 'Matrix'
| /usr/include/octave-2.9.6/octave/MatrixType.h:27: error: expected '=', 
| ',', ';', 'asm' or '__attribute__' before 'ComplexMatrix'
| /usr/include/octave-2.9.6/octave/MatrixType.h:28: error: expected '=', 
| ',', ';', 'asm' or '__attribute__' before 'SparseMatrix'
| /usr/include/octave-2.9.6/octave/MatrixType.h:29: error: expected '=', 
| ',', ';', 'asm' or '__attribute__' before 'SparseComplexMatrix'
| /
| ... followed by many similar lines
| 
| Obviously, I'm missing some fundamental concepts here, so any help will 
| be appreciated.

All files that use DEFUN_DLD must be compiled by a C++ compiler.  But
from there, you can call code written in C++, C, Fortran, or any other
language that can be called from C++.

jwe


reply via email to

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