我試圖運行mathgl
與cmake
,我遇到了一些問題。該文件test001.cpp
的代碼如下:MathGL和Cmake
#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 ..
一個文件夾,名爲內搭,我也得到明顯一個很好的答案:
-- 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
時,該程序的編譯效果非常好。有任何想法嗎?
打印,顯示'$ {} MathGL_LIBRARIES消息' – Jepessen
謝謝,這是一個領先的答案,我曾在'FindMathGL.cmake'文件中的一些問題。 – silgon
你介意發佈答案然後,我很好奇這個問題 – LBes