Octave示例代码无法在g ++中编译?

时间:2013-09-03 12:59:30

标签: c++ octave

我试图在g++ (Ubuntu/Linaro 4.6.4-1ubuntu1~12.04) 4.6.4)中编译Octave C ++代码here

以上修剪过的版本将在g++中编译:

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

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

  return 0;
}

但如果我取消标记Matrix L=Matrix(2,2);行,然后使用g++ temp.cpp进行编译,则会显示错误消息:

/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

我不确定为什么。也许我错过了#include,也许我没有安装合适的文件,也许我没有链接到相应的库,或者我可能在某种程度上误用了Octave。

  

问题:为什么无法编译?我该如何解决?

如果我按照上述网站所示使用mkoctfile --link-stand-alone temp.cpp,它编译得很好,但是,如果可能的话,我想使用g++,因为我最终希望能够从另一个地方调用Octave函数程序我用C ++编写。

3 个答案:

答案 0 :(得分:1)

您需要链接到八度音阶库。如果库是octave.a

g++ -loctave temp.cpp

答案 1 :(得分:1)

如我的评论中所示,可以找到一个简单的例子in this Howto。因此,在您的情况下,实现编译的简单方法是创建一个makefile,如下所示:

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)是一个环境变量,应设置为八度音阶包含路径(例如/usr/include/octave-x.x.xx)。然后,您可以使用命令make all编译和链接测试应用程序。

答案 2 :(得分:0)

将库目录添加到链接命令:

  • your-local-path \ octave-x.x.xx \ lib \
  • 您的本地路径\倍频x.x.xx \ lib中\倍频\ 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

对于cxx11链接器错误:
Converting std::__cxx11::string to std::string

“如果您收到链接器错误,这些错误涉及对涉及std :: __ cxx11命名空间中的类型或标签[abi:cxx11]的符号的未定义引用,那么它可能表示您正在尝试将使用不同编译的目标文件链接在一起_GLIBCXX_USE_CXX11_ABI宏的值。这通常在链接到使用旧版GCC编译的第三方库时发生。“

在包含任何标准库标题之前定义以下宏应解决您的问题:

#define _GLIBCXX_USE_CXX11_ABI 0
相关问题