3

I'm trying to run mathgl with cmake and I'm having some problems. The code for the file test001.cpp is as follows:

#include <stdio.h>
#include <mgl2/qt.h>
int sample(mglGraph *gr)
{
  gr->Rotate(60,40);
  gr->Box();
  return 0;
}
int main(int argc,char **argv)
{
  mglQT gr(sample,"MathGL examples");
  return gr.Run();
}

I compile it with this CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project (test001)
SET(CMAKE_MODULE_PATH ${test001_SOURCE_DIR})

# FIND_PACKAGE(Qt REQUIRED)
find_package(MathGL)
include_directories(
    ${MathGL_INCLUDE_DIRS}
)
add_executable(test001 test001.cpp)
target_link_libraries(test001 ${MathGL_LIBRARIES})

Which needs the following cmake module:

FIND_PATH(MATHGL_INCLUDE_DIR NAMES mgl2/mgl.h
  PATHS
  /opt/local/include
  /usr/include
  /usr/local/include
)

FIND_LIBRARY(MATHGL_LIB NAMES mgl
  PATHS
  /opt/local/lib
  /usr/local/lib
  /usr/lib
)

IF (MATHGL_INCLUDE_DIR AND MATHGL_LIB)
  SET(MATHGL_FOUND TRUE)
  MESSAGE(STATUS "MATHGL found")
  MESSAGE(STATUS "MATHGL Include dirs:" ${MATHGL_INCLUDE_DIR})
  MESSAGE(STATUS "MATHGL Library:" ${MATHGL_LIB})
ELSE (MATHGL_INCLUDE_DIR AND MATHGL_LIB)
  MESSAGE(STATUS "MATHGL was not found")
ENDIF(MATHGL_INCLUDE_DIR AND MATHGL_LIB)

All the files are in the same folder.

I run then cmake .. inside a folder called build and I get apparently a good answer:

-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- MATHGL found
-- MATHGL Include dirs:/usr/include
-- MATHGL Library:/usr/lib/libmgl.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fogg/dev/c/mathgl/build

But then I get a compilation error as if I wasn't linking the program, but I'm doing it in the CMakeLists.txt file.

