Inline Python code directly in your C++ code. Inspired by rust project inline-python!
#include <stdio.h>
#include "inline_py.h"
int main() {
int a = 0, b = 0;
python (a, b) (
print("hello world")
b = a + 1
if a >= 0:
b += 1
b *= 2
);
printf("a=%d, b=%d\n", a, b);
return 0;
}Use the python (args...) ( ...code...) macro to write Python code directly in your C++ code.
NOTE: xmake toolchain is required. You need a xmake.lua like this:
target("sample")
set_kind("binary")
set_languages("c++17")
add_rules("inline_py")
add_packages("pybind11")
add_files("sample.cc")If you use mac, this line might useful add_rpathdirs("/Library/Developer/CommandLineTools/Library/Frameworks/")
Then you can run these command lines:
xmake build sample # build this sample
xmake run sample # run this sampleTo reference C++ variables, let macro capture it, as shown in the example above. The output is
a=0, b=4- Export this package to xmake-repo.