gmp 5.0.1 windows 下編譯使用

來源:互聯網
上載者:User

編譯好的最新版gmp的dll 檔案下載

 在windows下使用gmp的動態庫,要注意到:

在gmp.info-1中有一段說明:
MINGW uses the C runtime library `msvcrt.dll' for I/O, so
     applications wanting to use the GMP I/O routines must be compiled
     with `cl /MD' to do the same.  If one of the other C runtime
     library choices provided by MS C is desired then the suggestion is
     to use the GMP string functions and confine I/O to the application.

所以在編譯的時候加一句/MD 即可(預設使用msvcrt.dll作為運行庫),當然這對於vc有效。

啟用 /MD 時VC6,VS2008,vs2010 分別對應msvcrt.dll、  msvcr80.dll  、MSVCR100.dll。
所以經常看到用vc6.0以上版本的開發軟體(涉及運行庫),會順便捎上***.dll 庫。
涉及到FILE檔案操作時,使用vc6加上/MD會運行很順利,如果是vs系列進行調用,最好不要用FILE相關的gmp函數,崩潰難免。

可以做個替換:

對於mpz_out_str 使用的解決方案:使用 mpz_get_str()
//替換mpz_out_str(stdout,36,r);
//替換mpz_out_str(f,36,r);
FILE *f=fopen("a.txt","w");
mpz_t r;
int i;
char ; //或用malloc分配空間
.....
//並初始化qq[100]為0 ...
mpz_get_str(qq,36,r);//The base may vary from 2 to 36
printf("%s",qq); //console顯示輸出,替換mpz_out_str(stdout,36,r);
i=0;
while(qq!=0)
     fputc(qq[i ],f);//寫入檔案 ,替換mpz_out_str(f,36,r);
fclose(f);
//-------------------------------------------------------------------

或者參考:http://gmplib.org/list-archives/gmp-discuss/2004-May/001171.html

 

修改wayne的gmp測試碼:

//******************************************

        #include <stdio.h>
        #include "gmp.h"
        #pragma  comment(lib,"libgmp.lib")
        int main (void)
       {
           FILE *f=fopen("a.txt","w");
           int num=50,i;
          
           mpz_t n,m,c,r;
           char qq[100]; //或用malloc分配空間,並初始化為0
           mpf_t pi,e,xx,yy;//**********
           mpq_t x,y,z;
          
           for(i=0;i<100;i++)
               qq[i]='/0';
           mpz_init_set_str (n, "13703703579", 10);
           mpz_init_set_str (m, "29012345415", 10);         
           mpz_inits(c,r,'/0');
           mpz_gcd(c,n,m);
           mpz_fac_ui(r,num);            
  
           gmp_printf ("gcd(%Zd,%Zd) = %Zd/n/n%d! =%Zd:/n in base 36:/t",n,m,c,num,r);
//***************************
           //替換mpz_out_str(stdout,36,r);
           //替換mpz_out_str(f,36,r);
           mpz_get_str(qq,36,r);//The base may vary from 2 to 36
           printf("%s",qq);//console顯示輸出
           i=0;
           while(qq[i]!=0)
               fputc(qq[i++],f);//寫入檔案
           fclose(f);
//***************************

           mpz_fib_ui(r,num);
           gmp_printf("/n/nthe %dth fibonacci number is:/n %Zd/n",num,r);
           mpz_bin_ui(c,n,num);
           gmp_printf("/nBinomial number C(%Zd,%d) is:/n%Zd/n",n,num,c);
           mpz_pow_ui(c,n,num);
           gmp_printf("/n%Zd^%d =:/n%Zd/n",n,num,c);
          
           mpf_set_default_prec(500);
          
           mpf_init_set_str(pi,"3.14159265358979323846264338327950288419716939937510582097494459230781",10);

           mpf_init_set_str(e ,"2.71828182845904523536028747135266249775724709369995957496696762772407",10);
           mpf_inits(xx,yy,'/0');
           mpf_pow_ui(xx,pi,4);mpf_pow_ui(yy,pi,5);
           mpf_add(xx,xx,yy);
           mpf_pow_ui(yy,e,6);
           gmp_printf("/npi^4+pi^5=%.50Ff/ne^6/t =%.50Ff/n/ninput a number to compute its next prime,",xx,yy);
           gmp_printf("arbitraryly large:/n");
           gmp_scanf("%Zd",c);
           mpz_nextprime(r,c);
           gmp_printf("after %Zd,closely followed a prime:/n%Zd/n/n",c,r);
                        
         
           mpq_inits(x,y,z,'/0');
           gmp_printf("you input two fractions with a plus sign(2/4+1/3),I compute their sum:/n");
           gmp_scanf ("%Qd + %Qd",x,y);
           mpq_canonicalize(x);
           mpq_canonicalize(y);
           mpq_add(z,x,y);
           gmp_printf("/nactually,after reduction,your equation is:/n%Qd+%Qd = %Qd/nGoodbye!/n",x,y,z);
            
           mpz_clears(m,n,c,r,'/0');
           mpq_clears(x,y,z,'/0');
           mpf_clears(xx,yy,'/0');

       return 0;
     }

 

調用後的結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.