简体   繁体   中英

How do I use boost with the xeus-cling jupyter kernel?

I'm running Pop._OS (ubuntu derivative) and apt installed boost (libboost-all-dev) from the default repositories. I know it has installed properly since I can compile and run the simple boost odeint example with GCC.

However when I tried to run the same example inside a jupyter notebook using the zeus-cling kernel I got an error while including the odeint header. I can recreate the error while executing this code:

#pragma cling add_include_path("/usr/include")
#include <boost/numeric/odeint.hpp>

The error message I get is:

In file included from input_line_8:1:
In file included from /usr/include/boost/numeric/odeint.hpp:22:
In file included from /usr/include/boost/numeric/odeint/config.hpp:44:
In file included from /usr/include/boost/config.hpp:48:
In file included from /usr/include/boost/config/stdlib/libstdcpp3.hpp:78:
/usr/include/unistd.h:756:28: error: expected function body after function declarator
extern __pid_t fork (void) __THROWNL;
                           ^
/usr/include/unistd.h:869:11: fatal error: 'bits/getopt_posix.h' file not found
# include <bits/getopt_posix.h>
          ^~~~~~~~~~~~~~~~~~~~~
Interpreter Error: 

From what I understand bits/getopt_posix.h is a GCC only header, thus I think the problem might be because the boost headers are configuring themselves as if they are compiling under GCC instead of cling/clang.

So, how do I properly include boost in a xeus-cling jupyter notebook?

I was getting desperate which ended up in the following solution to my problem.

So what I did is clone the boost super project as a submodule into my repository and then added all include paths required to get boost::numeric::odeint working. Odeint is header only anyways, so it works. This should work for all header only boost libraries.

My guess is that this is not the proper way, but it works for me. It's just for prototyping and small experiments later on.

Here is the harmonic oscilator example.

#pragma cling add_include_path("../lib/boost/libs/numeric/odeint/include")
#pragma cling add_include_path("../lib/boost/libs/numeric/ublas/include")
#pragma cling add_include_path("../lib/boost/libs/config/include")
#pragma cling add_include_path("../lib/boost/libs/type_traits/include")
#pragma cling add_include_path("../lib/boost/libs/serialization/include")
#pragma cling add_include_path("../lib/boost/libs/core/include")
#pragma cling add_include_path("../lib/boost/libs/preprocessor/include")
#pragma cling add_include_path("../lib/boost/libs/static_assert/include")
#pragma cling add_include_path("../lib/boost/libs/mpl/include")
#pragma cling add_include_path("../lib/boost/libs/utility/include")
#pragma cling add_include_path("../lib/boost/libs/typeof/include")
#pragma cling add_include_path("../lib/boost/libs/array/include")
#pragma cling add_include_path("../lib/boost/libs/assert/include")
#pragma cling add_include_path("../lib/boost/libs/throw_exception/include")
#pragma cling add_include_path("../lib/boost/libs/units/include")
#pragma cling add_include_path("../lib/boost/libs/integer/include")
#pragma cling add_include_path("../lib/boost/libs/fusion/include")
#pragma cling add_include_path("../lib/boost/libs/range/include")
#pragma cling add_include_path("../lib/boost/libs/iterator/include")
#pragma cling add_include_path("../lib/boost/libs/concept_check/include")
#pragma cling add_include_path("../lib/boost/libs/detail/include")
#pragma cling add_include_path("../lib/boost/libs/function_types/include")
#pragma cling add_include_path("../lib/boost/libs/predef/include")
#pragma cling add_include_path("../lib/boost/libs/math/include")
#pragma cling add_include_path("../lib/boost/libs/lexical_cast/include")
#pragma cling add_include_path("../lib/boost/libs/numeric/conversion/include")
#pragma cling add_include_path("../lib/boost/libs/container/include")
#pragma cling add_include_path("../lib/boost/libs/move/include")
#pragma cling add_include_path("../lib/boost/libs/smart_ptr/include")
#pragma cling add_include_path("../lib/boost/libs/multi_array/include")
#pragma cling add_include_path("../lib/boost/libs/functional/include")
#pragma cling add_include_path("../lib/boost/libs/function/include")
#pragma cling add_include_path("../lib/boost/libs/type_index/include")
#pragma cling add_include_path("../lib/boost/libs/container_hash/include")
#pragma cling add_include_path("../lib/boost/libs/bind/include")


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

typedef std::vector< double > state_type;
const double gam = 0.15;

void harmonic_oscillator(const state_type &x , state_type &dxdt , const double t)
{
    dxdt[0] = x[1];
    dxdt[1] = -x[0] - gam*x[1];
}

state_type x(2);
x[0] = 1.0; // start at x=1.0, p=0.0
x[1] = 0.0;

size_t steps = boost::numeric::odeint::integrate(harmonic_oscillator,
                                                 x , 0.0 , 10.0 , 0.1 );

std::cout << steps << std::endl;

Ηi there,

I ve been using boost (geometry) with xeus-cling kernel without any issues, so far. I install boost from conda forge (conda install boost -c conda-forge). I tried to include odeint.hpp without any issues on my cling environment:

在此处输入图像描述

ΒW Giorgos

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM