You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boost Python结合LibTorch时构造函数参数不匹配问题求助

问题:LibTorch与Boost.Python兼容导致参数类型不匹配错误

运行Python测试脚本时出现如下错误:

Boost.Python.ArgumentError: Python argument types in
Test.init(Test, int, str)
did not match C++ signature:
init(_object*, int, std::string)

编译生成的C++可执行文件运行正常,移除LibTorch依赖的Boost.Python包也能正常工作,因此问题源于LibTorch与Boost.Python的兼容性冲突。


示例代码与CMake配置

test.h

#ifndef TEST_H
#define TEST_H

#include <string>

class Test{

    int tensorSize;
    std::string label;

    public:
        Test(int i, std::string label);
    void printTensor();
};

#endif

test.cpp

#include <boost/python.hpp>
#include <string>
#include "test.h"
#include <torch/torch.h>
#include <torch/script.h>


#include <iostream>

Test::Test(int i, std::string lbl) {
    tensorSize = i;
    label = lbl;
}

void Test::printTensor(){
    auto x = torch::randn({10, tensorSize});

    std::cout << label << '\n';
    std::cout << "x:\n" << x << '\n';
}


using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
    class_<Test>("Test", init<int, std::string>())
    .def("printTensor", &Test::printTensor)
    ;
}

main.cpp(可执行文件入口)

#include "test.h"

int main() {
    Test t(3, "test");
    t.printTensor();
    return 0;
}

Python测试文件

import test

t = test.Test(3, "test")
t.printTensor()

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(boostTest)

set(BOOST_MIN_VERSION 1.86.0)
find_package(Torch REQUIRED)
find_package(PythonLibs 3 REQUIRED)

find_package(
    Boost ${BOOST_MIN_VERSION} REQUIRED
    COMPONENTS python REQUIRED
)
#C++ executable
include_directories(${PYTHON_INCLUDE_PATH})
add_executable(test.out main.cpp test.cpp  test.h)
target_link_libraries(test.out ${PYTHON_LIBRARIES} ${TORCH_LIBRARIES} Boost::python)

#boost python
set(CMAKE_SHARED_MODULE_PREFIX "")
add_library(test MODULE test.cpp)
target_link_libraries(test ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${TORCH_LIBRARIES} )
target_include_directories(test PRIVATE ${PYTHON_INCLUDE_DIRS})

解决方案

1. 统一C++标准与ABI设置

LibTorch默认使用C17及C11 ABI,需强制Boost.Python编译环境与之一致:
在CMakeLists.txt开头添加:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)

2. 对齐Python版本依赖

LibTorch自带Python依赖,需确保Boost.Python链接到同一版本:
替换原find_package(PythonLibs 3 REQUIRED)为:

find_package(Python 3 EXACT REQUIRED COMPONENTS Interpreter Development)

3. 调整链接库顺序

将Boost.Python放在链接列表最前面,避免符号冲突:
修改Python模块的链接指令:

target_link_libraries(test Boost::python ${PYTHON_LIBRARIES} ${TORCH_LIBRARIES})

4. 显式注册类型转换器

强制Boost.Python识别std::string与Python str的转换,在test.cpp的模块定义中添加:

#include <boost/python/string.hpp>

BOOST_PYTHON_MODULE(test)
{
    implicitly_convertible<std::string, std::string>();
    class_<Test>("Test", init<int, std::string>())
    .def("printTensor", &Test::printTensor)
    ;
}

5. 匹配LibTorch编译版本

确保LibTorch的版本(CPU/GPU、Debug/Release)与Boost.Python、编译环境完全一致,禁止混编Debug和Release库。


内容的提问来源于stack exchange,提问作者ppeloton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 14:18:11