无法构建 C++ 源代码(由于库?)

Cannot build C++ source (due to libraries?)

为了了解 C++ 如何求解微分方程,我尝试了在互联网教程中找到的以下脚本。它应该可以工作。

我不直接使用C++。我使用 R 和 Rcpp 库来 compile/run 脚本。这是脚本:

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

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


/* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
* with initial condition x(1) = 0.
* Analytic solution is x(t) = sqrt(t) - 1/t
*/

void rhs( const double x , double &dxdt , const double t )
{
  dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
}

void write_cout( const double &x , const double t )
{
  cout << t << '\t' << x << endl;
}

// state_type = double
typedef runge_kutta_dopri5< double > stepper_type;

int main()
{
  double x = 0.0;    
  integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type () ) ,rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
 }

出现以下错误:

ex1.cpp:2:36: fatal error: boost/numeric/odeint.hpp: No such file or directory
 #include <boost/numeric/odeint.hpp>
                                    ^
compilation terminated.
make: *** [ex1.o] Error 1

我应该在 运行 我的脚本之前 download/load 库吗?怎么做到的?

编辑: 我正在使用 Windows 并且我已经安装了 Rtools。我真的是 Rcpp 的新手。我想知道的是我是否必须安装一些东西,比如boost库,以及我应该安装它的位置。

请安装 boost headers。这不是图书馆的问题。

使用包管理器*或从 boost.org 下载 boost 解压并将 header 目录 <boost-location>/boost 添加到您的包含目录 -I.

* F.e。在 Ubuntu f.e 上。 sudo apt -y install libboost1.65-dev

Anthony Hauser,忘了反对票吧。试试我的建议,你会没事的。

假设您将其解压到 /home/anthony/include 然后添加 -I /home/anthony/include/boost-1.65 或在 Windows C:\Users\Anthony\Boost 添加 -I C:\Users\Anthony\Boost\boost-1.65

您有多个 严重错误:

  • 您想从 R 调用它,但没有 Rcpp 接口
  • 您从 R 调用的代码不能有 main()
  • 无 Boost headers:您需要安装 BH 软件包

更正了下面的代码。

代码

这个版本的代码可以运行示例。我在其中添加了一些评论。

// include Rcpp, it takes care of most other headers you need
#include <Rcpp.h>

// include Boost's odeint
#include <boost/numeric/odeint.hpp>

// tell R you need Boost
// [[Rcpp::depends(BH)]]

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


/* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
* with initial condition x(1) = 0.
* Analytic solution is x(t) = sqrt(t) - 1/t
*/

void rhs( const double x , double &dxdt , const double t ) {
  dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
}

void write_cout( const double &x , const double t ) {
  // use Rcpp's stream
  Rcpp::Rcout << t << '\t' << x << endl;
}

// state_type = double
typedef runge_kutta_dopri5< double > stepper_type;

// call this from R
// [[Rcpp::export]]
bool boostExample() {
  double x = 0.0;    
  integrate_adaptive(make_controlled( 1E-12 , 1E-12 , stepper_type () ) ,
                     rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
  return true;
}

/*** R
boostExample()                  // call from R
*/

演示

由于最后的 R 片段,运行 sourceCpp() 在此编译,link,加载还运行新的 boostExample() 函数:

R> sourceCpp("/tmp/sobh.cpp")

R> boostExample()                  
1   0
1.00843 0.0125618
1.0197  0.0291282
1.03098 0.0454238
1.04226 0.061457
1.05409 0.0779974
1.06591 0.0942666
1.07774 0.110273
1.09015 0.126803
1.10257 0.14306
1.11498 0.159055
1.12802 0.175574
1.14106 0.191821
1.15409 0.207804
1.16778 0.224315
1.18147 0.240553
1.19516 0.256527
1.20954 0.273032
1.22392 0.289264
1.2383  0.305233
1.25341 0.321735
1.26852 0.337966
1.28414 0.354468
1.29976 0.370699
1.31591 0.387205
1.33207 0.403439
1.34877 0.419952
1.36548 0.436192
1.38276 0.452714
1.40004 0.468964
1.41791 0.485498
1.43579 0.501761
1.45428 0.518311
1.47278 0.534589
1.49191 0.551159
1.51105 0.567457
1.53086 0.58405
1.55067 0.600373
1.57117 0.616993
1.59167 0.633343
1.6129  0.649996
1.63412 0.666378
1.6561  0.683067
1.67808 0.699487
1.70084 0.716217
1.7236  0.732678
1.74718 0.749455
1.77075 0.765963
1.79517 0.782792
1.8196  0.799352
1.8449  0.816238
1.87021 0.832857
1.89643 0.849806
1.92266 0.866487
1.94984 0.883505
1.97702 0.900256
2.0052  0.91735
2.03338 0.934176
2.0626  0.951351
2.09182 0.96826
2.12212 0.985523
2.15242 1.00252
2.18385 1.01988
2.21528 1.03697
2.24789 1.05443
2.28049 1.07163
2.31432 1.0892
2.34816 1.1065
2.38327 1.12419
2.41838 1.14161
2.45482 1.15943
2.49127 1.17697
2.5291  1.19492
2.56694 1.2126
2.60622 1.23068
2.64551 1.2485
2.68631 1.26674
2.72711 1.28471
2.7695  1.3031
2.81188 1.32123
2.85591 1.33979
2.89994 1.35809
2.9457  1.37682
2.99145 1.39529
3.039   1.41422
3.08655 1.43287
3.13598 1.45199
3.18541 1.47084
3.23679 1.49016
3.28818 1.50921
3.34162 1.52875
3.39505 1.54802
3.45062 1.56778
3.50619 1.58727
3.56399 1.60727
3.6218  1.627
3.68193 1.64724
3.74206 1.66721
3.80463 1.68771
3.8672  1.70794
3.93231 1.7287
3.99743 1.7492
4.0652  1.77024
4.13297 1.79101
4.20352 1.81235
4.27406 1.83341
4.34751 1.85505
4.42096 1.87641
4.49744 1.89837
4.57392 1.92004
4.65356 1.94232
4.7332  1.96432
4.81615 1.98694
4.89911 2.00927
4.98551 2.03225
5.07192 2.05493
5.16194 2.07826
5.25196 2.10131
5.34575 2.12502
5.43953 2.14844
5.53726 2.17254
5.63499 2.19635
5.73684 2.22086
5.83868 2.24507
5.94483 2.26999
6.05097 2.29461
6.16161 2.31996
6.27224 2.34501
6.38757 2.37081
6.50289 2.3963
6.62311 2.42255
6.74334 2.4485
6.86867 2.47523
6.99401 2.50164
7.12469 2.52885
7.25537 2.55575
7.39162 2.58347
7.52788 2.61086
7.66995 2.63909
7.81203 2.66699
7.96018 2.69575
8.10834 2.72418
8.26284 2.75349
8.41733 2.78246
8.57845 2.81233
8.73957 2.84185
8.9076  2.8723
9.07562 2.90239
9.25086 2.93342
9.4261  2.96411
9.60886 2.99575
9.79162 3.02703
9.98222 3.05929
10  3.06228
[1] TRUE
R>