0

I'm attempting to get the Octave C++ code here to compile in g++ (Ubuntu/Linaro 4.6.4-1ubuntu1~12.04) 4.6.4).

This trimmed version of the above will compile in g++:

#include <iostream>
#include <octave/oct.h>
using namespace std;

int main() {
//  Matrix L=Matrix(2,2);

  return 0;
}

but if I unremark out the line Matrix L=Matrix(2,2); then compile with g++ temp.cpp it gives the error message:

/tmp/ccTa3Am5.o: In function `Array2<double>::~Array2()':
temp.cpp:(.text._ZN6Array2IdED2Ev[_ZN6Array2IdED5Ev]+0x1f): undefined reference to `Array<double>::~Array()'
/tmp/ccTa3Am5.o: In function `Array<double>::Array(dim_vector const&)':
temp.cpp:(.text._ZN5ArrayIdEC2ERK10dim_vector[_ZN5ArrayIdEC5ERK10dim_vector]+0x26): undefined reference to `Array<double>::get_size(dim_vector const&)'
/tmp/ccTa3Am5.o:(.rodata._ZTV5ArrayIdE[vtable for Array<double>]+0x10): undefined reference to `Array<double>::~Array()'
/tmp/ccTa3Am5.o:(.rodata._ZTV5ArrayIdE[vtable for Array<double>]+0x18): undefined reference to `Array<double>::~Array()'
collect2: ld returned 1 exit status

I'm unsure why. Perhaps I'm missing an #include, perhaps I don't have an appropriate file installed, perhaps I'm not linking to the appropriate library, or perhaps I'm misusing Octave in some way.

Question: Why is this failing to compile? How can I fix it?

It compiles fine if I use mkoctfile --link-stand-alone temp.cpp as indicated at the above site, however, I'd like to use g++ if possible, since I eventually want to be able to call Octave functions from another program I've written in C++.

Douglas S. Stones
  • 541
  • 1
  • 4
  • 14
  • Maybe you should have a look at [Howto Use Octave Functions in C/C++ Programs](http://www.mathias-michel.de/download/howto-octave-c%2B%2B.ps), which provides some useful information about how to compile C/C++ code with octave included. On Page 2 you will find an example of how to compile such a program using g++ – sebi Sep 03 '13 at 13:06
  • It's not failing to compile, all of those are linker errors. – Ben Voigt Sep 03 '13 at 13:38

3 Answers3

1

You need to link to the octave library. If the library is octave.a:

g++ -loctave temp.cpp
Appleshell
  • 7,088
  • 6
  • 47
  • 96
  • 1
    Not that easy - you should let the `mkoctfile` do the linking for you when working with Octave in C/C++. Can save you a lot of headaches ;) – sebi Sep 03 '13 at 13:11
1

As indicated in my comment a simple example can be found in this Howto. So in your case a simple way to achieve compilation will be creating a makefile as follows:

makefile:
all: temp

clean:
    -rm temp.o temp

temp: temp.o
      mkoctfile --link-stand-alone -o temp temp.o

temp.o: temp.cpp
        g++ -c -I$(OCTAVE_INCLUDE)
        -I$(OCTAVE_INCLUDE)octave -o temp.o temp.cpp

$(OCTAVE_INCLUDE) is an environment variable that should be set to your octave include path (e.g. /usr/include/octave-x.x.xx). Then you can simply compile and link your test application using the command make all.

sebi
  • 431
  • 1
  • 4
  • 9
0

Add the library directories to your link command:

  • your-local-path\octave-x.x.xx\lib\
  • your-local-path\octave-x.x.xx\lib\octave\x.x.xx\

mkoctfile -L"\your-path\octave-x.x.xx\lib" -L"\your-path\octave-x.x.x\lib\octave\x.x.xx" --link-stand-alone temp.cpp

For cxx11 linker errors:
Converting std::__cxx11::string to std::string

"If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC."

Defining the following macro before including any standard library headers should fix your problem:

#define _GLIBCXX_USE_CXX11_ABI 0
Community
  • 1
  • 1
debar
  • 1
  • 1