【问题标题】:Canot compile C++ which uses odeint from boost无法编译使用 boost 中的 odeint 的 C++
【发布时间】:2013-11-09 11:44:21
【问题描述】:

我使用的是 Ubuntu 12.04,并且在 /usr/include 中已经有了一些提升文件。我做了一个

sudo apt-get install libboost-all-dev

这也安装了很多文件。我不想删除这个 boost 并从源代码安装,因为其他几个包依赖于 ubuntu repos 的版本。这是我要运行的示例代码:-

#include <iostream>
#include <boost/numeric/odeint.hpp>



using namespace std;
using namespace boost::numeric::odeint;

typedef vector< double > state_type;

const double sigma = 10.0;
const double R = 28.0;
const double b = 8.0 / 3.0;

void lorenz( state_type &x , state_type &dxdt , double t )
{
    dxdt[0] = sigma * ( x[1] - x[0] );
    dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
    dxdt[2] = x[0]*x[1] - b * x[2];
}

int main()
{
    const double dt = 0.01;

    state_type x(3);
    x[0] = 1.0 ;
    x[1] = 0.0 ;
    x[2] = 0.0;
    stepper_euler< state_type > stepper;
    stepper.adjust_size( x );

    double t = 0.0;
    for( size_t oi=0 ; oi<10000 ; ++oi,t+=dt )
    {
        stepper.do_step( lorenz , x , t , dt );
        cout << x[0] << " " << x[1] << " " << x[2] << endl;
    }
}

先编译g++ -o test test.cpp,报错 /usr/include/boost/numeric/odeint.hpp permission denied

所以我使用递归更改了所有odeint文件的文件权限

sudo chmod -R +x odeint/

这一次,它没有说权限被拒绝,而是抛出了 400 行错误,如下所示 -> error log from terminal

如何编译它?文档或其他任何地方都没有 odeint 的安装指南

【问题讨论】:

  • 首先:文件存在吗?
  • 是的,所有必需的文件都存在
  • 难道 odeint 与 ubuntu repos 的 boost 版本不兼容,除了安装默认提供 odeint 的源代码的最新 boost 之外别无选择?
  • 然后从日志中列出的第一个错误开始:/usr/include/boost/numeric/odeint/stepper/controlled_runge_kutta.hpp:307:42: error: ‘bind’ is not a member of ‘boost::numeric::odeint::detail’。确保它实际上是第一个。您可以使用-Wfatal-errors 编译以在第一个错误时中止。
  • @stefan 带有 -Wfatal-errors 标志,它给出:- '在来自 /usr/include/boost/numeric/odeint/stepper/base/explicit_stepper_base.hpp:25:0 的文件中,来自/usr/include/boost/numeric/odeint/stepper/euler.hpp:23,来自 /usr/include/boost/numeric/odeint.hpp:27,来自 test.cpp:2:/usr/include/boost/numeric /odeint/util/bind.hpp:44:14: 错误:'std::bind' 尚未声明编译因 -Wfatal-errors'而终止

标签: c++ ubuntu boost odeint


【解决方案1】:

boost的这部分似乎使用了C++11的特性。因此,您需要在编译器调用中添加-std=c++0x-std=c++11

随后的错误test.cpp: In function ‘int main()’: test.cpp:30:5: error: ‘stepper_euler’ was not declared in this scope 将您指向另一个错误来源:您忘记包含声明stepper_euler 的文件。将适当的#include &lt;file&gt; 放在代码的开头。

【讨论】:

  • @headmyshoulder cmets 中提供的 OP 的编译器错误提示其他情况。它似乎使用std::bind,它是 C++11。
  • 我确信它可以在没有 C++11 的情况下工作(我是作者之一)。有预处理器宏检查是否启用了 C++11。如果是,则使用 std::bind,其他 boost::bind。
  • @headmyshould 使用std::bind 可能是用户的错(由于缺少-std=c++0x 而无法使用),但从OP 显示的内容来看,这似乎不太可能。我怀疑决定boost::bind 的逻辑存在错误。
  • headmyshould & /stefan,两者都是对的。 odeint 与 ubuntu repos 上的 boost ver[1.42] 不兼容。我从 sourceforge 获得了最新的提升并构建了整个东西。然后按照 stefan 的建议进行编译。正如@headmyshould 正确指出的那样,发布的示例是较旧的 odeint 版本。g++ -o example -std=c++0x -Wfatal-errors example.cpp 让 NVIDIA 的 CUDA 编译器运行有点棘手,nvcc -o example -Xcompiler "-std=c++0x" example.cpp C++11 启用是必要的,AFAIK 仅适用于 CUDA 工具包 5.5 .感谢您的帮助人们:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多