DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

1 说明

conda创建的虚拟环境中使用pybind11时出现问题。

1.1 cmake …

mkdir build
cd build
cmake ..

输出:

(py36_pytorch041) ➜  build git:(master) ✗ cmake ..
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /Users/zhangxin/anaconda3/bin/python3.7 (found version "3.7") 
-- Found PythonLibs: /Users/zhangxin/anaconda3/lib/libpython3.7m.dylib
-- Performing Test HAS_CPP14_FLAG
-- Performing Test HAS_CPP14_FLAG - Success
-- pybind11 v2.3.dev0
-- Performing Test HAS_FLTO
-- Performing Test HAS_FLTO - Success
-- LTO enabled
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/zhangxin/github/pybind11_examples/01_py-list_cpp-vector/build

【注意】Found PythonInterp和Found PythonLibs这两行。

1.2 make

make

输出:

(py36_pytorch041) ➜  build git:(master) ✗ make 
Scanning dependencies of target example
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[100%] Linking CXX shared module example.cpython-37m-darwin.so
[100%] Built target example

1.3 执行python测试

python ../test.py

输出:

(py36_pytorch041) ➜  build git:(master) ✗ python ../test.py 
Traceback (most recent call last):
  File "../test.py", line 2, in <module>
    import example
ModuleNotFoundError: No module named 'example'

2 解决方法

2. 1 在cmake 时设定python的路径。

cmake -DPYTHON_LIBRARY=/Users/zhangxin/anaconda3/envs/py36_pytorch041/lib/libpython3.6m.dylib \
    -DPYTHON_INCLUDE_DIR=/Users/zhangxin/anaconda3/envs/py36_pytorch041/include/python3.6 \
    -DPYTHON_EXECUTABLE=/Users/zhangxin/anaconda3/envs/py36_pytorch041/bin/python3.6 \
    ..

输出:

(py36_pytorch041) ➜  build git:(master) ✗ cmake -DPYTHON_LIBRARY=/Users/zhangxin/anaconda3/envs/py36_pytorch041/lib/libpython3.6m.dylib \
    -DPYTHON_INCLUDE_DIR=/Users/zhangxin/anaconda3/envs/py36_pytorch041/include/python3.6 \
    -DPYTHON_EXECUTABLE=/Users/zhangxin/anaconda3/envs/py36_pytorch041/bin/python3.6 \
    ..
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /Users/zhangxin/anaconda3/envs/py36_pytorch041/bin/python3.6 (found version "3.6.9") 
-- Found PythonLibs: /Users/zhangxin/anaconda3/envs/py36_pytorch041/lib/libpython3.6m.dylib
-- Performing Test HAS_CPP14_FLAG
-- Performing Test HAS_CPP14_FLAG - Success
-- pybind11 v2.3.dev0
-- Performing Test HAS_FLTO
-- Performing Test HAS_FLTO - Success
-- LTO enabled
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/zhangxin/github/pybind11_examples/01_py-list_cpp-vector/build

2.2 make

(py36_pytorch041) ➜  build git:(master) ✗ make 
Scanning dependencies of target example
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[100%] Linking CXX shared module example.cpython-36m-darwin.so
[100%] Built target example

2.3 测试

mv example.cpython-36m-darwin.so
cd ..
python test.py

3 代码

example.cpp

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>

// ----------------
// Regular C++ code
// ----------------

// multiply all entries by 2.0
// input:  std::vector ([...]) (read only)
// output: std::vector ([...]) (new copy)
std::vector<double> modify(const std::vector<double>& input)
{
  std::vector<double> output;

  std::transform(
    input.begin(),
    input.end(),
    std::back_inserter(output),
    [](double x) -> double { return 2.*x; }
  );

  // N.B. this is equivalent to (but there are also other ways to do the same)
  //
  // std::vector<double> output(input.size());
  //
  // for ( size_t i = 0 ; i < input.size() ; ++i )
  //   output[i] = 2. * input[i];

  return output;
}

// ----------------
// Python interface
// ----------------

namespace py = pybind11;

PYBIND11_MODULE(example,m)
{
  m.doc() = "pybind11 example plugin";

  m.def("modify", &modify, "Multiply all entries of a list by 2.0");
}

test.py

import example

A = [1.,2.,3.,4.]

B = example.modify(A)

print(B)

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(example)

add_subdirectory(pybind11)
pybind11_add_module(example example.cpp)
posted on   DoubleLi  阅读(325)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2017-08-23 ffmpeg编码h264只包含I帧P帧的方法
2017-08-23 【FFmpeg】FFmpeg常用基本命令
2017-08-23 FFmpeg基础知识之————H264编码profile & level控制
2017-08-23 ffmpeg混音(将多个声音合成一个)命令
2017-08-23 ffpanel --ffmpeg的GUI,让ffmpeg离开黑黑的命令行
2017-08-23 使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度
2017-08-23 几个学习流媒体的案例代码网址
点击右上角即可分享
微信分享提示