Coder Social home page Coder Social logo

cpp2py's Introduction

Cpp2Py is the Python-C++ interfacing tool of the TRIQS project, provided here as a standalone project.

Installation

To install Cpp2Py, follow the installation steps:

git clone  https://github.com/TRIQS/cpp2py.git cpp2py
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=INSTALL_DIR ../cpp2py
make && make install

This installs the library in INSTALL_DIR. In order to make Cpp2Py available in your current environment you should run

source INSTALL_DIR/share/cpp2pyvars.sh

Example

Make sure that you have loaded Cpp2Py into your environment as instructed above. Created a C++ source file mymodule.hpp in a folder SRC:

///A wonderful little class
class myclass{
  int a, b;

  public:
    myclass(int a_, int b_) : a(a_), b(b_) {}

    ///getter for member a
    int get_a() const { return a;}
};

In the same folder, create a file CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)
find_package(Cpp2Py REQUIRED)

add_cpp2py_module(mymodule)
target_compile_options(mymodule PRIVATE -std=c++17)
target_include_directories(mymodule PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

Then, in the SRC folder, issue the command

c++2py mymodule.hpp

This creates a file mymodule_desc.py.

Exit the SRC folder and create a BUILD folder. Then, issue the following commands:

cd BUILD
cmake ../SRC
make

In the BUILD dir, you should see a mymodule.so file. You can now use your c++ class in Python:

import mymodule
A = mymodule.Myclass(4,5)
print(A.get_a())

By convention, c++ classes of the type my_little_class are converted in python classes of the type MyLittleClass.

License

Before you proceed, make sure you have read the LICENSE.txt file.

Enjoy!

The TRIQS team

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.