Using Octave in C++

1.6k views Asked by At

I am new in both Octave and C++ ! I want to use Octave in C++ and found this webpage. I started with the upper code and tried to run it. Unfortunately there is no document on this matter and I encountered with a lot of errors. I first used msvc 2010 and found that the I should use gcc!! Here are what I'v done so far:

  1. Download Octave from this link, (I download octave-4.0.0_0.zip)
  2. I tried to compile this code:

    int main (void)
    {
      std::cout << "Hello Octave world!\n";
    
      int n = 2;
      Matrix a_matrix = Matrix (n, n);
    
      for (octave_idx_type i = 0; i < n; i++)
        for (octave_idx_type j = 0; j < n; j++)
          a_matrix(i,j) = (i + 1) * 10 + (j + 1);
    
      std::cout << a_matrix;
    
      return 0;
    }
    

I also included: 1) iostream and 2) octave directory: N:\octave-4.0.0\include\octave-4.0.0\octave\oct.h

I got this error in msvc10: 1>n:\octave-4.0.0\include\math.h(169): error C2065: 'asm' : undeclared identifier 1>n:\octave-4.0.0\include\math.h(169): error C2146: syntax error : missing ';' before identifier 'volatile' 1>n:\octave-4.0.0\include\math.h(169): error C2143: syntax error : missing ')' before ':' 1>n:\octave-4.0.0\include\math.h(169): error C2059: syntax error : ')'

and this error in codeblocks using gcc:

C:/Users/Sam/Documents/codeblock_C++/testOctave/main.cpp:19: undefined reference to operator<<(std::ostream&, Matrix const&)' obj\Debug\main.o: In functionZN10dim_vector11make_uniqueEv': N:/octave-4.0.0/include/octave-4.0.0/octave/dim-vector.h:134: undefined reference to __sync_add_and_fetch_4' obj\Debug\main.o: In functionZN10dim_vectorD1Ev': N:/octave-4.0.0/include/octave-4.0.0/octave/dim-vector.h:286: undefined reference to __sync_add_and_fetch_4' obj\Debug\main.o: In functionZN15octave_refcountIiEmmEv': N:/octave-4.0.0/include/octave-4.0.0/octave/oct-refcount.h:72: undefined reference to __sync_add_and_fetch_4' obj\Debug\main.o: In functionZN5ArrayIdEC2ERK10dim_vector': N:/octave-4.0.0/include/octave-4.0.0/octave/Array.h:184: undefined reference to dim_vector::safe_numel() const' obj\Debug\main.o:main.cpp:(.rdata$_ZTV6Matrix[__ZTV6Matrix]+0x10): undefined reference toArray::resize_fill_value() const' obj\Debug\main.o:main.cpp:(.rdata$_ZTV7NDArray[__ZTV7NDArray]+0x10): undefined reference to Array<double>::resize_fill_value() const' obj\Debug\main.o:main.cpp:(.rdata$_ZTV6MArrayIdE[__ZTV6MArrayIdE]+0x10): undefined reference toArray::resize_fill_value() const' obj\Debug\main.o:main.cpp:(.rdata$_ZTV5ArrayIdE[__ZTV5ArrayIdE]+0x10): undefined reference to `Array::resize_fill_value() const' collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 1 second(s))

I really appreciate any help!

1

There are 1 answers

0
lostbard On

Octave > 4.0 is using msys and gcc in thein windows, but you don't really need to know that in order to get it to compile.

Looking at what you are trying to do, your are using a IDE other than octave to try to compile the code without then including the octave paths and octave libraries correctly.

The easiest way is to use octave to do it.

Firstly you will need to include the octave header, and since you are using cout, iostream:

#include <octave/oct.h>
#include <iostream>

Then from octave us the mkoctfile command with the --link-stand-alone option as shown in the link you referenced.

mkoctfile --link-stand-alone main.cpp