首页 > 解决方案 > c 中的基本 MathGL 示例无法编译

问题描述

这是文档中的基本 c 示例:

#include <mgl2/mgl_cf.h>
int sample(HMGL gr, void *)
{
  mgl_rotate(gr,60,40,0);
  mgl_box(gr);
}
int main(int argc,char **argv)
{
  HMGL gr;
  gr = mgl_create_graph_qt(sample,"MathGL examples",0,0);
  return mgl_qt_run();
/* generally I should call mgl_delete_graph() here,
 * but I omit it in main() function. */
}

这是编译输出的开始:

$ gcc test.c -lmgl-qt5 -lmgl
In file included from /usr/include/mgl2/mgl_cf.h:29,
                 from test.c:1:
/usr/include/mgl2/data_cf.h:527:17: error: expected ‘,’ or ‘;’ before ‘mgl_find_roots’
  527 | bool MGL_EXPORT mgl_find_roots(size_t n, void (*func)(const mreal *x, mreal *f, void *par), mreal *x0, void *par);
      |                 ^~~~~~~~~~~~~~
test.c: In function ‘sample’:
test.c:2:21: error: parameter name omitted
    2 | int sample(HMGL gr, void *)
      |                     ^~~~~~

我似乎很清楚,该示例甚至不是有效的 c,缺少 sample() 函数的参数(实际未使用)。我已尝试将其删除,但仍然出现第一个(内部 mathgl)错误。

任何想法如何进行?

标签: cmathgl

解决方案


似乎 MathGL 没有#include按顺序排列其内部语句,并要求您注意自己#include的内容和顺序。特别是,确保你#include <mgl2/mgl.h>在任何其他 MathGL 标题之前,并在此之前确保你#include <stdbool.h>. 此外,当您使用例如与 Qt 相关的函数时,请确保#include <mgl2/qt.h>. 这应该有效:

#include <stdbool.h>
#include <mgl2/mgl.h>
#include <mgl2/qt.h>

int sample(HMGL gr, void *ignored)
{
  mgl_rotate(gr,60,40,0);
  mgl_box(gr);
}

int main(int argc, char **argv)
{
  HMGL gr = mgl_create_graph_qt(sample, "MathGL examples", 0, 0);
  return mgl_qt_run();
}

推荐阅读