【问题标题】:Using Octave in C++在 C++ 中使用 Octave
【发布时间】:2018-10-18 20:44:26
【问题描述】:

我是 Octave 和 C++ 的新手!我想在 C++ 中使用 Octave 并找到了这个webpage。我从上面的代码开始并尝试运行它。不幸的是,没有关于这个问题的文件,我遇到了很多错误。我第一次使用msvc 2010,发现我应该使用gcc!以下是我到目前为止所做的:

  1. link 下载 Octave,(我下载 octave-4.0.0_0.zip)
  2. 我试图编译这段代码:

    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;
    }
    

我还包括:1) iostream 和 2) octave 目录:N:\octave-4.0.0\include\octave-4.0.0\octave\oct.h

我在 msvc10 中收到此错误: 1>n:\octave-4.0.0\include\math.h(169): 错误 C2065: 'asm' : 未声明的标识符 1>n:\octave-4.0.0\include\math.h(169): 错误 C2146: 语法错误: 缺少';'在标识符“volatile”之前 1>n:\octave-4.0.0\include\math.h(169): 错误 C2143: 语法错误: 在 ':' 之前缺少 ')' 1>n:\octave-4.0.0\include\math.h(169): 错误 C2059: 语法错误: ')'

以及使用 gcc 的代码块中的此错误:

C:/Users/Sam/Documents/codeblock_C++/testOctave/main.cpp:19: 未定义引用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: 未定义引用__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: 未定义引用__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: 未定义引用__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: 未定义引用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): 未定义引用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:错误:ld 返回 1 退出状态 进程以状态 1 终止(0 分钟,1 秒)

非常感谢任何帮助!

【问题讨论】:

  • 如果可以,不如先在Linux上试试?
  • 我也有 Ubuntu,你知道我如何在 Ubuntu 中做到这一点吗?
  • 我认为最好先在 Ubuntu 上编译 Octave 本身。您可以参考thisthis。然后按照您找到的 Octave 网页上的说明进行操作。
  • 我知道它的旧线程,但我在使用 Visual Studio 2013 编译时也遇到了同样的错误。有人有任何线索吗?
  • 我已经尝试使用 MinGW 和 Cygwin 进行编译 - 在 dim_vector 和 Array 区域中仍然出现“未定义的引用...”错误。有人对此有所了解吗?

标签: c++ octave


【解决方案1】:

Octave > 4.0 在 Windows 中使用 msys 和 gcc,但您并不需要知道这些才能编译它。

看看您要做什么,您正在使用 octave 以外的 IDE 来尝试编译代码,而没有正确包含 octave 路径和 octave 库。

最简单的方法是使用八度来做到这一点。

首先,您需要包含 octave 标头,并且由于您使用的是 cout,iostream:

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

然后从 octave 我们使用带有 --link-stand-alone 选项的 mkoctfile 命令,如您引用的链接中所示。

mkoctfile --link-stand-alone main.cpp

【讨论】:

    猜你喜欢
    • 2017-05-30
    • 2019-10-04
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多