在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);
//-------------------------------------------------------------------

本文介绍了在Windows环境下编译GMP 5.0.1动态库的方法,特别指出需使用/MD选项链接msvcrt.dll。同时,由于GMP的FILE操作在不同VC版本中可能引发问题,建议避免使用FILE相关的GMP函数,可以使用mpz_get_str()替代mpz_out_str()进行输出。提供了一个示例代码展示如何替换并成功运行。
最低0.47元/天 解锁文章
1961





