Problem with basics of embedding the Octave interpreter in C++

I want to embed the octave interpreter in C++, so I’m looking at the example’s shown here… (I should point out that I have very little experience with C++…)

I’m trying to do this on Mac OSX using Eclipse.

Initially, I’ve just set up a Hello World, to see if I can actually link to the Octave libraries as follows:

#include <iostream>
#include <cstdlib>
#include "/usr/local/Cellar/octave/5.2.0_10/include/octave-5.2.0/octave/oct.h"
#include "/usr/local/Cellar/octave/5.2.0_10/include/octave-5.2.0/octave/octave.h"
#include "/usr/local/Cellar/octave/5.2.0_10/include/octave-5.2.0/octave/parse.h"
//#include "/usr/local/Cellar/octave/5.2.0_10/include/octave-5.2.0/octave/interpreter.h"

using namespace std;

int main() {
	std::cout << "!!!Hello World!!!";  // prints !!!Hello World!!!
	return 0;

… so not doing anything with Octave yet.

Everything compiles fine if I just include oct.h and octave.h, but as soon as I try to include parse.h, the build fails as follows…

10:21:16 **** Incremental Build of configuration Debug for project octaveTest_new ****
make all 
Building file: ../src/octaveTest_new.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/Cellar/octave/5.2.0_7/include/octave-5.2.0/octave/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/octaveTest_new.d" -MT"src/octaveTest_new.o" -o "src/octaveTest_new.o" "../src/octaveTest_new.cpp"
Finished building: ../src/octaveTest_new.cpp
Building target: octaveTest_new
Invoking: MacOS X C++ Linker
g++  -o "octaveTest_new"  ./src/octaveTest_new.o   
Undefined symbols for architecture x86_64:
  "error(char const*, ...)", referenced from:
      cdef_object_rep::err_invalid_object(char const*) const in octaveTest_new.o
      cdef_class::cdef_class(cdef_object const&) in octaveTest_new.o
  "dim_vector::nil_rep()", referenced from:
      dim_vector::dim_vector() in octaveTest_new.o
  "cdef_object_rep::release(cdef_object const&)", referenced from:
      cdef_object::~cdef_object() in octaveTest_new.o
      cdef_object::operator=(cdef_object const&) in octaveTest_new.o
  "Array<cdef_object>::nil_rep()", referenced from:
      Array<cdef_object>::Array() in octaveTest_new.o
  "cdef_object_rep::map_keys() const", referenced from:
      vtable for cdef_object_rep in octaveTest_new.o
      vtable for cdef_object_base in octaveTest_new.o
  "Array<cdef_object>::resize_fill_value() const", referenced from:
      vtable for Array<cdef_object> in octaveTest_new.o
  "typeinfo for handle_cdef_object", referenced from:
      typeinfo for cdef_meta_object_rep in octaveTest_new.o
  "typeinfo for cdef_class::cdef_class_rep", referenced from:
      cdef_class::get_rep() const in octaveTest_new.o
      cdef_class::get_rep() in octaveTest_new.o
  "vtable for cdef_object_array", referenced from:
      cdef_object_array::cdef_object_array() in octaveTest_new.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [octaveTest_new] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

10:21:19 Build Failed. 2 errors, 0 warnings. (took 2s.801ms)

Now, I suspect this is a result of my inexperience with C++ / Eclipse, but could someone maybe point me in the right direction as to what might be wrong?

Some points that might help to get you started:

  • You already tell the compiler to look for include files with -I/usr/local/Cellar/octave/5.2.0_7/include/octave-5.2.0/octave. So don’t include the Octave headers with absolute paths. But use just #include "oct.h" (and similar for the other #includes).
  • You didn’t link to the Octave libraries. Try adding -loctave -loctinterp to the compiler/linker flags. If the libraries aren’t found (which is likely), pass the path to these libraries with -L flags. That might be -L/usr/local/Cellar/octave/5.2.0_7/lib (or wherever homebrew installs these libraries).

Something like this should work if you were on Linux. Linking to libraries might work differently on MacOS.