现在的位置: 首页 > 综合 > 正文

linux下GSL安装

2017年08月04日 ⁄ 综合 ⁄ 共 2470字 ⁄ 字号 评论关闭

背景:

Blei的hlda的C语言实现需要使用C语言的科学计算包GSL,因此决定安装。由于在windows下安装极其繁琐,先在Linux上安装之。

系统环境:

Linux version 2.6.35-22-generic (buildd@allspice) 
(gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu4) ) 
#33-Ubuntu SMP Sun Sep 19 20:32:27 UTC 2010

gsl安装过程

1, 下载 gsl-1.9.tar.gz
2, 安装
tar -zxvf gsl-1.9.tar.gz 
cd gsl-1.9
sudo ./configure
sudo make
sudo make install
执行最后一个命令之后会有很多现实,其中有:
Libraries have been installed in:
   /usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.

一个是libgslcblas.so,libgsl.so的位置很重要,编译后的文件被扔到了这里,比如 /usr/local/lib;另一个重要的位置是

gsl_rng.h 这样的头文件所在的文件夹目录,比如/usr/local/include/gsl。如果想要查找他们,可以使用find / -name gsl_rng.h这样的命令。
3,使用gsl
用gsl写一个例子文件gsl_test.c,这个文件来源于http://www.mas.ncl.ac.uk/~ndjw1/teaching/sim/gsl.html

#include <stdio.h>
#include <gsl_rng.h>
#include <gsl_randist.h>


int main (int argc, char *argv[])
{
  /* set up GSL RNG */
  gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
  /* end of GSL setup */


  int i,n;
  double gauss,gamma;


  n=atoi(argv[1]);
  for (i=0;i<n;i++)
    {
      gauss=gsl_ran_gaussian(r,2.0);
      gamma=gsl_ran_gamma(r,2.0,3.0);
      printf("%2.4f %2.4f\n", gauss,gamma);
    }
  return(0);
}

(1)第一反应是直接 gcc gsl_test.c

显示错误是:gsl_test.c:2: fatal error: gsl_rng.h: 没有那个文件或目录

(2)这个错误是说没有找到头文件,然后加上头文件的位置,gcc -I/usr/local/include/gsl gsl_test.c

显示错误是:gsl_test.c:(.text+0x12): undefined reference to `gsl_rng_mt19937' ...

(3)这个错误是说没有找到gsl_rng_mt19937的定义,查看了http://www.daniweb.com/software-development/cpp/threads/289812/cant-link-gsl-properly,这里说需要加上-lgsl,即链接到gsl,找到 libgsl.so.

使用命令:gcc -I/usr/local/include/gsl -lgsl gsl_test.c

(4)显示错误是://usr/local/lib/libgsl.so: undefined reference to `cblas_ctrmv'

查看了http://sourceware.org/ml/gsl-discuss/2003-q2/msg00123.html,是说错误是由于没有链接libgslcblas.so引起的,再加上-lgslcblas可以解决问题。

使用命令:gcc -I/usr/local/include/gsl -lgsl -lgslcblas gsl_test.c。

或者命令:gcc -I/usr/local/include/gsl -L/usr/local/lib -lgsl -lgslcblas gsl_test.c

这个时候出现了产出物a.out。

(5)运行a.out 

./a.out 出现错误“段错误”

(6)查看源文件,发现还需要输入参数

./a.out 10

结果是:
0.2678 6.9645
3.3488 1.6894
1.9950 2.1575
-4.7934 6.1648
-0.0782 4.0292
1.7871 11.6031
-2.5931 7.7629
0.3634 1.3344
-1.0965 11.1658
0.0142 3.5412

彻底搞定,done#

抱歉!评论已关闭.