0

I'm trying to implement calling octave from C, as per the example shown in this answer Call GNU Octave functions in C?

I'm trying this in Nebeans on Linux.

I've created the files calloctave.cc, calloctave.h, main.c and myfunction.m as per the example. Pointed the links and includes to the correct places (in my case /usr/include/octave-5.2.0/octave/ and /usr/lib64/octave/5.2.0 ). I've chosen C++11 as the standard. In the code, there are no errors highlighted and it seems to find everything it needs, and nothing is highlighted as missing.

When I try to compile it, I just get a series of errors as follows....

cd '/home/arwel/NetBeansProjects/callOctave_new'
/bin/gmake -f Makefile CONF=Debug
"/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/arwel/NetBeansProjects/callOctave_new'
"/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/libcallOctave_new.so
gmake[2]: Entering directory `/home/arwel/NetBeansProjects/callOctave_new'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/calloctave.o.d"
g++    -c -g -I/usr/include/octave-5.2.0/octave -include /usr/include/octave-5.2.0/octave/mex.h -std=c++11 -fPIC  -MMD -MP -MF "build/Debug/GNU-Linux/calloctave.o.d" -o build/Debug/GNU-Linux/calloctave.o calloctave.cc
In file included from /usr/include/octave-5.2.0/octave/Cell.h:33:0,
                 from /usr/include/octave-5.2.0/octave/gtk-manager.h:32,
                 from /usr/include/octave-5.2.0/octave/interpreter.h:36,
                 from calloctave.cc:7:
/usr/include/octave-5.2.0/octave/ov.h:52:7: error: using typedef-name ‘mxArray’ after ‘class’
 class mxArray;
       ^
In file included from <command-line>:0:0:
/usr/include/octave-5.2.0/octave/mex.h:55:14: note: ‘mxArray’ has a previous declaration here
 typedef void mxArray;
              ^
In file included from /usr/include/octave-5.2.0/octave/ov.h:62:0,
                 from /usr/include/octave-5.2.0/octave/Cell.h:33,
                 from /usr/include/octave-5.2.0/octave/gtk-manager.h:32,
                 from /usr/include/octave-5.2.0/octave/interpreter.h:36,
                 from calloctave.cc:7:
/usr/include/octave-5.2.0/octave/ov-base.h:57:7: error: using typedef-name ‘mxArray’ after ‘class’
 class mxArray;
       ^
In file included from <command-line>:0:0:
/usr/include/octave-5.2.0/octave/mex.h:55:14: note: ‘mxArray’ has a previous declaration here
 typedef void mxArray;
              ^
calloctave.cc: In function ‘int mexCallOctave(int, mxArray**, int, mxArray**, const char*)’:
calloctave.cc:26:15: error: ‘mxArray’ is not a class, namespace, or enumeration
     args(i) = mxArray::as_octave_value (argin[i]);
               ^
calloctave.cc:42:41: error: invalid use of ‘mxArray {aka void}’
       argout[i] = new mxArray (retval(i));
                                         ^
calloctave.cc: In function ‘void free_arg_list(int, mxArray**)’:
calloctave.cc:56:29: warning: deleting ‘mxArray* {aka void*}’ is undefined [enabled by default]
             delete arglist[i];
                             ^
gmake[2]: *** [build/Debug/GNU-Linux/calloctave.o] Error 1
gmake[2]: Leaving directory `/home/arwel/NetBeansProjects/callOctave_new'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/arwel/NetBeansProjects/callOctave_new'
gmake: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)

It seems to be not understanding the mex interface in some way. I'm not very knowledgeable about C/C++, so am at a loss as to how to proceed here. What could be causing these errors and how might I resolve them?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • I'm not an expert either, but the errors seem to suggest that mixing the mex and oct interfaces isn't compatible, and the "interpreter.h" header for calling built-in octave functions from standalone files, is only compatible with the oct interface, otherwise it will complain of attempts to redefine types. Read the first part of the [Mex Interface](https://octave.org/doc/v5.2.0/Mex_002dFiles.html#Mex_002dFiles) in the manual, it mentions more information on this mxArray behaviour. My understanding is that the mex interface is mostly for matlab-compatible compiled functions, not standalones. – Tasos Papastylianou Oct 28 '20 at 10:51
  • It seems that you didn't follow the linked answer. You first need to get the correct compiler options. For it you need to compile a hello world program using `mkoctfile` with --verbose as explained in the linked answer. It prints the required compiler options that you can use to compile your project. As you have used the option `-include /usr/include/octave-5.2.0/octave/mex.h` that is invalid in this case. One thing that I should add is that after the compilation of `calloctave.o` and when you want to link `main.o` by `gcc` use `-lstdc++` at the end of compiler options. – rahnema1 Oct 28 '20 at 12:33
  • Moreover I think mex c interface is nearly painful and I would use c++ .oct API. It is simpler and faster. The linked answer can be used in rare cases that someone is forced to use c mex interface. – rahnema1 Oct 28 '20 at 12:45
  • What is Nebeans? Do you mean NetBeans? That’s for Java, what does it have to do with C? You need a C compiler. And why do you use the C++11 standard of you’re writing C code? C and C++ are different languages. – Cris Luengo Oct 28 '20 at 14:11
  • Cris Luengo - Yes Netbeans - sorry for the typo. But NB has plugins for a range of languages, just like Eclipse - I just prefer it to Eclipse, that's all https://netbeans.org/features/cpp/ . Also, if you look at the original example, the interface to Octave is C++, but this is about a C wrapper for that. – user14506090 Oct 28 '20 at 17:31
  • rahnema1 - ' ...mkoctfile with --verbose as explained in the linked answer. It prints the required compiler options that you can use to compile your project..' - Good idea - I'll try that, thanks. – user14506090 Oct 28 '20 at 19:08

0 Answers0