记录一次关于OpenCV的CmakeLists的探索
Steps for building the Python module
We are now ready to go over the steps for building our Python module.
- Step 1: Put your c++ source code and header files inside the src directory.
- Step 2: Include your header file in headers.txt
- Step 3: Make a build directory.
<i><span>1</span><span> mkdir build</span></i>
Step 4: Use gen2.py to generate the Python binding files. You need to specify the prefix (pybv), the location of the temporary files (build) and the location of the header files (headers.txt).
<i><span>1</span><span> python3 gen2.py pybv build headers.txt</span></i>
This should generate a whole bunch of header files with prefix pybv_*.h. If you are curious, feel free to inspect the generated files.
- Step 5: Compile the module
<i><span>1</span><span> g</span><span>++</span> <span>-</span><span>shared </span><span>-</span><span>rdynamic </span><span>-</span><span>g </span><span>-</span><span>O3 </span><span>-</span><span>Wall </span><span>-</span><span>fPIC \</span><br /><span>2</span><span> bv.cpp src</span><span>/</span><span>bvmodule.cpp \</span><br /><span>3</span><span> </span><span>-</span><span>DMODULE_STR</span><span>=</span><span>bv </span><span>-</span><span>DMODULE_PREFIX</span><span>=</span><span>pybv \</span><br /><span>4</span><span> </span><span>-</span><span>DNDEBUG </span><span>-</span><span>DPY_MAJOR_VERSION</span><span>=</span><span>3</span><span> \</span><br /><span>5</span><span> </span><span>`</span><span>pkg</span><span>-</span><span>config </span><span>--</span><span>cflags </span><span>--</span><span>libs opencv</span><span>`</span><span> \</span><br /><span>6</span><span> </span><span>`</span><span>python3</span><span>-</span><span>config </span><span>--</span><span>includes </span><span>--</span><span>ldflags</span><span>`</span><span> \</span><br /><span>7</span><span> </span><span>-</span><span>I . </span><span>-</span><span>I</span><span>/</span><span>usr</span><span>/</span><span>local</span><span>/</span><span>lib</span><span>/</span><span>python3.</span><span>5</span><span>/</span><span>dist</span><span>-</span><span>packages</span><span>/</span><span>numpy</span><span>/</span><span>core</span><span>/</span><span>include \</span><br /><span>8</span><span> </span><span>-</span><span>o build</span><span>/</span><span>bv.so </span></i>
In Line 2 we specify the source files. In Line 3, we set the module name using MODULE_STR and MODULE_PREFIX (pybv) used in the previous step. In Line 4 we specify the Python version. In Line 5 we include OpenCV library and the header files and in Line 6 we include Python 3 related header files and some standard libraries. In my machine, numpy was not in the included path and so I had to add an extra Line 7 for numpy. Your location for numpy may be different. Finally, in Line 8 we specify the location of the output module (build/bv.so).