Search code examples
cmathgl

Basic MathGL example in c does not compile


Here is the basic c example from the documentation:

#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. */
}

Here is the start of compilation output:

$ 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 *)
      |                     ^~~~~~

It seems clear to me that the example is not even valid c, missing a parameter (that is not actually used) to the sample() function. I have tried removing it but still get the first (internal mathgl) error.

Any ideas how to proceed?


Solution

  • It seems MathGL doesn't have their internal #include statements in order, and require you to be careful about what you #include and in what order. In particular, ensure you #include <mgl2/mgl.h> before any other MathGL header, and before that one ensure you #include <stdbool.h>. Futhermore, when you use for example Qt-related functions, ensure you #include <mgl2/qt.h>. This should work:

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