前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GSL+DevC++使用

GSL+DevC++使用

作者头像
用户4645519
发布2020-09-07 10:50:54
1.1K0
发布2020-09-07 10:50:54
举报
文章被收录于专栏:嵌入式学习

在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文件,如下所示:

代码语言:javascript
复制
#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\n", u);
    }

    // 打印 0.0, 1.0 处的概率密度值
    printf("erlang_pdf(0.0)=%.5f\n", gsl_ran_erlang_pdf(0.0, erlang_a, erlang_n));
    printf("erlang_pdf(1.0)=%.5f\n", 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:\GSL\example_practices\erlang_demo.c" -o "F:\GSL\example_practices\erlang_demo.exe" -m32 -g3 -lgsl -lgslcblas -I"C:\MinGW32\include" -L"C:\MinGW32\lib" -m32 -g3 Compilation succeeded in 0.33 seconds

运行结果:

代码语言:javascript
复制
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 Files\GnuWin32\include and press Add button 6.Goto Parameters tab 7.Click on button "Add library or object" 8.Navigate and select C:\Program Files\GnuWin32\lib\libgsl.a 9.Click on button "Add library or object" 10.Navigate and select C:\Program Files\GnuWin32\lib\libgslcblas.a 9.Click on button "Add library or object" 10.Navigate and select C:\Program Files\GnuWin32\lib\libgslcblas.dll.a 11.Add the path C:\Program Files\GnuWin32\bin to system runtime, or copy the dlls libgsl.dll and libgslcblas.dll to the location where you want to run your .exe

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014/04/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档