操作步骤
添加 gpg 秘钥:
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
添加源
vim /etc/apt/sources.list.d/llvm.list
# Focal (20.04) LTS deb http://apt.llvm.org/focal/ llvm-toolchain-focal main deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main
- 更新 apt 包信息:
sudo apt update
安装 clang、lld 和 libc++:
sudo apt install install clang-17 clang-tools-17 clang-17-doc libclang-common-17-dev libclang-17-dev libclang1-17 clang-format-17 python3-clang-17 clangd-17 clang-tidy-17 sudo apt install lld-17 sudo apt install libc++-17-dev libc++abi-17-dev
Linux 程序编译:
clang++ --std=c++17 -o main main.cpp
Windows 程序(交叉)编译:
clang++ --std=c++17 -target x86_64-pc-windows-gnu -mconsole -o main.exe main.cpp
示例程序
目录结构:
. ├── main.cpp └── pocketpy.h
main.cpp
内容:#include <iostream> #include "pocketpy.h" using namespace pkpy; using namespace std; int main(int argc, char** argv) { cout << "C++: hello world!" << endl; VM* vm = new VM(true); // Hello world! vm->exec("print('Python: Hello world!')", "main.py", EXEC_MODE); // Create a list vm->exec("a = [1, 2, 3]", "main.py", EXEC_MODE); // Eval the sum of the list PyVar result = vm->exec("sum(a)", "<eval>", EVAL_MODE); std::cout << py_cast<i64>(vm, result) << endl; // 6 pkpy_delete(vm); return 0; }
pocketpy.h
来自项目 blueloveTH/pocketpy:PocketPy is a lightweight(~6000 LOC) Python interpreter for game engines.
PocketPy是一个轻量级的Python解释器,为嵌入至游戏引擎而设计。
你写得非常清晰明了,让我很容易理解你的观点。