Call C / C ++ from Python on Mac

This is the first post. I recently requested to touch Python, but it's slow. But when I had to write it in Python, I asked, "Why don't you call me C !?" But it didn't work as I did in the articles written by others. I was wondering if the reference or something was wrong, but I made a note because it moved when I moved it forcibly.

Overview

It seems that you should use the one called ** Boost.Python **. I couldn't browse the library, so I put it all in one directory ** and it worked.

environment

macOS 10.13.6 Python 3.7 BoostPython 1.68.0

Prepare what you need

I have prepared a directory * to execute this program. I think it can be anywhere, but I chose ~ / Python / BoostPythonTest /. Prepare the following items here.

libboost_python37.dylib Install Boost.Python below. [^ 1]

% brew install boost-python3

Once installed, you should have /usr/local/Cellar/boost-python3/1.68.0/lib/libboost_python37.dylib, copy it to the * execute directory *.

libpython3.7.dylib Have Python installed. [^ 1]

% brew install python3

Copy /usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/Python to the * execution directory * and rename it to libpython3.7.dylib.

C / C ++ source

I prepared a source called hello.cpp for testing.

hello.cpp


#include <boost/python.hpp>
#include <stdio.h>

char greet(int x){
    printf("printHELLO!!\n");
    return '0' + x + 1;
}

char const* fine(){
    return "How are you?";
}

BOOST_PYTHON_MODULE(hello){
  using namespace boost::python;
    def("greet", greet);
    def("fine", fine);
}

Python source

I prepared a source called test.py for testing.

test.py


import hello
x = hello.greet(2)
print(x)
y = hello.file()
print(y)

compile

You should be able to compile with: Just browse for the files in * the directory you want to run *.

% g++ -fPIC -Wall -O2 -shared -o hello.so hello.cpp libboost_python37.dylib libpython3.7.dylib

hello.so is created in the * execution directory *.

Run

Try to run it.

% python3 test.py
printHELLO!!
3
How are you?

It seems to be done properly.

Summary

It was a bit of a brute force method to put everything in one directory and move it, but it worked for the time being, so let's do it.

I tried to run it on Ubuntu, but I couldn't. I'm using dylib, so it seems to be useless unless it's a Mac.

reference

[1] Wrap c / c ++ so that it can be used in python https://www.quark.kj.yamagata-u.ac.jp/~hiroki/python/?id=19

[2] From boost installation to execution (boost :: python). http://nonbiri-tereka.hatenablog.com/entry/2014/02/01/004547

[^ 1]: If you get a permission error when using brew, dosudo brew ....

Recommended Posts

Call C / C ++ from Python on Mac
Call C from Python with DragonFFI
Call popcount from Ruby / Python / C #
python on mac
Call c language from python (python.h)
Update Python on Mac from 2 to 3
Install Python on Mac
Install Python 3 on Mac
Install Python 3.4 on Mac
Call a Python script from Embedded Python in C ++ / C ++
Call CPLEX from Python (DO cplex)
Install pygame on python3.4 on mac
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing Python on Mac
Preferences to generate animated GIFs from Python on Mac
[Python] How to call a c function from python (ctypes)
Call Matlab from Python to optimize
Call C language functions from Python to exchange multidimensional arrays
Building a Python environment on Mac
Call a Python function from p5.js.
Notes on using MeCab from Python
Create a Python environment on Mac (2017/4)
Python environment construction memo on Mac
Install Python 3.7 Anaconda on MAC, but Python 2
Call the code generated by Cython from C / C ++ (Mac version)
Install python3 on Mac (El Capitan)
Tips for calling Python from C
Execute Python code from C # GUI
Environment construction of python3.8 on mac
Run Python scripts synchronously from C #
Notes on accessing dashDB from python
Call python from nim with Nimpy
If Python 3.5.0 installation fails on Mac
Steps to install python3 on mac
How to rebuild python environment from pyenv on Mac environment (El Capitan)
Install mecab on Sakura shared server and call it from python
Simple code to call a python program from Javascript on EC2
Install lp_solve on Mac OS X and call it with python.
Call your own C language shared library from Python using ctypes
Execute Python code on C ++ (using Boost.Python)
Create a python environment on your Mac
Follow active applications on Mac with Python
[C] [python] Read with AquesTalk on Linux
How to erase Python 2.x on Mac.
Connecting from python to MySQL on CentOS 6.4
Notes on building Python and pyenv on Mac
Build Python environment with Anaconda on Mac
Generate C language from S-expressions in Python
Video processing using Python + OpenCV on Mac
Call a command from Python (Windows version)
Run Python in C ++ on Visual Studio 2017
C language to see and remember Part 2 Call C language from Python (argument) string
Python on Windows
twitter on python3
sql from python
C language to see and remember Part 1 Call C language from Python (hello world)
python C ++ notes
python, openFrameworks (c ++)
MeCab from Python