简体   繁体   中英

How to use Boost.Python in Eclipse running on Linux Mint 18?

The objective is to compile a C++ program with Boost.Python in Eclipse on the OS on Linux Mint 18. In particularly, we want to run the template program:

#include<boost/version.hpp>
#include<boost/python.hpp>
#include<iostream>

using namespace std;

int main() {
    cout << "My first boost program with version: " << BOOST_LIB_VERSION << endl;
    return 0;
}

This personally took me quite a while to get working, so the tutorial below should serve as a one-stop-shop reference for anyone in a similar situation. Note on software used:

  • Boost version: 1.62.0
  • Python version: 2.7.12
  • OS: Linux Mint 18 Xfce 64-bit
  • Eclipse version: CDT 9.1.0 for Eclipse Neon.1

Installing Eclipse CDT

  1. Make sure that you have Java installed. For the Java JDK (if you want to also develop Java programs), run:

     sudo apt-get install default-jdk 

    To simply run Java programs (such as Eclipse), you can install the lighter Java JRE:

     sudo apt-get install default-jre 

    Both JDK and JRE will work, but JDK takes up more space.

  2. Go to CDT download webpage and download the Linux 64-bit file eclipse-cpp-neon-1-linux-gtk-x86_64.tar.gz

  3. Assuming you downloaded the file in ~/Downloads/ , run the following command to unpack ("install") Eclipse into the /opt/ directory:

     sudo tar zxvf ~/Downloads/eclipse-cpp-neon-1-linux-gtk-x86_64.tar.gz -C /opt/ 
  4. To create a desktop launcher icon, run

     sudo gedit ~/.local/share/applications/eclipse.desktop 

    and in the text editor copy-paste:

     [Desktop Entry] Name=Eclipse Type=Application Exec=/opt/eclipse/eclipse Terminal=false Icon=/opt/eclipse/icon.xpm Comment=Integrated Development Environment NoDisplay=false Categories=Development;IDE; Name[en]=Eclipse 

You now have Eclipse installed, but you don't need to run it yet, just keep reading and following the instructions :)

Installing Boost

The instructions are more or less given here , but for the sake of cohesion I write them here.

  1. Download boost_1_62_0.tar.gz
  2. Assuming you downloaded the file into ~/Downloads/ , run the following command to unpack Boost into the /usr/include/ directory:

     sudo tar zxvf ~/Downloads/boost_1_62_0.tar.gz -C /usr/include/ 

It's as easy as that - you now have Boost... but that's not all yet. Read on :)

Building Boost.Python and Boost.System

The Boost libraries Boost.Python and Boost.System must be built before they can be used. Here's how you do this:

  1. Go into the Boost directory in your Terminal:

     cd /usr/include/boost_1_62_0/ 
  2. Run the command:

     sudo ./bootstrap.sh --prefix=/usr/local 
  3. To build Boost.Python and Boost.System into the /usr/local/lib folder, run:

     sudo ./b2 install --with-system --with-python 

    once the command has finished, you will find libboost_system and libboost_python (with various file endings) in /usr/local/lib

That's it for building the necessary Boost libraries. Note that any other Boost library that needs to be built can be done in the same way.

Preparing Python 2.7

Linux Mint 18 comes with Python 2.7 pre-installed. For me, running the command python --version returns the output Python 2.7.12 . I'll assume this is the same for you - but probably it doesn't make a difference what the version is. However, to get our objective of the C++ program with Boost.Python to build and run, you have to execute the additional command:

sudo apt-get install python-dev

which places some additional necessary files (particularly pyconfig.h ) into /usr/include/python2.7 .

Installing Linux GCC

Linux GCC allows you to compile C++ programs. To install everything necessary, simply run:

sudo apt-get install build-essential

Making our program run in Eclipse

Now we've got everything configured to be able to write, build and run our program in Eclipse. So, let's do it!

  1. Run Eclipse, either from the terminal (either by using the desktop icon we've created or by running /opt/eclipse/eclipse in the Terminal).
  2. If it's your first time running Eclipse, you'll be prompted to choose a workspace (basically, a folder where all Eclipse project files will be stored):

在此处输入图片说明

  1. Now go to File -> New -> C++ Project and enter the following and click Finish (you can use whatever Location you wish, including the default location (ie the one of your workspace)):

在此处输入图片说明

  1. Now go to File -> New -> Source File, enter the following and click Finish:

在此处输入图片说明

  1. In the main.cpp file that automatically opens in the editor, type in the program that we want to compile (note that I customized my Eclipse layout, so it looks different from the default layout you might have):

在此处输入图片说明

  1. Now the secret sauce. Go to Project -> Properties -> C/C++ Build -> Settings where you will see:

在此处输入图片说明

Go to GCC C++ Compiler -> Includes and, using the little icon that looks like a folder with a green plus sign, add the following Include paths:

在此处输入图片说明

Go to GCC C++ Linker -> Libraries and add the following Libraries and Library search paths:

在此处输入图片说明

Press OK to close the Project Properties window.

  1. Back in the main Eclipse window, first click the hammer icon to build the project, then the green play icon to run it (both buttons are highlighted in the below screenshot). In the console view you will first see a successful build, then the Terminal output of our program:

在此处输入图片说明 在此处输入图片说明

That's it! We've achieved our objective of building and running a C++ program with the Boost.Python library included. That's the end of this tutorial, I hope it helped you :)

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