开发环境搭建:C++编译器、Python环境与混合编程工具链

做高频交易系统,说白了就是跟纳秒级别的时间赛跑。C++负责底层执行,Python负责策略迭代——这个组合我用了快十年,至今没找到更好的替代方案。今天咱们先把开发环境搭起来,这是后面所有实战的基础。

一、C++编译器选择:GCC vs MSVC

我个人习惯在Linux上做高频交易开发,但Windows也有它的场景。先说说两个主流编译器。

1. GCC(MinGW-w64)

如果你用Linux或者WSL,GCC基本是默认选择。我建议用GCC 9.3以上版本,对C++17支持比较完整。

# Ubuntu/Debian
sudo apt update
sudo apt install build-essential g++-9

# 验证安装
g++ --version

避坑指南:我曾经在Ubuntu 18.04上直接用apt装g++,结果装了个4.8版本,连C++11的std::optional都不支持。后来学乖了,先apt-cache search g++看看仓库里有什么版本。

2. MSVC(Visual Studio Build Tools)

Windows用户我建议装Visual Studio 2022 Community版,或者只装Build Tools也行。做量化交易时,MSVC对Windows API的调用更原生,延迟也更可控。

# 用choco安装(推荐)
choco install visualstudio2022buildtools

# 或者手动下载安装
# https://visualstudio.microsoft.com/downloads/
我的建议:开发阶段用MSVC调试方便,生产环境切到GCC/Linux。两个编译器我都用,但最终上线一定是Linux + GCC。

二、Python环境配置:Anaconda

Python这块我强烈推荐Anaconda。为什么?因为它把numpy、pandas这些科学计算库都打包好了,省得你一个个编译。做量化回测时,这些库的底层很多是C/C++写的,性能不差。

# 下载Anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh

# 安装
bash Anaconda3-2023.09-0-Linux-x86_64.sh

# 创建专用环境
conda create -n hft_env python=3.10
conda activate hft_env

注意:Python版本别追新。3.10是目前pybind11和Boost.Python支持最稳定的版本。我试过3.12,有些旧库直接报错。

三、pybind11安装

pybind11是我最常用的C++/Python绑定工具。它轻量、头文件驱动,写起来比Boost.Python清爽得多。

# pip安装
pip install pybind11

# 或者从源码编译(我推荐这个,更可控)
git clone https://github.com/pybind/pybind11.git
cd pybind11
mkdir build && cd build
cmake ..
make -j4
sudo make install

安装完后,写个简单测试验证一下:

// example.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;

int add(int i, int j) {
    return i + j;
}

PYBIND11_MODULE(example, m) {
    m.def("add", &add, "A function that adds two numbers");
}
# 编译
c++ -O3 -Wall -shared -std=c++17 -fPIC \
    $(python3 -m pybind11 --includes) \
    example.cpp -o example$(python3-config --extension-suffix)

# 测试
python -c "import example; print(example.add(1, 2))"
核心要点:pybind11的编译命令里,--includes--extension-suffix这两个参数必须用对。我刚开始时总忘记加-fPIC,链接阶段各种报错。

四、Boost.Python安装

Boost.Python是老牌工具了,功能更全,但编译起来也重一些。如果你需要序列化、线程等高级功能,可以考虑它。

# Ubuntu安装
sudo apt install libboost-all-dev

# 或者源码编译
wget https://boostorg.jfrog.io/artifactory/main/release/1.83.0/source/boost_1_83_0.tar.gz
tar -xzf boost_1_83_0.tar.gz
cd boost_1_83_0
./bootstrap.sh --with-python=python3.10
./b2 install

避坑指南:我曾经在编译Boost.Python时,因为系统里同时有Python 3.8和3.10,bootstrap.sh自动选了3.8,导致后面链接失败。解决办法是显式指定Python路径:

./bootstrap.sh --with-python=/path/to/python3.10

五、知识体系总览

下面这张图是我自己整理的开发环境搭建逻辑,你看一眼就能明白各组件之间的关系:

高频做市开发环境知识体系 操作系统层 Linux (Ubuntu 20.04+) / Windows C++编译器 GCC 9.3+ / MSVC 2022 Python环境 Anaconda + Python 3.10 C++/Python绑定层 pybind11(推荐) / Boost.Python 关键配置项 • C++17标准 • -O3优化 • -fPIC编译 • 共享库(.so) • Python扩展 • 头文件路径 • 链接库路径 • 版本一致性

六、环境验证清单

搭完环境后,我建议你跑一遍这个验证脚本,确保所有组件都能正常工作:

#!/bin/bash
# check_env.sh - 环境验证脚本

echo "=== C++编译器 ==="
g++ --version | head -1

echo "=== Python版本 ==="
python3 --version

echo "=== pybind11 ==="
python3 -c "import pybind11; print(pybind11.__version__)"

echo "=== Boost.Python ==="
python3 -c "import boost; print('Boost.Python OK')" 2>/dev/null || echo "Boost.Python未安装"

echo "=== 编译测试 ==="
cat << 'EOF' > test.cpp
#include <pybind11/pybind11.h>
int square(int x) { return x * x; }
PYBIND11_MODULE(test, m) {
    m.def("square", &square);
}
EOF

c++ -O3 -shared -std=c++17 -fPIC \
    $(python3 -m pybind11 --includes) \
    test.cpp -o test$(python3-config --extension-suffix)

python3 -c "import test; print('2的平方:', test.square(2))"
rm -f test.cpp test*.so
常见问题
  • 编译时报"undefined symbol"——检查Python版本是否一致
  • 导入时报"ModuleNotFoundError"——检查.so文件是否在当前目录
  • 链接时报"cannot find -lpython3.10"——安装python3-dev包

嗯,环境搭建这块就这些。说白了就是三件事:C++编译器能编译、Python能跑、绑定工具能把两者连起来。我当年第一次搭环境时折腾了两天,现在有了这些经验,半小时就能搞定。


专注资料整理