在C语言中使用Octave内置函数

3

我被告知要使用Octave用C编译此程序。之前我已经成功地使用Octave编译了一个简单的C程序,但对于这个程序,尽管我试图将部分C++语言更改为C语言,但我仍然无法在C中进行编译。

 #include <iostream>

 #include <octave/oct.h>

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

这个问题有解决方案吗?非常感谢,对于我在使用Octave和这个论坛上的任何错误,请原谅。
编辑(Alter Mann回答后):
这是我的C程序代码。
#include <stdio.h>

int main(void)
{
        printf("Hello Octave World!");

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

            printf ("%d", &a_matrix);
    }
    return 0;
}

但是我收到了这个错误

standalone.c: 在函数‘main’中:
standalone.c:8: 错误:未声明‘Matrix’(在此函数中第一次使用)
standalone.c:8: 错误:(每个未声明的标识符仅报告一次
standalone.c:8: 错误:对于出现的每个函数。)
standalone.c:8: 错误:在‘a_matrix’之前期望‘;’
standalone.c:10: 错误:未声明‘octave_idx_type’(在此函数中第一次使用)
standalone.c:10: 错误:在‘i’之前期望‘;’
standalone.c:10: 错误:在此函数中第一次使用‘i’
standalone.c:12: 错误:在此函数中第一次使用‘j’
standalone.c:12: 错误:在‘j’之前期望‘;’
standalone.c:14: 警告:隐式声明函数‘a_matrix’
standalone.c:17: 错误:未声明‘a_matrix’(在此函数中第一次使用)


你能提供最终的代码吗?你有用过C语言中的Octave吗? - mohammadsdtmnd
1个回答

2
#include <iostream>

应该是

#include <stdio.h>

并且

std::cout << a_matrix;

应该是

printf("%d", a_matrix);

但是在C中不能使用<octave/oct.h>

A.1.1 Oct-Files入门

第一行关键代码是 #include ,它使 C++ oct-file 所需的大多数定义可用。请注意,octave/oct.h 是一个 C++ 头文件,不能在 C 源文件中直接 #include,也不能在任何其他语言中使用。

另一种方法:

接口围绕支持 C++、C 和 Fortran 语言展开。Octave 本身是用 C++ 编写的,可以通过其本地 oct-file 接口调用外部的 C++/C 代码。为了与 MATLAB 兼容性,C 语言也通过 mex-file 接口得到支持。Fortran 代码最容易通过 oct-file 接口实现。


是的,我按照你建议的修改了代码,但我在编辑后的帖子中得到了一个错误。 - IS1001
Note that octave/oct.h is a C++ header and cannot be directly #include’ed in a C source file, nor any other language. - David Ranieri
是的,我已经像我发布的那样删除了octave/oct.h文件,但我仍然遇到错误。您能否向我展示如何在C源文件中使用Octave函数的示例? - IS1001
你仍然会遇到错误,因为MatrixOctave类型,请使用C++或mex文件接口以与MATLAB兼容。 - David Ranieri
哦,我明白了。所以这段代码不能使用C编译,对吗? - IS1001
哦,好的。谢谢你的帮助。 - IS1001

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接