Create a project using the GSL function library in Ubuntu Linux

Source: Internet
Author: User
Tags regex replace

GSL is an open-source mathematical function library written in C/C ++. It is very popular and practical.

We use cmake to create Makefile files. If you are familiar with cmake, you should know that cmake can directly generate engineering files for development environments such as MS studio, Eclipse, and QT. Here we generate a general Makfile file.

Download, install, and configure Ubuntu 14.04

Ubuntu 14.04 system:

Ubuntu 14.04 text tutorial on hard drive installation in Windows 7

1. Install GSL in Ubuntu Linux:
Open Synaptic Package Manager and install the libgsl0-dev and libgsl0ldbl

2. Create the FindGSL. cmake file and move FindGSL. cmake to "/usr/share/cmake-2.8/Modules"
COMMENTS: .cmakefile is very important. It is the basis of the findpackage command in cmakelists.txt. If the Find *. cmake file is not found, an error is returned when the findpackage command is executed in cmake.

Bytes ---------------------------------------------------------------------------------------

# Try to find gnu scientific library GSL
# See
# Http://www.gnu.org/software/gsl/ and
# Http://gnuwin32.sourceforge.net/packages/gsl.htm
#
# 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, essential tially "'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
#
# Www.mip.informatik.uni-kiel.de
#--------------------------------


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_gce-blas_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_gce-blas_library =$ {gsl_gce-blas_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 }")


# ADD_DEFINITIONS ("-DHAVE_GSL ")
# SET (GSL_DEFINITIONS "-DHAVE_GSL ")
MARK_AS_ADVANCED (
GSL_CXX_FLAGS
GSL_INCLUDE_DIR
GSL_LIBRARIES
GSL_LINK_DIRECTORIES
GSL_DEFINITIONS
)
MESSAGE (STATUS "Using GSL from $ {GSL_PREFIX }")


ELSE (GSL_CONFIG)


INCLUDE (UsePkgConfig) # needed for PKGCONFIG (...)


MESSAGE (STATUS "GSL using pkgconfig ")
# PKGCONFIG (gsl includedir libdir linkflags cflags)
PKGCONFIG (gsl GSL_INCLUDE_DIR GSL_LINK_DIRECTORIES GSL_LIBRARIES GSL_CXX_FLAGS)
IF (GSL_INCLUDE_DIR)
MARK_AS_ADVANCED (
GSL_CXX_FLAGS
GSL_INCLUDE_DIR
GSL_LIBRARIES
GSL_LINK_DIRECTORIES
)


ELSE (GSL_INCLUDE_DIR)
MESSAGE ("FindGSL. cmake: gsl-config/pkg-config gsl not found. Please set it manually. GSL_CONFIG =$ {GSL_CONFIG }")
ENDIF (GSL_INCLUDE_DIR)


ENDIF (GSL_CONFIG)


ENDIF (UNIX)
ENDIF (WIN32)

 


IF (GSL_LIBRARIES)
IF (GSL_INCLUDE_DIR OR GSL_CXX_FLAGS)


SET (GSL_FOUND 1)


ENDIF (GSL_INCLUDE_DIR OR GSL_CXX_FLAGS)
ENDIF (GSL_LIBRARIES)

 


#===================================================== ===
IF (NOT GSL_FOUND)
# Make FIND_PACKAGE friendly
IF (NOT GSL_FIND_QUIETLY)
IF (GSL_FIND_REQUIRED)
MESSAGE (FATAL_ERROR "GSL required, please specify it's location .")
ELSE (GSL_FIND_REQUIRED)
MESSAGE (STATUS "ERROR: GSL was not found .")
ENDIF (GSL_FIND_REQUIRED)
ENDIF (NOT GSL_FIND_QUIETLY)
ENDIF (NOT GSL_FOUND)


Bytes -----------------------------------------------------------------------------------------


3. After the preparation is complete, create our own program.
Create hello. c
-----------------------------------------------
# Include
# Include
Int main (void)
{
Double x = 5.0;
Double y = gsl_sf_bessel_J0 (x );
Printf ("J0 (% g) = %. 18e \ n", x, y );
Return 0;
}
-----------------------------------------------
Create cmakelists.txt
-----------------------------------------------------

Cmake_minimum_required (VERSION 2.8)
Project (hello)
 
Set (CMAKE_MODULE_PATH $ {CMAKE_MODULE_PATH} "$ {CMAKE_SOURCE_DIR }")
 
Find_package (gsl required)
Include_directories ($ {GSL_INCLUDE_DIRES }$ {gce-blas_include_dirs })
Link_libraries ($ {GSL_LIBRARIES }$ {GSLBLAS_LIBRARIES })
 
Target_link_libraries ($ {GSL_LIBRARIES }$ {GSLBLAS_LIBRARIES })
Add_executable (hello. c)
-----------------------------------------------------
Cmake. & make

The executable file "hello" is generated"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.