本文介绍在Windows下安装使用GSL库,涉及GSL两个版本(官方最新版及GSL1.8 VC版)、msys shell、GCC、G++等内容,最终实现对GSL安装及示例基于MinGW64在Sublime Text3下的编译运行,并实现GSL在Visual Studio下的使用及部分示例测试。还介绍了GSL1.8 VC版本的编译使用过程。php
目录html
1、GSL简介ios
2、在WINDOWS下安装介绍c++
3、安装使用GSL (GNU)git
1. 安装过程(32bit & 64bit)shell
2. 使用过程(基于MinGW-w64命令行及Sublime Text3和Visual Studio)windows
4、安装使用Gsl1.8(GnuWin32)网络
总结app
(官方介绍,不作累述)dom
GSL简介及下载地址:GSL - GNU Scientific Library
l Introduction
The GNU Scientific Library (GSL) is a numerical library for C and C++ programmers. It is free software under the GNU General Public License.
The library provides a wide range of mathematical routines such as random number generators, special functions and least-squares fitting. There are over 1000 functions in total with an extensive test suite.
The complete range of subject areas covered by the library includes,
Complex Numbers |
Roots of Polynomials |
Special Functions |
Vectors and Matrices |
Permutations |
Sorting |
BLAS Support |
Linear Algebra |
Eigensystems |
Fast Fourier Transforms |
Quadrature |
Random Numbers |
Quasi-Random Sequences |
Random Distributions |
Statistics |
Histograms |
N-Tuples |
Monte Carlo Integration |
Simulated Annealing |
Differential Equations |
Interpolation |
Numerical Differentiation |
Chebyshev Approximation |
Series Acceleration |
Discrete Hankel Transforms |
Root-Finding |
Minimization |
Least-Squares Fitting |
Physical Constants |
IEEE Floating-Point |
Discrete Wavelet Transforms |
Basis splines |
Running Statistics |
Sparse Matrices and Linear Algebra |
Unlike the licenses of proprietary numerical libraries the license of GSL does not restrict scientific cooperation. It allows you to share your programs freely with others.
l Downloading GSL
The current stable version is GSL-2.4. It was released on 19 June 2017. Details of recent changes can be found in the NEWS file.
GSL can be found in the gsl subdirectory on your nearest GNU mirror http://ftpmirror.gnu.org/gsl/.
For other ways to obtain GSL, please read How to get GNU Software
Installation instructions can be found in the included README and INSTALL files.
Precompiled binary packages are included in most GNU/Linux distributions.
A compiled version of GSL is available as part of Cygwin on Windows.
l Documentation
GSL includes a reference manual in reStructuredText format. You can view the manual in HTML and PDF, or read it on your system using the shell command info gsl-ref (if the library is installed).
The GSL Reference Manual is available online,
GSL在Windows下,可经过安装Cygwin使用,因其自带GSL库;本文主要介绍在MinGW下用msys生成GSL最新版的静态库、导入库及动态库过程,并经过命令行、Sublime Text3或Visual Studio编译使用GSL库;另外介绍了GSL1.8 VC版在Visual Studio2015下的编译使用。
官方建议的最简单编译安装方式:
1. `cd' to the directory containing the package's source code and type `./configure' to configure the package for your system. Running `configure' might take a while. While running, it prints some messages telling which features it is checking for.
2. Type `make' to compile the package.
3. Type `make install' to install the programs and any data files and documentation. When installing into a prefix owned by root, it is recommended that the package be configured and built as a regular user, and only the `make install' phase executed with root privileges. OR For example, `make install prefix=/alternate/directory'。
GSL最新版源码解压后目录内容以下:
要执行上面的三个步骤,首先须要一个UNIX环境,在Windows下,可使用msys来模拟,MSYS是一个小型的GNU环境,是一个经过将Linux源代码在Win32上编译而成的UNIX工做环境;MSYS相似于Cygwin,可是因为工做原理的不一样,速度更快、体积更小、功能强大、便于携带;使用MSYS能够取代商业的VC环境,一样开发出商业版权的程序而不受任何版权限制;可从MinGW32官方下载,在此推荐一个集成版下载,其集成了多个实用工具,本文采用集成版msys。将下载的msys集成版压缩包解压后,将msys文件夹直接复制到本身喜欢的地方,建议放在MinGW目录下,便于明确归属。而后将<msys install dir>/bin目录加到系统PATH中。
另外,还须要准备MinGW编译工具,在此就MinGW作一下简单介绍,MinGW 的全称是:Minimalist GNU on Windows 。它提供了一套简单方便的Windows下的基于GCC 程序开发环境,是从Cygwin里fork出来的。当初只考虑32位,和Cygwin相比,不强调POSIX兼容性而相对强调性能和减少依赖。目前还存在的版本有MinGW-w64, MinGW-w64 与 MinGW 的区别在于 MinGW 只能编译生成32位可执行程序,而 MinGW-w64 则能够编译生成 64位 或 32位 可执行程序。(听说,MinGW-W64的主要维护者Kai Tietz由于工做须要,他想让MinGW提供扩充x64支持,但对方态度很不友好,因而愤而fork出来,这就是MinGW-W64的由来:)。如今推荐使用MinGW-w64,因GSL的按照会根据本地系统平台自动生成,也就是说在64位系统下会自动匹配64位的GCC,这样生成的库文件没法在x86的项目中编译经过,所以,须要在本地下载配置32位和64位的MinGW-w64。
先进入MinGW-w64下载地址:https://sourceforge.net/projects/mingw-w64/files/,在此页面根据平台须要连接64位<Toolchains targetting Win64//Personal Builds/mingw-builds/版本号/threads-posix/seh/>或32位<Toolchains targetting Win32/Personal Builds/mingw-builds/版本号/threads-posix/dwarf/>,本示例使用的版本是:x86_64-7.1.0-release-posix-seh-rt_v5-rev2.7z和i686-7.1.0-release-posix-dwarf-rt_v5-rev2.7z,解压后,将bin目录添加到系统路径中(系统路径中应仅存在一个版本的bin目录),在命令行提示符下输入"gcc -v"或"g++ -v"来查看版本信息。
对应关系:
32bit g++.exe —> i686-w64-mingw32-g++
32bit gcc.exe —> i686-w64-mingw32-gcc
64bit g++.exe —> x86_64-w64-mingw32-g++
64bit gcc.exe —> x86_64-w64-mingw32-gcc
根据拟编译GSL的平台环境,配置好MinGW-w64后,进入msys目录,在此目录中执行msys.bat(直接双击运行或以超级管理员身份运行),在$提示符下输入help的界面以下图:
在$提示符下输入:
cd空格/盘符/gsl源码目录/——如:cd /c/gnu/gslmsys_code
进入该目录后,进行GSL安装的三个步骤,前后执行:
1) ./configure —— 此过程较慢,耐心等待便可(针对32位编译,执行CFLAGS="-m32" CXXFLAGS="-m32" ./configure)
2) make —— 编译生成过程
3) make install —— 将编译好的库和头文件等安装在<msys install dir>\local目录中。
安装后的GSL目录中含有bin、lib、include、share四个文件夹
至此,GSL最新版安装完成,GSL库目录结构以下:
\bin (libgsl-23.dll、libgslcblas-0.dll)
\include (\gsl\gsl_*.h)
\lib (libgsl.a、libgsl.la、libgsl.dll.a、libgslcblas.a、libgslcblas.la、libgslcblas.dll.a)
\share
在本身的示例代码目录下新建一个C或CPP文件,复制示例代码并保存。
C示例代码:(gsldemo.c)
#include <stdio.h> #include <gsl/gsl_sf_bessel.h> int main () { double x = 5.0; double y = gsl_sf_bessel_J0 (x); printf ("J0(%g) = %.18e\n", x, y); return 0; // Expected output: J0(5) = -1.775967713143382600e-001 }
CPP示例代码:(gsldemo.cpp)
//#include <stdio.h> #include <iostream> #include <iomanip> //for use std::setprecision() #include <gsl/gsl_sf_bessel.h> using namespace std; int main () { double x = 5.0; double y = gsl_sf_bessel_J0 (x); //printf ("J0(%g) = %.18e\n", x, y); cout << "J0(" << x <<") = " << std::setprecision(18) << std::scientific << y << endl; return 0; // Expected output: J0(5) = -1.775967713143382642e-001 }
在Windows命令行提示符下,进入示例目录,执行gcc编译示例:
gcc -o gsldemo gsldemo.c -IC:/gnu/gsl/include -LC:/gnu/gsl/lib -lgsl -lgslcblas –lm
附:使用Sublime Text3编译使用了GSL库的C或CPP源程序,能够在ST3新建一个编译环境文件,复制粘贴下面内容,并对其中引用的路径修改成正确路径,便可在ST3中进行编译使用了。
// Put this file ([Your Build Name].sublime-build) here: // "C:\Users\[User Name]\AppData\Roaming\Sublime Text 3\Packages\User" // OR use "Tools -> Build System -> New Build System..." and put the code there and save. // Use "Ctrl+B" to Build and "Crtl+Shift+B" to Build or Run the project. { //根据使用GCC或G++须要取消或添加注释便可 //用G++编译调用了GSL库的CPP程序,可命名为G++_GSL.sublime-build "shell_cmd": "g++ -o \"${file}\" \"${file_path}/${file_base_name}\" -I\"<GSL install dir>/include/\" -L\"<GSL install dir>/lib\" -lgsl -lgslcblas –lm ", "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${file_path}", "selector": "source.c, source.c++", "variants": [ { "name": "Run", "shell_cmd": " g++ -o \"${file}\" \"${file_path}/${file_base_name}\" -I\"<GSL install dir>/include/\" -L\"<GSL install dir>/lib\" -lgsl -lgslcblas –lm && start cmd /c \"\"${file_path}/${file_base_name}\" & pause\"" } ] //命令行G++命令示例 // g++ -o gsldemo gsldemo.cpp -IC:/gnu/gsl/include -LC:/gnu/gsl/lib -lgsl -lgslcblas -lm //========================================================= //用GCC编译调用了GSL库的C程序,可命名为GCC_GSL.sublime-build //"shell_cmd": "gcc -o \"${file}\" \"${file_path}/${file_base_name}\" -I\"<GSL install dir>/include/\" -L\"<GSL install dir>/lib\" -lgsl -lgslcblas –lm ", //"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", //"working_dir": "${file_path}", //"selector": "source.c", //"shell": true, //"variants": //[ //{ //"name": "Run", //"shell_cmd": " gcc -o \"${file}\" \"${file_path}/${file_base_name}\" -I\"<GSL install dir>/include/\" -L\"<GSL install dir>/lib\" -lgsl -lgslcblas –lm && start cmd /c \"\"${file_path}/${file_base_name}\" & pause\"" //} //] //命令行GCC命令示例 // gcc -o gsldemo gsldemo.c -IC:/gnu/gsl/include -LC:/gnu/gsl/lib -lgsl -lgslcblas –lm }
源码压缩包中/VC8/Readme_VC8.htm
下载地址:https://sourceforge.net/projects/gnuwin32/files/gsl/1.8/
或
Description |
Download |
Size |
Last change |
Md5sum |
||||
• Complete package, except sources |
|
|
|
|
11 July 2006 |
|
|
|
• Sources |
|
|
|
|
11 July 2006 |
|
|
|
|
||||||||
• Binaries |
|
|
|
|
11 July 2006 |
|
|
|
• Developer files |
|
|
|
|
11 July 2006 |
|
|
|
• Documentation |
|
|
|
|
11 July 2006 |
|
|
|
• Sources |
|
|
|
|
11 July 2006 |
|
|
You can also download the files from the GnuWin32 files page.
以上下载连接分别对应以下文件:
1) gsl-1.8.exe 2006-07-11 6.5 MB
2) gsl-1.8-src.exe 2006-07-11 2.1 MB
3) gsl-1.8-bin.zip 2006-07-11 982.3 kB
4) gsl-1.8-lib.zip 2006-07-11 1.2 MB
5) gsl-1.8-doc.zip 2006-07-11 5.1 MB
6) gsl-1.8-src.zip 2006-07-11 4.4 MB
先不急于立刻安装,分析一下这些文件的做用,1)项执行后将创建一个GSL目录,并可选择性安装三、四、5项的内容,分别创建bin、lib、include目录;2)项执行后将GSL所有源文件复制到GSL目录的/src/gsl-1.8下而已,一、2完整安装后的文件结构以下:
\contrib\gsl-1.8
AUTHORS original gsl-1.7 contributors
BUGS original gsl-1.7 bug list
ChangeLog gsl-1.7 ChangeLog modified for GnuWin32
COPYING original gsl-1.7 license
INSTALL original gsl-1.7 install instructions
NEWS original gsl-1.7 news
README original gsl-1.7 Readme
Readme_GnuWin32.txt GnuWin32-specific Readme
SUPPORT original gsl-1.7 support notice
THANKS original gsl-1.7 acknowledgements
TODO original gsl-1.7 to-do list
\bin
libgsl.dll gsl-1.7 dynamic link library (release version)
libgsl_d.dll gsl-1.7 dynamic link library (gcc debug) (*)
libgslcblas.dll cblas dynamic link library (release version)
libgslcblas_d.dll cblas dynamic link library (gcc debug) (*)
gsl-histogram.exe program for calculating histograms for data
gsl-randist.exe program for generating data conforming to a specified distribution
\lib
libgsl.a gsl static library (gcc)
libgsl_d.a gsl static library (gcc debug) (*)
libgslcblas.a cblas static library (gcc)
libgslcblas_d.a cblas static library (gcc debug) (*)
libgsl_dll.a gcc import library for libgsl.dll
libgsl_d_dll.a gcc import library for libgsl_d.dll (*)
libgslcblas_dll.a gcc import library for libgslcblas.dll
libgslcblas_d_dll.a gcc import library for libgslcblas_d.dll (*)
libgsl.def export definitions for libgsl.dll
libgslcblas.def export definitions for libgslcblas.dll
\doc (**)
\info
\man1
\man3 gsl help in various formats
\include\gsl gsl include files
在此,仅依靠第6项下载文件:gsl-1.8-src.zip来完成此工做,建议就不要解压或安装其它包了,避免文件混乱。
具体步骤以下:
libgsl |
|
||||||||||
libgslcblas |
|
一、 在64bit系统下,强行编译32bit的GSL库,需在msys shell中使用“CFLAGS="-m32" CXXFLAGS="-m32"”参数进行配置。
二、 利用msys环境下make生成的*.dll.a(import library)一样能够在VS下进行调用编译,等同于VS编译生成的*_dll.lib(import library)。
三、关于Static libraries、import library、Dynamic libraries 【原文】
.a
file extension. Static libraries on Windows have the .lib
file extension..dll
extension; on Linux have the .so
extension.cl.exe
) to compile on Windows. GCC has another file extension convention for import libraries, it "should be called *.dll.a or *.a", as explained in the doc for the --out-implib
you referred to. Import libraries (.lib
with MSVC or .dll.a
with GCC) are static libraries: they contain the code to load the DLL. I had the same question the other day.四、 关于“ -l ”【原文】
How do I specify the libraries to be searched by the linker?
$ gcc -lfoo bar.c
is incorrect, and will surely lead to "undefined reference" errors. The correct form for this command is:
$ gcc bar.c -lfoo
五、 相关参考及扩展阅读
(文中部份内容来源于网络,感谢原做者分享)