在WINDOWS中安裝使用GSL(MinGW64+Sublime Text3 & Visual Studio)

        本文介紹在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


 

1、GSL簡介

(官方介紹,不作累述)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,


 

2、在WINDOWS下安裝介紹

        GSL在Windows下,可經過安裝Cygwin使用,因其自帶GSL庫;本文主要介紹在MinGW下用msys生成GSL最新版的靜態庫、導入庫及動態庫過程,並經過命令行、Sublime Text3或Visual Studio編譯使用GSL庫;另外介紹了GSL1.8 VC版在Visual Studio2015下的編譯使用。

3、安裝使用GSL (GNU)

官方建議的最簡單編譯安裝方式:

  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最新版源碼解壓後目錄內容以下:

 

1.  安裝過程(32bit & 64bit)

        要執行上面的三個步驟,首先須要一個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

2.  使用過程(基於MinGW-w64命令行及Sublime Text3和Visual Studio)

  • 示例代碼

在本身的示例代碼目錄下新建一個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
}

 

  • Visual Studio
    • 使用此庫,在Dubug和Release配置下設置方式同樣,根據不一樣編譯平臺(x86或x64)選擇對應庫目錄便可。
    • 在項目的屬性頁,選擇VC++目錄的的常規選項頁,在包含目錄添加"<GSL install dir>\<GSLxXX>\include",在庫目錄添加"<GSL install dir>\<GSLxXX>\lib",可在源目錄添加"<GSL install dir>\src\gsl-2.4";
    • 選擇C/C++目錄的預處理器選項頁,在預處理器定義中添加"GSL_DLL;";
    • 選擇連接器目錄的輸入選項頁,在附加依賴項中添加"libgsl.dll.a;libgslcblas.dll.a;";
    • 此時可編譯生成,成功後,將<GSL install dir>\<GSLxXX>\bin目錄下的libgsl-23.dll和libgslcblas-0.dll複製到項目執行文件生成同目錄下,或直接將該bin目錄添加到系統路徑中。
  • MinGW-w64命令行

在Windows命令行提示符下,進入示例目錄,執行gcc編譯示例:

gcc -o gsldemo gsldemo.c -IC:/gnu/gsl/include -LC:/gnu/gsl/lib -lgsl -lgslcblas –lm
  • Sublime Text3

      附:使用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
}

4、安裝使用Gsl1.8(GnuWin32)

參考:GSL for Windows

          源碼壓縮包中/VC8/Readme_VC8.htm

下載地址:https://sourceforge.net/projects/gnuwin32/files/gsl/1.8/

  • Download

Description

 

Download

 

Size

 

Last change

 

Md5sum

• Complete package, except sources

 

Setup

 

6476814

 

11 July 2006

 

d0c114f842933622a156bfa757ee14c2

• Sources

 

Setup

 

2145579

 

11 July 2006

 

71ee925eb86d7772fa8b4904dc755508

 

               

• Binaries

 

Zip

 

982270

 

11 July 2006

 

94635ae687c4f39d5f4e5985e31c1360

• Developer files

 

Zip

 

1202181

 

11 July 2006

 

ff1be5ca621f2e7849b6405c132f4d59

• Documentation

 

Zip

 

5077599

 

11 July 2006

 

8c97d5a31f179b066c6db885fd42ca22

• Sources

 

Zip

 

4361954

 

11 July 2006

 

5c0df6ca6798ef666b6f89af161b76ba

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來完成此工做,建議就不要解壓或安裝其它包了,避免文件混亂。

具體步驟以下:

  • 用VS2015打開解壓後的目錄中的VC8下的libgsl.sln工程文件;
  • 對主項目按四種編譯配置在x86平臺選項下進行生成;【Release-StaticLib  Multi-Threaded (/MT)、Release-DLL  Multi-Threaded DLL (/MD)、Debug-StaticLib  Multi-Threaded Debug (/MTd)、Debug-DLL  Multi-Threaded Debug DLL (/MDd)】
  • 生成成功後,在VC8項目目錄下可找到以下文件:

libgsl

Configuration

Build Targets

Release-StaticLib

libgsl.lib

Release-DLL

libgsl.dll

libgsl_dll.lib (import library)

Debug-StaticLib

libgsl_d.lib

Debug-DLL

libgsl_d.dll

libgsl_dll_d.lib (import library)

libgslcblas

Configuration

Build Targets

Release-StaticLib

libgslcblas.lib

Release-DLL

libgslcblas.dll

libgslcblas_dll.lib (import library)

Debug-StaticLib

libgslcblas_d.lib

Debug-DLL

libgslcblas_d.dll

libgslcblas_dll_d.lib (import library)

 

  • 在VS和MinGW下使用libgsl及libgslcblas,均以Release-DLL和Debug-DLL文件夾下的.lib及.dll進行調用編譯便可。

總結

一、 在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 【原文

  • Static libraries on Linux have the .a file extension. Static libraries on Windows have the .lib file extension.
  • Dynamic libraries on Windows have the .dll extension; on Linux have the .so extension.
  • The import library is a static library. It contains the code required to load the DLL. in order to link against a DLL, an import library is required. Now you're using GCC (not 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.
  • A DLL may have functions that are exported and functions that are not exported. An import library has to know which functions are exported and which aren't. One of the means of telling it is a DEF file. When building the DLL, the linker uses the .def file to create an export (.exp) file and an import library (.lib) file. The linker then uses the export file to build the DLL file. Executables that implicitly link to the DLL link to the import library when they are built. -- MSDN: Exporting from a DLL Using DEF Files
  • Also see MSDN: Exporting Functions from a DLL by Ordinal Rather Than by Name, together that should answer your last question on export by index, or ordinal number.

四、 關於「 -l 」【原文

How do I specify the libraries to be searched by the linker?

  • MinGW supports libraries named according to the "<name>.lib" and "<name>.dll" conventions, in addition to the normal "lib<name>.a" convention common on *nix systems. To include libraries named according to any of these conventions, simply add an associated "-l<name>" specification to the compiler command, ensuring it is placed after the name of the module in which the reference appears.
  • Note that, if the library is not found in any of the default library search paths, you may also need to insert an appropriate "-L<dir>"switch to specify its location; (it is recommended that you place the "-L<dir>" switch before the "-l<name>" specification which requires it).
  • Also note that the library names "lib<name>.a" and "lib<name>.lib" are not equivalent; if you have a library named according to the aberrant "lib<name>.lib" convention, it will not be found by an "-l<name>" specification -- if you cannot rename the library, you must use the form "-llib<name>" instead.
  • Finally, please do not fall into the all-too-common trap of placing any "-l<name>" specification before the name of any source or object module which requires it. Where the source file bar.c has dependencies on the library archive libfoo.a, the command:
$ 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

五、 相關參考及擴展閱讀

(文中部份內容來源於網絡,感謝原做者分享)

相關文章
相關標籤/搜索