-
Set up the git repo.
-
Add essential submodules: put all 3rd party libraries in a folder, and manage them with
git submoudle
.
cd third_party
git submodule add https://gitlab.com/libeigen/eigen.git eigen
cd eigen
git checkout 3.4
cd ..
git submodule add https://github.com/pybind/pybind11.git pybind11
cd pybind11
git checkout v2.8.0
cd ..
Other libraries like JSON can be added as well. Note: it's highly recommended to checkout to the latest stable release of each submodule.
- Create the DLL project:
- In VS, create a new DLL project, whose name is the same as the desired Python module name.
- Add the path to all submodules into additional include path:
- Properties --> Configuration Properties --> VC++ Directories --> Include Directories
- Add
$(SolutionDir)
,$(ProjectDir)
,$(SolutionDir)..\third_party\pybind11\include\pybind11
and$(SolutionDir)..\third_party\eigen
.
- Specify the output file ext to be
pyd
:- Properties --> Configuration Properties --> Advanced --> Target File Extension:
.pyd
.
- Properties --> Configuration Properties --> Advanced --> Target File Extension:
- Install the desired Python NuGet Package.
- Add the path to Python headers into additional include path:
- Properties --> Configuration Properties --> VC++ Directories --> Include Directories
- Add $(ProjectDir)packages\python.3.8.10\tools\include
- Add path to
python3.lib
andpython38.lib
(replace version number based on your installed Python version) to the input of the linker:- Properties --> Linker --> Input --> Additional Dependencies
- add
$(ProjectDir)packages\python.3.8.10\tools\libs\python3.lib
and$(ProjectDir)packages\python.3.8.10\tools\libs\python38.lib
into the list, separated by;
.
A few Windows specific steps:
- If the pyd file is going to be shared, it's better to use the static version of runtime library:
- Properties --> C/C++ --> Code Generation --> Runtime Library
- For
Release
build: Multi-threaded (/MT) - For
Debug
build: Multi-threaded Debug (/MTd)
- If the build time isn't too long, there's no need to use
Precompiled Headers
:- Properties --> C/C++ --> Precompiled Headers --> Precompiled Header:
Not Using Precompiled Headers
.
- Properties --> C/C++ --> Precompiled Headers --> Precompiled Header:
- If Windows APIs are called, add
WindowsApp.lib
into the input of the linker:- Properties --> Linker --> Input --> Additional Dependencies: add WindowsApp.lib into the list, separated by
;
.
- Properties --> Linker --> Input --> Additional Dependencies: add WindowsApp.lib into the list, separated by
Clone the repo:
git clone https://github.com/zhwa/PyCppDemo.git
cd PyCppDemo
git submodule update --init --recursive
Change the Python NuGet package version: start Python and check the Python version. Right click project --> Manage NuGet Packages... --> Updates --> Version.
-
Choose Windows Desktop Dynamic-Link Library (DLL). Avoid Windows Universal DLL (Universal Windows). Otherwise, the C++ functions won't be exported to Python.
-
Set Conformance mode --> No; and Enable Run-Time Type Information --> Yes
-
If there are compiler complaints about CRT security: In pch.h --> #define _CRT_SECURE_NO_WARNINGS