1

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?

m4r35n357
  • 111
  • 8
  • It looks like you are missing `#include `. – G. Sliepen Oct 22 '20 at 13:58
  • @G.Sliepen I think you mean the docs are missing . . . ;) No matter, I still get the same compiler error (did you try it?). BTW I've been looking since and haven't seen another example like this one to learn from. – m4r35n357 Oct 22 '20 at 14:23

1 Answers1

1

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();
}
G. Sliepen
  • 7,637
  • 1
  • 15
  • 31
  • That worked perfectly, thanks! This is clearly going to be a bit more work than I guessed, but now I have a bit of motivation to persevere. – m4r35n357 Oct 22 '20 at 18:36
  • I tried that too and your code partially solved my problem. I was able to make object of your solution code but was not able to build it. I am getting lots of undefined reference errors. The same with my other simple sine plot example. Looks like inclusion of those header files are broken. – Aschoolar Feb 20 '21 at 23:45
  • @Aschoolar You should post your problem as a new question on this site. – G. Sliepen Feb 21 '21 at 09:24
  • @G.Sliepen, thanks for comments. I figure it out anyway but run into other problems. I posted a question there [link](https://stackoverflow.com/questions/66819141/how-to-close-mathgl-glut-subroutine-with-c-code-without-leaving-the-applicatio) – Aschoolar Mar 26 '21 at 14:51