c++ - 无法弄清楚为什么使用 boost 库的多精度计算对我不起作用

标签 c++ visual-studio boost

首先,我尝试使用 GMP,因为 boost 文档说它更快,但是 boost 库中缺少 gmp.h 文件,所以我不得不安装 GMP 库并复制 gmp.h。这样做之后,我在使用 mpz_int 时遇到了外部符号错误。 所以我决定尝试 cpp_int,从 boost 文档中复制示例并且它起作用了。这是我试过的:

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>

int main()
{
   using namespace boost::multiprecision;

   int128_t v = 1;

   // Do some fixed precision arithmetic:
   for(unsigned i = 1; i <= 20; ++i)
      v *= i;

   std::cout << v << std::endl; // prints 20!

   // Repeat at arbitrary precision:
   cpp_int u = 1;
   for(unsigned i = 1; i <= 100; ++i)
      u *= i;

   std::cout << u << std::endl; // prints 100!

   return 0;
}

然后我在 Math 类中创建了一个阶乘函数,但现在每次我使用 cpp_int 库中的变量时,我都会收到该错误: 错误 LNK2019:未解析的外部符号 __CrtDbgReportW 在函数“void __cdecl std::_Debug_message(wchar_t const *,wchar_t const *,unsigned int)”(?_Debug_message@std@@YAXPB_W0I@Z) 中引用

现在,每次我尝试为 cpp_int 变量分配一个新值时,我都会收到该错误,奇怪的是该示例有效,但现在同一示例不适用于该项目,但是如果我创建一个新项目并使用相同的 boost 库它再次工作。

最佳答案

很可能您正在使用的库之一(可能是 cpp_int 库)想要链接到 Visual Studio 运行时库的Debug 版本。 (符号 __CrtDbgReportW 仅在 VS 运行时库的 Debug 版本中定义。)

确保您针对适当的目标(调试/发布)编译您的代码,您使用的第三方库是针对同一目标编译的,并且您链接到相应的运行时库。

编辑(在您之前添加的评论之后):

确保为 Static Debug 版本的 VC 运行时库(aka libcpmtd.lib)编译代码:

在 Visual Studio 中,打开“项目属性”对话框,然后在 配置属性 -> C/C++ -> 代码生成 中,字段 Runtime Library,设置为:Multi-threaded Debug (/MTd)

请注意,您链接到构建的任何其他库必须具有相同的设置。

关于c++ - 无法弄清楚为什么使用 boost 库的多精度计算对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39578952/

相关文章:

c++ - __declspec 之前或之后的返回类型?

c++ - 如何修复 vs2013 上的 C3848 错误?

c++ - 帮助使用 boost relaxed heap

c++ - 将 filtering_istream 类型的变量转换为 ifstream 类型?

visual-studio - 如何完全禁用代码契约(Contract)?

c++ - 如何使用 Visual Studio 2010 在 Windows 上使用 Open MPI 构建 boost::mpi 库

c++ - 无法让线程在 C++ 中工作?

c++ - std::common_type 在 VC10 编译器下有 bug 吗?

Java 不会调用使用 swig 创建的 .dll 中的函数

c++ - 写入文件无法正常工作