1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PyBind11のcmake_exampleをVisual Studio 2017でデバッグ

1
Last updated at Posted at 2018-12-14

バージョン

Visual Studio 2017: 15.9.4
CMake: 3.13.1
cmake_example: 8818f49

cmake_exampleとは

PyBind11のHello World。CMakeでパッケージ化できる。

Visual StudioのCMake integrationは?

混合モード(Python / C++)でのデバッグに対応していない。Mixed mode debugging for cmake projects

準備

set Path=C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_86;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_86\Scripts;%Path%

手順

まずはリポジトリから取ってくる。そしてCMakeLists.txtからsln / vcxprojファイルを生成。

git clone --recursive https://github.com/pybind/cmake_example.git
cd cmake_example
cmake -G "Visual Studio 15 2017" -A Win32 # 64ビットならx64

以下はVisual Studio 2017のGUIで。

  1. cmake_example.slnを開いて、cmake_exampleプロジェクトをビルド。Debugフォルダにcmake_example.cp36-win32.pydができていることを確かめる。

  2. ソリューションに新しいプロジェクト(Pythonアプリケーション)を追加

  • これはPythonからpydを叩くテスト用モジュール
  • 名前はpython_testとしておく
  1. python_testプロジェクトのプロパティで
  • 「インタープリター」に32ビット版Pythonを指定
  • 「ネイティブコードのデバッグを有効にする」にチェック
  1. ソリューションエクスプローラーでpython_testプロジェクトの中の「検索パス」を右クリック、「フォルダーを検索パスに追加」でDebugフォルダを追加

  2. python_test.pyの中身を書く

python_test.py
import sys
print(sys.path)
import cmake_example
assert cmake_example.add(1,2) == 3

python_test.pyを実行。もしimport cmake_exampleでコケるなら、sys.pathがDebugフォルダに通っているかどうか確かめる。
main.cppにブレークポイントを設定して実行してみて、ブレークポイントが効くことを確かめる。

参考

Python 用 C++ 拡張機能の作成
cmake-generators

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?