linux下GSL安装
背景:
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.
2, 安装
tar -zxvf gsl-1. 
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
linux完全安装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/f'
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,这个文件来源于/~ndjw1/teaching/sim/gsl.html
[cpp] view plain copy
1.#include <stdio.h> 
2.#include <gsl_rng.h> 
3.#include <gsl_randist.h> 
4.
5.
6.int main (int argc, char *argv[]) 
7.
8./* set up GSL RNG */ 
9.gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); 
10./* end of GSL setup */ 
11.
12.
13.int i,n; 
14.double gauss,gamma; 
15.
16.
17.n=atoi(argv[1]); 
18.for (i=0;i<n;i++) 
19.
20.gauss=gsl_ran_gaussian(r,2.0); 
21.gamma=gsl_ran_gamma(r,2.0,3.0); 
22.printf("%2.4f %2.4f\n", gauss,gamma); 
23.
24.return(0); 
25.
(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的定义,查看了/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'
查看了/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#