Scanning dependencies of target test001
[100%] Building CXX object CMakeFiles/test001.dir/test001.cpp.o
Linking CXX executable test001
CMakeFiles/test001.dir/test001.cpp.o: In function `mglGraph::mglGraph(int, int, int)':
test001.cpp:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x3b): undefined reference to `mgl_create_graph_gl'
test001.cpp:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x57): undefined reference to `mgl_create_graph'
CMakeFiles/test001.dir/test001.cpp.o: In function `mglGraph::~mglGraph()':
test001.cpp:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x28): undefined reference to `mgl_use_graph'
test001.cpp:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x42): undefined reference to `mgl_delete_graph'
CMakeFiles/test001.dir/test001.cpp.o: In function `mglGraph::SetFontSize(double)':
test001.cpp:(.text._ZN8mglGraph11SetFontSizeEd[_ZN8mglGraph11SetFontSizeEd]+0x2a): undefined reference to `mgl_set_font_size'
CMakeFiles/test001.dir/test001.cpp.o: In function `mglGraph::Rotate(double, double, double)':
test001.cpp:(.text._ZN8mglGraph6RotateEddd[_ZN8mglGraph6RotateEddd]+0x4e): undefined reference to `mgl_rotate'
CMakeFiles/test001.dir/test001.cpp.o: In function `mglGraph::Box(char const*, bool)':
test001.cpp:(.text._ZN8mglGraph3BoxEPKcb[_ZN8mglGraph3BoxEPKcb]+0x2c): undefined reference to `mgl_box_str'
CMakeFiles/test001.dir/test001.cpp.o: In function `mglQT::mglQT(int (*)(mglGraph*), char const*)':
test001.cpp:(.text._ZN5mglQTC2EPFiP8mglGraphEPKc[_ZN5mglQTC5EPFiP8mglGraphEPKc]+0x34): undefined reference to `mgl_draw_graph'
test001.cpp:(.text._ZN5mglQTC2EPFiP8mglGraphEPKc[_ZN5mglQTC5EPFiP8mglGraphEPKc]+0x50): undefined reference to `mgl_create_graph_qt'
CMakeFiles/test001.dir/test001.cpp.o: In function `mglQT::Run()':
test001.cpp:(.text._ZN5mglQT3RunEv[_ZN5mglQT3RunEv]+0xd): undefined reference to `mgl_qt_run'
collect2: error: ld returned 1 exit status
make[2]: *** [test001] Error 1
make[1]: *** [CMakeFiles/test001.dir/all] Error 2
make: *** [all] Error 2

Side-note: this program compiles perfectly well when using g++ test001.cpp -lmgl-qt -lmgl -o test001. Any ideas?

silgon
  • 6,890
  • 7
  • 46
  • 67
  • 1
    print a message that shows `${MathGL_LIBRARIES}` – Jepessen Nov 03 '15 at 22:10
  • Thanks, it was a lead to the answer, I had some problems in the `FindMathGL.cmake` file. – silgon Nov 03 '15 at 22:26
  • Do you mind posting the answer then, I'm pretty curious about this one – LBes Nov 03 '15 at 22:28
  • It was really something silly since I'm not an expert with cmake, it's the first time I play around with a module. I decided look for them and this was kind of working but it needed some tweaks :/ Thanks for your input, it helped a lot! – silgon Nov 03 '15 at 22:34
  • In CMake variables' names are case-sensitive. While it is recomended for `find*.cmake` script to use `_LIBRARIES`, `_INCLUDE_DIRECTORIES` variables, it is up to the script to choose naming. Given script uses upper-case package name as a variable name's prefix, and this is not an error. – Tsyvarev Nov 03 '15 at 23:01

4 Answers4

4

I was using a FindMathGL.cmake that I downloaded it from this site and apparently it contains some errors. I corrected the file and post it next, it could be useful for somebody in the future.

FIND_PATH(MathGL_INCLUDE_DIRS NAMES mgl2/mgl.h
  PATHS
  /opt/local/include
  /usr/include
  /usr/local/include
)

FIND_LIBRARY(MathGL_LIB NAMES mgl
  PATHS
  /opt/local/lib
  /usr/local/lib
  /usr/lib
)
FIND_LIBRARY(MathGL_QT_LIB NAMES mgl-qt
  PATHS
  /opt/local/lib
  /usr/local/lib
  /usr/lib
)


SET(MathGL_LIBRARIES ${MathGL_LIB} ${MathGL_QT_LIB})

IF (MathGL_INCLUDE_DIRS AND MathGL_LIBRARIES)
  SET(MathGL_FOUND TRUE)
  MESSAGE(STATUS "MathGL found")
  MESSAGE(STATUS "MathGL Include dirs:" ${MathGL_INCLUDE_DIRS})
  MESSAGE(STATUS "MathGL Library:" ${MathGL_LIB})
ELSE (MathGL_INCLUDE_DIRS AND MathGL_LIBRARIES)
  MESSAGE(STATUS "MathGL was not found")
ENDIF(MathGL_INCLUDE_DIRS AND MathGL_LIBRARIES)
silgon
  • 6,890
  • 7
  • 46
  • 67
1

For everyone else looking for this issue:

There is a file named FindMathGL2.cmake in the scripts folder under the mathgl-code folder in the bundle downloaded from sourceforge.

To use it, include the following lines in your CMakeLists.txt file:

set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /path-to-mathgl-code-folder/scripts)
find_package(MathGL2 REQUIRED)
include_directories(${MATHGL2_INCLUDE_DIRS})
link_directories(${CMAKE_PREFIX_PATH}/lib/)

Replace path-to-mathgl-code-folder with the suitable path.

For example, in my project:

set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake_add/)

Where cmake_add is a folder in my project containing only FindMathGL2.cmake

P.S.: You can find more customization instructions in the file FindMathGL2.cmake itself

P.S.2: install dev mathgl package on ubuntu:

sudo apt-get install libmgl-dev
Duloren
  • 2,395
  • 1
  • 25
  • 36
1

For anyone who looking on @silgon answer and attempts to make it work on Mac:

  1. MathGL is not found by default with brew. Add tap:

    brew tap brewsci/homebrew-science

  2. Even you now try to install MathGL, it lacks QT, FLTK etc support. Need to install from source like this:

    brew install --build-from-source mathgl --with-qt --with-wxmac

  3. Now headers and libraries are in versioned directories like /usr/local/Cellar/mathgl/2.3.5.1_4/lib/ and /usr/local/Cellar/mathgl/2.3.5.1_4/include/mgl2/ and hardcoding is something we want to avoid. To make CMake work above modify it like this

    FIND_PATH(MathGL_INCLUDE_DIRS NAMES mgl2/mgl.h PATHS /opt/local/include /usr/include /usr/local/include /usr/local/Cellar/mathgl/*/ PATH_SUFFIXES lib )

Same for every header and library you want to use.

Tõnu Samuel
  • 2,877
  • 2
  • 20
  • 30
1

Not strictly related, for posterity: the version of FindMathGL2.cmake bundled with MathGL 2.4 doesn't handle GLUT libraries for some reason (other components probably aren't linked either).

You'll have to add a section to add /usr/local/lib/libmgl-glut.so to MATHGL2_LIBRARY.

0xDBFB7
  • 242
  • 4
  • 10