Search code examples
c++mathgl

Building mathgl on windows


I am kind of new to C++ (used to be a Java developer, where this is way easier...) and I have to write an application, which has to plot some graphs and charts. To do so I want to use the MathGL library. And I learned, that I have to compile it myself in order to use it. So that's what I'm trying to do the last few days...

I am using Windows with MinGW and the CLion IDE. I started by extracting MathGL's source code into a folder and opening the folder with CLion. Then I downloaded the source codes of zlib and libpng and set the INCLUDE_DIR variables in the MathGL project to the respective folders and PNG_PNG_LIBRARY to png32. When I try to compile mgl_example via CLion it gives me the following error:

In file included from [...]\mathgl-2.3.5.1\src\data_png.cpp:22:0:
[...]/libpng-1.6.29/png.h:361:27: fatal error: pnglibconf.h: No such file or directory
compilation terminated.
mingw32-make.exe[2]: *** [src/CMakeFiles/mgl.dir/data_png.cpp.obj] Error 1
mingw32-make.exe[2]: *** Waiting for unfinished jobs....
src\CMakeFiles\mgl.dir\build.make:465: recipe for target 'src/CMakeFiles/mgl.dir/data_png.cpp.obj' failed
mingw32-make.exe[2]: Leaving directory '[..]/mathgl-2.3.5.1/cmake-build-debug'
mingw32-make.exe[1]: *** [src/CMakeFiles/mgl.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
CMakeFiles\Makefile2:89: recipe for target 'src/CMakeFiles/mgl.dir/all' failed
mingw32-make.exe[1]: Leaving directory '[...]/mathgl-2.3.5.1/cmake-build-debug'
Makefile:129: recipe for target 'all' failed

EDIT: I managed to fix that first error by copying the prebuilt pnglibconf.h from the scripts directory of the libpng source code. After that the definition of byte in the MinGW header rpcndr.h seems to interfer with the byte(double) method in oPRCFile.cc, which I just fixed by renaming the method to byteN and calling it from a macro (#define byte(c) byteN(c)). Although this might not be the right way to go, it works.

But having that all fixed the linker seems to be configured wrongly: it says cannot find -lpng32. How can I fix that one?

EDIT: Alright, it's compiled. So I copied libmgl.a and put it into the project I want to use it it. I linked against it with cmake by calling target_link_libraries(Test ${CMAKE_SOURCE_DIR}/libmgl.a) but it just throws a bunch of undefined reference to errors (same if I use the procompiled binaries):

MakeFiles\Test.dir/objects.a(main.cpp.obj): In function `ZN8mglDataAC2Ev':
c:/mingw/include/mgl2/abstract.h:156: undefined reference to `_imp___ZTV8mglDataA'
CMakeFiles\Test.dir/objects.a(main.cpp.obj): In function `ZN8mglDataAD2Ev':
c:/mingw/include/mgl2/abstract.h:157: undefined reference to `_imp___ZTV8mglDataA'
[...]

Solution

  • It looks like that missing header file is generated after running a configure for libpng (which under Windows you may be unable to do and need to generate yourself -> Cannot open include file: 'pnglibconf.h':No such file or directory)

    But besides that, how did you get the impression that you need to build it from scratch in order to use it? You can also download the precompiled binaries and link them with your program (http://mathgl.sourceforge.net/doc_en/Installation.html - point 2).

    edit: Looking at your edit, you need to specify the actual library file in Windows (probably something along the lines of png32.a or whatever your libpng compilation generated), assuming the linker also has the path where the library file is (see http://www.mingw.org/wiki/specify_the_libraries_for_the_linker_to_use)