MathGL 和 Cmake
MathGL and Cmake
我正在尝试 运行 mathgl
和 cmake
,但我遇到了一些问题。 test001.cpp
文件的代码如下:
#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();
}
我用这个编译它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})
需要以下内容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)
所有文件都在同一个文件夹中。
我 运行 然后 cmake ..
在一个名为 build 的文件夹中,我显然得到了一个很好的答案:
-- 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
但随后出现编译错误,好像我没有链接程序,但我在 CMakeLists.txt 文件中进行了链接。
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
旁注:该程序在使用 g++ test001.cpp -lmgl-qt -lmgl -o test001
时编译得非常好。有什么想法吗?
我使用的是从 this site 下载的 FindMathGL.cmake
,显然它包含一些错误。我更正了文件并 post 接下来,它可能对将来的人有用。
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)
对于寻找此问题的其他人:
从 sourceforge 下载的包中 mathgl-code 文件夹下的 scripts 文件夹中有一个名为 FindMathGL2.cmake 的文件。
要使用它,请在您的 CMakeLists.txt 文件中包含以下行:
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/)
用合适的路径替换path-to-mathgl-code-folder
。
比如我的项目中:
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake_add/)
其中 cmake_add 是我项目中的一个文件夹,仅包含 FindMathGL2.cmake
P.S.: 您可以在文件 FindMathGL2.cmake 本身
中找到更多自定义说明
P.S.2: 在 ubuntu:
上安装 dev mathgl 包
sudo apt-get install libmgl-dev
对于查看@silgon 答案并尝试使其在 Mac 上运行的任何人:
默认情况下,brew 找不到 MathGL。添加水龙头:
酿造水龙头brewsci/homebrew-science
即使您现在尝试安装 MathGL,它也缺少 QT、FLTK 等支持。需要像这样从源安装:
brew 安装 --build-from-source mathgl --with-qt --with-wxmac
现在 headers 和库在版本化目录中,例如
/usr/local/Cellar/mathgl/2.3.5.1_4/lib/ 和 /usr/local/Cellar/mathgl/2.3.5.1_4/include/mgl2/ 和硬编码是我们要避免的事情。要使 CMake 在上面工作,请像这样修改它
FIND_PATH(MathGL_INCLUDE_DIRS 名称 mgl2/mgl.h
路径
/opt/local/include
/usr/include
/usr/local/include
/usr/local/Cellar/mathgl/*/
PATH_SUFFIXES 库
)
每个 header 和您要使用的库都相同。
不严格相关,为了后代:与 MathGL 2.4 捆绑在一起的 FindMathGL2.cmake 版本出于某种原因不处理 GLUT 库(其他组件可能也没有链接)。
您必须添加一个部分以将 /usr/local/lib/libmgl-glut.so
添加到 MATHGL2_LIBRARY
。
我正在尝试 运行 mathgl
和 cmake
,但我遇到了一些问题。 test001.cpp
文件的代码如下:
#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();
}
我用这个编译它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})
需要以下内容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)
所有文件都在同一个文件夹中。
我 运行 然后 cmake ..
在一个名为 build 的文件夹中,我显然得到了一个很好的答案:
-- 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
但随后出现编译错误,好像我没有链接程序,但我在 CMakeLists.txt 文件中进行了链接。
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
旁注:该程序在使用 g++ test001.cpp -lmgl-qt -lmgl -o test001
时编译得非常好。有什么想法吗?
我使用的是从 this site 下载的 FindMathGL.cmake
,显然它包含一些错误。我更正了文件并 post 接下来,它可能对将来的人有用。
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)
对于寻找此问题的其他人:
从 sourceforge 下载的包中 mathgl-code 文件夹下的 scripts 文件夹中有一个名为 FindMathGL2.cmake 的文件。
要使用它,请在您的 CMakeLists.txt 文件中包含以下行:
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/)
用合适的路径替换path-to-mathgl-code-folder
。
比如我的项目中:
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake_add/)
其中 cmake_add 是我项目中的一个文件夹,仅包含 FindMathGL2.cmake
P.S.: 您可以在文件 FindMathGL2.cmake 本身
中找到更多自定义说明P.S.2: 在 ubuntu:
上安装 dev mathgl 包sudo apt-get install libmgl-dev
对于查看@silgon 答案并尝试使其在 Mac 上运行的任何人:
默认情况下,brew 找不到 MathGL。添加水龙头:
酿造水龙头brewsci/homebrew-science
即使您现在尝试安装 MathGL,它也缺少 QT、FLTK 等支持。需要像这样从源安装:
brew 安装 --build-from-source mathgl --with-qt --with-wxmac
现在 headers 和库在版本化目录中,例如 /usr/local/Cellar/mathgl/2.3.5.1_4/lib/ 和 /usr/local/Cellar/mathgl/2.3.5.1_4/include/mgl2/ 和硬编码是我们要避免的事情。要使 CMake 在上面工作,请像这样修改它
FIND_PATH(MathGL_INCLUDE_DIRS 名称 mgl2/mgl.h 路径 /opt/local/include /usr/include /usr/local/include /usr/local/Cellar/mathgl/*/ PATH_SUFFIXES 库 )
每个 header 和您要使用的库都相同。
不严格相关,为了后代:与 MathGL 2.4 捆绑在一起的 FindMathGL2.cmake 版本出于某种原因不处理 GLUT 库(其他组件可能也没有链接)。
您必须添加一个部分以将 /usr/local/lib/libmgl-glut.so
添加到 MATHGL2_LIBRARY
。