• GSL+DevC++使用


    在DEV C++中配置GSL1.8库
    前面写了如何在vs2005中添加gsl,本文所所述为在dev c++中使用gsl库,由实践总结而得。
    准备软件:
    1、Orwell Dev C++ 5.6.2 No Compiler Setup.exe(devc++的社区升级版,很不错的)
    2、gsl-1.8.exe
    3、TDM-GCC4.7.1-2.exe,安装后,目录同样名为mingw32(也可以安装mingw版)


    步骤如下:
    1、安装完以上3个软件。
    2、将 gsl 安装目录下的 bin 下 libgsl.dll,libgslcblas.dll 复制到mingw32的bin目录中,lib 下 ibgsl.a,libgslcblas.a 复制到 mingw32目录下的 lib 目录下;include 下的整个 gsl 文件夹复制到mingw32目录下的 include 目录下。 
    3、 打开 dev-c++,工具-编译选项-编译器,选上“在连接器命令行加入如下命令”,加入-lgsl -lgslcblas(中间有空格,即在连接时连上 libgsl.a,libgslcblas.a,gcc 可以自动识别前缀 lib 和后缀.a)


    此时在用Dev C++打开一个gsl的c文件,如下所示:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <gsl/gsl_rng.h>
    #include <gsl/gsl_randist.h>
    
    #define MAXRNDNUM 100
    
    int main(int argc, char * argv[])
    {
        const gsl_rng_type *T; // 随机数生成器类型指针
        gsl_rng *r; // 随机数生成器指针
    
        int i;
        double u; // 随机数变量
    
        const double erlang_a=0.6; // lambda=0.6
        const double erlang_n=2.0; // n=2.0
    
        // gsl_rng_default (gsl_rng_mt19937)
        // gsl_rng_ranlxs0, gsl_rng_ranlxs1, gsl_rng_ranlxs2
        // gsl_rng_ranlxd1, gsl_rng_ranlxd2
        T = gsl_rng_ranlxs0;
    
        gsl_rng_default_seed = ((unsigned long)(time(NULL))); // 取当前时间作为种子
        r = gsl_rng_alloc(T); // 创建随机数生成器实例
    
        for (i=0; i<MAXRNDNUM; i++)
        {
            /** Functions:
             * double gsl_ran_erlang (const gsl_rng * r, const double a, const double n)
             * double gsl_ran_erlang_pdf (const double x, const double a, const double n)
              */
            u = gsl_ran_erlang(r, erlang_a, erlang_n); // 生成服从 Erlang 分布的随机数
            printf("%.5f
    ", u);
        }
    
        // 打印 0.0, 1.0 处的概率密度值
        printf("erlang_pdf(0.0)=%.5f
    ", gsl_ran_erlang_pdf(0.0, erlang_a, erlang_n));
        printf("erlang_pdf(1.0)=%.5f
    ", gsl_ran_erlang_pdf(1.0, erlang_a, erlang_n));
    
        gsl_rng_free(r); // 释放随机数生成器
    
        return 0;
    }
    

    点击 compile &run,即可运行,编译的命令如下:
    General: mingw5
    Executing gcc.exe...
    gcc.exe "F:GSLexample_practiceserlang_demo.c" -o "F:GSLexample_practiceserlang_demo.exe" -m32 -g3 -lgsl -lgslcblas -I"C:MinGW32include" -L"C:MinGW32lib" -m32 -g3
    Compilation succeeded in 0.33 seconds

    运行结果:


    0.90501
    1.34102
    0.53111
    2.18400
    1.10534
    1.48317
    0.59413
    0.56080
    0.10496
    0.65189
    1.21032
    4.07168
    0.71350
    0.62467
    1.78255
    0.35549
    1.08990
    0.84559
    3.24031
    1.62870
    2.33040
    0.82586
    1.34858
    1.07816
    0.71486
    0.58534
    1.69676
    0.36377
    1.39621
    2.26477
    1.46325
    1.09332
    0.45466
    3.76635
    0.61206
    0.49410
    1.08605
    1.59985
    1.04695
    0.59166
    0.09401
    0.31422
    0.72572
    1.37264
    3.60675
    0.46318
    0.70602
    2.92091
    3.14072
    2.50999
    0.38493
    1.45044
    1.23082
    2.37491
    2.02381
    1.57281
    1.45119
    0.73306
    1.09668
    0.37983
    0.74744
    1.84946
    0.62742
    1.09751
    0.44519
    1.20147
    0.28054
    0.67864
    1.37922
    0.68963
    0.77507
    0.16112
    2.46477
    0.35400
    3.24018
    2.16743
    0.52736
    0.34015
    3.33357
    2.25478
    1.69646
    0.49001
    0.60242
    1.52576
    1.34437
    2.51762
    3.05635
    1.18719
    1.27544
    2.63845
    2.04897
    0.63952
    0.87056
    2.83859
    0.69683
    0.50104
    0.91714
    1.17773
    1.72386
    1.30541
    erlang_pdf(0.0)=0.00000
    erlang_pdf(1.0)=0.52465
    
    --------------------------------
    Process exited with return value 0
    Press any key to continue . . .
    

    若要是在devc++中建立了一个gsl的dev的工程,那么还需要做一下步骤,可参考如下:
    1.Open Dev C++ and create a new project 
    2.Click Project->Project Options menu
    3.Goto Directories tab
    4.Goto "Include Directories" sub-tab
    5.Enter C:Program FilesGnuWin32include and press Add button
    6.Goto Parameters tab
    7.Click on button "Add library or object"
    8.Navigate and select C:Program FilesGnuWin32liblibgsl.a
    9.Click on button "Add library or object"
    10.Navigate and select C:Program FilesGnuWin32liblibgslcblas.a
    9.Click on button "Add library or object"
    10.Navigate and select C:Program FilesGnuWin32liblibgslcblas.dll.a
    11.Add the path C:Program FilesGnuWin32in to system runtime, or copy the dlls libgsl.dll and libgslcblas.dll to the location where you want to run your .exe
  • 相关阅读:
    爬虫流程
    康哥笔记
    csdn笔记
    数据库多表联查
    完整数据恢复
    Linux安装mysql
    linux在vm下实现桥接模式
    Linux下ntpdate时间同步
    spark集群在执行任务出现nitial job has not accepted any resources; check your cluster UI to ensure that worker
    flume在启动时出现brokerList must contain at least one Kafka broker
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007496.html
Copyright © 2020-2023  润新知