本文介紹在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
五、 相關參考及擴展閱讀
(文中部份內容來源於網絡,感謝原做者分享)