7

I'm sorta new to C++ and I've decided to try and use odeint to do some simulations because python is too slow for my needs.

I found this package, which I want to play with. I'm just not totally sure how to install or where to place these libraries. Is there something for C++ similar to python's pip install?

Side note: I'm trying to do this using Eclipse Kepler, but I'm not married to that idea.

Matt
  • 3,508
  • 6
  • 38
  • 66
  • 1
    Essentially, to use a third party library you will have to tell the compiler where to find the headers for including, and where to find the libraries for linking. Sometimes you also have to get certain compiler flags right. How this is accomplished varies greatly between OSs and IDEs. On Linux often the easiest way is to install the library through the package manager, in which case the compiler can find the files in the system's default locations. Since you seem to be on OSX, maybe you could install the package with macports or something similar? – MB-F Feb 21 '14 at 18:46

2 Answers2

18

I recommend not putting the code into your own project - that is a rather quick and dirty solution. The correct way to use a library in C++ (in fact, in any programming language that I know) is to keep all libraries separate from your own projects, at a separate location on your filesystem.

You then tell your environment where to find the library files and tell your project to use them. It's always the same basic idea, whether you are using Makefiles or Visual Studio project files.

Look at the documentation of this library. It says:

odeint is a header-only library, no linking against pre-compiled code is required

This means that the "library files" I just mentioned are just header files. That makes it much easier for you, because you don't have to deal with linker options. In C++, the location where additional (project-external) header files can be found is usually called the "include path".

Your new problem should therefore be: How to tell Eclipse Kepler my include path?

Entering this new problem into Google (as "eclipse kepler include path") yields a few interesting results. It will eventually lead you to the Eclipse documentation about include paths, where you can learn how to edit the C++ include path.

Now that everything is set up, you can finally use the library's header files in your projects via lines like the following:

#include <boost/numeric/odeint.hpp>

Do you notice the < >? They make a big difference, because they are the C++ way of saying "this is not part of my project, please get it from my include path". Just like headers which are part of the language (e.g. <vector> or <iostream>).

All of this may appear troublesome at first, and perhaps you even gain little from it at the beginning, but in the long run, for many different projects and many different libraries, it's the only way to prevent chaos.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • Ah, that makes sense...I think. So what I'd do is `#include`. Is that the right way to look at it? – Matt Feb 21 '14 at 19:35
  • Almost! :) Your include path should point to the parent directory of "boost", so that the line in your code is just #include . – Christian Hackl Feb 21 '14 at 19:38
  • The idea is that, for example, I could just take all your project's code and build it as it is on my own machine (where I probably have installed the library at a different location, even using a completely different compiler and operating system). – Christian Hackl Feb 21 '14 at 19:39
  • I think I'm on the same page, I just mistyped, I meant `#include`, right? – Matt Feb 21 '14 at 19:42
  • Nono, without the "pathtoBOOSTfile" part. Just #include . No more, no less. Look at the example linked in the library's documentation: https://github.com/headmyshoulder/odeint-v2/blob/master/libs/numeric/odeint/examples/harmonic_oscillator.cpp. Your goal is to set up Eclipse such that the compiler will find the file when you write the #include exactly like that. Or perhaps I'm misunderstanding your question... – Christian Hackl Feb 21 '14 at 19:48
  • P.S.: "odeint.hpp" probably includes a lot of other files automatically for you, so you don't need to include them all separately. Is this what you were concerned about? – Christian Hackl Feb 21 '14 at 19:50
  • No, I misunderstood what you wrote. I was just confused slightly because I would think the compiler would need to know where those files would be kept. So, for instance, if I had `boost` on my `Desktop` but some other header file somewhere else wouldn't I need to tell my compiler somewhere? I understand the Eclipse part, but what if I just did this in a light editor like `vim` or `Emacs`? – Matt Feb 21 '14 at 19:57
  • Surely somewhere the `path` needs to be specified, right? – Matt Feb 21 '14 at 19:58
  • Indeed, the path needs to be specified. But the point is that the path should not just be in your project but be more "global". Google for "gcc CPLUS_INCLUDE_PATH"; it will give you an idea of how this can be done. The idea is always that you give this information globally to your compiler and not individually to every project which may need it. When the compiler then compiles a C++ file and encounters an #include <...> line, it looks at its global include-path information (wherever and however that may be stored). This is exactly how #include <...> differs from #include "...". – Christian Hackl Feb 21 '14 at 20:24
  • Just a small sidenode: odeint shipes with boost which is also needed if you want to use odeint. If you install boost version >= 1.53 odeint is already present and it should be usable without specifying any include path. Maybe installing boost version 1.53 can be done from your package manager. – headmyshoulder Feb 22 '14 at 07:09
0

Since odeint is a header only library you can place it with your own source code. Simply copy odeint's boost directory where your main.cpp is (assuming you have a main.cpp, but you should get the idea):

your_sources/
    main.cpp
    boost/
        numeric/
            odeint/
            odeint.hpp

Now you can use the library by including

#include "boost/numeric/odeint.hpp"
MB-F
  • 22,770
  • 4
  • 61
  • 116
  • Okay, I get it. My path (at least where I can see the other 'normal' libraries) `/usr/lib/c++/v1`. I'll just place the directory in there. Does that about sum it up? I was hoping there would be a more automated way to do it, but this is fine. – Matt Feb 21 '14 at 19:04
  • Yep, you can do that. But it's usually not a good idea to manually place libraries in the system because they might interfere with the package manager. (I don't know about OSX, though) – MB-F Feb 21 '14 at 19:09