Skip to content

Instantly share code, notes, and snippets.

@Drunkar
Last active February 2, 2022 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drunkar/8c78680279c237465d76bfc9f106dc3e to your computer and use it in GitHub Desktop.
Save Drunkar/8c78680279c237465d76bfc9f106dc3e to your computer and use it in GitHub Desktop.
pybind11+MinGWのcmake+visual studio 2019でboostライブラリを使用したスクリプトのpythonモジュールを作成するCMakeList.
# CMakeのバージョン
cmake_minimum_required(VERSION 3.19)
# プロジェクト名
project(proj_name CXX)
# PyBind11 を取得
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.6.1
)
FetchContent_MakeAvailable(pybind11)
# コンパイラーの指定
set(CMAKE_CXX_COMPILER C:\\MinGW\\mingw64\\bin)
# コンパイルオプション。下記はC++の場合
set(CMAKE_CXX_FLAGS "-Wall -std=c++1z /EHsc")
# Pythonのパスを探索
find_package(Python REQUIRED)
message(${Python_FOUND})
message(${Python_EXECUTABLE})
# boost
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
# .h を検出できるように,
# -I : インクルードパス
#include_directories(${Python_EXECUTABLE})
include_directories(C:\\ProgramData\\Anaconda3\\envs\\env_name\\include)
include_directories(C:\\ProgramData\\Anaconda3\\envs\\env_name\\Lib\\site-packages\\pybind11\\include)
include_directories(C:\\ProgramData\\Anaconda3\\envs\\env_name\\Lib\\site-packages\\pybind11\\include\\pybind11)
include_directories(C:\\boost_1_60_0)
# -L : ライブラリ探索パス
link_directories(C:\\ProgramData\\Anaconda3\\envs\\env_name\\Lib)
link_directories(C:\\boost_1_60_0\\libs\\graph)
# 作成する実行ファイル名とソースファイル
pybind11_add_module(proj_name proj_name.cpp)
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/boyer_myrvold_planar_test.hpp>
void boyer_myrvold(std::vector<int> lst) {
using namespace boost;
typedef adjacency_list
< vecS,
vecS,
undirectedS,
property<vertex_index_t, int>,
property<edge_index_t, int>
>
graph;
graph K_4(4);
add_edge(0, 1, K_4);
add_edge(0, 2, K_4);
add_edge(0, 3, K_4);
add_edge(1, 2, K_4);
add_edge(1, 3, K_4);
add_edge(2, 3, K_4);
if (boyer_myrvold_planarity_test(K_4))
std::cout << "K_4 is planar." << std::endl;
else
std::cout << "ERROR! K_4 should have been recognized as planar!"
<< std::endl;
graph K_5(5);
add_edge(0, 1, K_5);
add_edge(0, 2, K_5);
add_edge(0, 3, K_5);
add_edge(0, 4, K_5);
add_edge(1, 2, K_5);
add_edge(1, 3, K_5);
add_edge(1, 4, K_5);
add_edge(2, 3, K_5);
add_edge(2, 4, K_5);
// Now add the final edge...
add_edge(3, 4, K_5);
// Test for planarity - we know it is not planar, we just want to
// compute the kuratowski subgraph as a side-effect
typedef std::vector< graph_traits<graph>::edge_descriptor >
kuratowski_edges_t;
kuratowski_edges_t kuratowski_edges;
if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = K_5,
boyer_myrvold_params::kuratowski_subgraph = std::back_inserter(kuratowski_edges)
)
)
{
std::cout << "Input graph is planar" << std::endl;
}
else
{
std::cout << "Input graph is not planar" << std::endl;
kuratowski_edges_t::iterator ki, ki_end;
ki_end = kuratowski_edges.end();
for(ki = kuratowski_edges.begin(); ki != ki_end; ++ki)
{
std::cout << *ki << " ";
}
std::cout << std::endl;
}
return;
}
// PYBIND11_MODULE()はPythonでimportする際の関数を作成する
PYBIND11_MODULE(graph_utils_boost, m) {
m.doc() = "Modules about graph planarity based on boost library."; // optional module docstring
m.def("boyer_myrvold", &boyer_myrvold, "BVoyer and Myrvold's planarity test.");
}
@Drunkar
Copy link
Author

Drunkar commented Feb 2, 2022

mkdir build
cd build
cmake ..

でproj_name.slnが生成されるのでvisual studio2019で開き、Releaseでビルドする

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment