在Ubuntu Linux下创建使用GSL函数库的工程

GSL是一个开源的,用C/C++写成的数学函数库,非常流行也非常实用。

我们用cmake 创建 Makefile 文件,熟悉cmake的朋友应该知道,cmake可以直接产生MS studio, Eclipse,QT 等开发环境的工程文件。这里我们产生通用的Makfile文件。

Ubuntu 14.04 下载、安装、配置的相关知识

Ubuntu 14.04系统下载地址

Windows 7下硬盘安装Ubuntu 14.04图文教程

1. 在Ubuntu Linux下安装GSL:
  打开Synaptic Package Manager, 安装libgsl0-dev 和libgsl0ldbl

2.  创建FindGSL.cmake 文件,然后将FindGSL.cmake 移动到 "/usr/share/cmake-2.8/Modules"
COMMENTS: .cmake文件很重要,是CMakeLists.txt中findpackage命令的基础,如果没有Find*.cmake文件,在cmake执行findpackage命令是会报错的。

---------------------------------------------------------------------------------------

# Try to find gnu scientific library GSL
# See
#   and
#
#
# Once run this will define:
#
# GSL_FOUND      = system has GSL lib
#
# GSL_LIBRARIES  = full path to the libraries
#    on Unix/Linux with additional linker flags from "gsl-config --libs"
#
# CMAKE_GSL_CXX_FLAGS  = Unix compiler flags for GSL, essentially "`gsl-config --cxxflags`"
#
# GSL_INCLUDE_DIR      = where to find headers
#
# GSL_LINK_DIRECTORIES = link directories, useful for rpath on Unix
# GSL_EXE_LINKER_FLAGS = rpath on Unix
#
# Felix Woelk 07/2004
# Jan Woetzel
#
#
# --------------------------------


IF(WIN32)
# JW tested with gsl-1.8, Windows XP, MSVS 7.1, MSVS 8.0
SET(GSL_POSSIBLE_ROOT_DIRS
${GSL_ROOT_DIR}
$ENV{GSL_ROOT_DIR}
${GSL_DIR}
${GSL_HOME}
$ENV{GSL_DIR}
$ENV{GSL_HOME}
$ENV{EXTERN_LIBS_DIR}/gsl
$ENV{EXTRA}
# "C:/home/jw/source2/gsl-1.8"
)
FIND_PATH(GSL_INCLUDE_DIR
NAMES gsl/gsl_cdf.h gsl/gsl_randist.h
PATHS ${GSL_POSSIBLE_ROOT_DIRS}
PATH_SUFFIXES include
DOC "GSL header include dir"
)


FIND_LIBRARY(GSL_GSL_LIBRARY
NAMES gsl libgsl
PATHS  ${GSL_POSSIBLE_ROOT_DIRS}
PATH_SUFFIXES lib
DOC "GSL library dir" )


FIND_LIBRARY(GSL_GSLCBLAS_LIBRARY
NAMES gslcblas libgslcblas
PATHS  ${GSL_POSSIBLE_ROOT_DIRS}
PATH_SUFFIXES lib
DOC "GSL cblas library dir" )


SET(GSL_LIBRARIES ${GSL_GSL_LIBRARY})


#MESSAGE("DBG\n"
#  "GSL_GSL_LIBRARY=${GSL_GSL_LIBRARY}\n"
#  "GSL_GSLCBLAS_LIBRARY=${GSL_GSLCBLAS_LIBRARY}\n"
#  "GSL_LIBRARIES=${GSL_LIBRARIES}")


ELSE(WIN32)


IF(UNIX)
SET(GSL_CONFIG_PREFER_PATH
 "$ENV{GSL_DIR}/bin"
 "$ENV{GSL_DIR}"
 "$ENV{GSL_HOME}/bin"
 "$ENV{GSL_HOME}"
 CACHE STRING "preferred path to GSL (gsl-config)")
FIND_PROGRAM(GSL_CONFIG gsl-config
 ${GSL_CONFIG_PREFER_PATH}
 /usr/bin/
 )
# MESSAGE("DBG GSL_CONFIG ${GSL_CONFIG}")


IF (GSL_CONFIG)


 MESSAGE(STATUS "GSL using gsl-config ${GSL_CONFIG}")
 # set CXXFLAGS to be fed into CXX_FLAGS by the user:
 EXEC_PROGRAM(${GSL_CONFIG}
  ARGS --cflags
  OUTPUT_VARIABLE  GSL_CXX_FLAGS )
 #SET(GSL_CXX_FLAGS "`${GSL_CONFIG} --cflags`")


 # set INCLUDE_DIRS to prefix+include
 EXEC_PROGRAM(${GSL_CONFIG}
  ARGS --prefix
  OUTPUT_VARIABLE GSL_PREFIX)
 SET(GSL_INCLUDE_DIR ${GSL_PREFIX}/include CACHE STRING INTERNAL)


 # set link libraries and link flags


 #SET(GSL_LIBRARIES "`${GSL_CONFIG} --libs`")


 # extract link dirs for rpath
 EXEC_PROGRAM(${GSL_CONFIG}
  ARGS --libs
  OUTPUT_VARIABLE  GSL_CONFIG_LIBS )
 SET(GSL_LIBRARIES "${GSL_CONFIG_LIBS}")


 # split off the link dirs (for rpath)
 # use regular expression to match wildcard equivalent "-L*"
 # with  is a space or a semicolon
 STRING(REGEX MATCHALL "[-][L]([^ ;])+"
  GSL_LINK_DIRECTORIES_WITH_PREFIX
  "${GSL_CONFIG_LIBS}" )
 #      MESSAGE("DBG  GSL_LINK_DIRECTORIES_WITH_PREFIX=${GSL_LINK_DIRECTORIES_WITH_PREFIX}")


 # remove prefix -L because we need the pure directory for LINK_DIRECTORIES


 IF (GSL_LINK_DIRECTORIES_WITH_PREFIX)
  STRING(REGEX REPLACE "[-][L]" "" GSL_LINK_DIRECTORIES ${GSL_LINK_DIRECTORIES_WITH_PREFIX} )
 ENDIF (GSL_LINK_DIRECTORIES_WITH_PREFIX)
 SET(GSL_EXE_LINKER_FLAGS "-Wl,-rpath,${GSL_LINK_DIRECTORIES}" CACHE STRING INTERNAL)
 #      MESSAGE("DBG  GSL_LINK_DIRECTORIES=${GSL_LINK_DIRECTORIES}")
 #      MESSAGE("DBG  GSL_EXE_LINKER_FLAGS=${GSL_EXE_LINKER_FLAGS}")

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/17979.html