{"id":1614,"date":"2016-04-10T20:50:15","date_gmt":"2016-04-11T03:50:15","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1614"},"modified":"2016-04-10T20:50:15","modified_gmt":"2016-04-11T03:50:15","slug":"developing-c-python-modules-for-cygwin","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/cygwin\/python\/","title":{"rendered":"Developing C++ Python modules for Cygwin"},"content":{"rendered":"<p>This tutorial shows how to develop C++ modules that can be loaded into Python scripts under Cygwin. The Cygwin environment contains Windows ports for many Linux-based tools, so if you are porting a Linux application to run on Windows, it can prove time-saving by drastically reducing the porting effort. It also comes with pre-built Python 2.x and 3.x and we will show in this tutorial how to create a module that will be loaded into a Cygwin Python script.<\/p>\n<p>Before you begin, install VisualGDB 5.1 or later and install Cygwin including the following packages:<\/p>\n<ul>\n<li>g++<\/li>\n<li>gdb<\/li>\n<li>make<\/li>\n<li>python3<\/li>\n<li>python3-debuginfo<\/li>\n<\/ul>\n<ol>\n<li>Start Visual Studio and launch the VisualGDB MinGW\/Cygwin Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-cygpythonprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1615\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-cygpythonprj.png\" alt=\"01-cygpythonprj\" width=\"786\" height=\"478\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-cygpythonprj.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-cygpythonprj-300x182.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project -&gt; A Cygwin Python module&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1616\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype1.png\" alt=\"02-prjtype\" width=\"757\" height=\"610\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype1.png 757w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype1-300x242.png 300w\" sizes=\"(max-width: 757px) 100vw, 757px\" \/><\/a><\/li>\n<li>Select the location of your Cygwin toolchain. If it&#8217;s not auto-detected, select &#8220;specify a Cygwin-based toolchain manually&#8221; and point VisualGDB to its location:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-toolchain.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1617\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-toolchain.png\" alt=\"03-toolchain\" width=\"676\" height=\"610\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-toolchain.png 676w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-toolchain-300x271.png 300w\" sizes=\"(max-width: 676px) 100vw, 676px\" \/><\/a><\/li>\n<li>On the last page of the wizard replace &#8220;python&#8221; with &#8220;python3&#8221; to explicitly select Python 3.x and press &#8220;Finish&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1618\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python3.png\" alt=\"04-python3\" width=\"676\" height=\"610\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python3.png 676w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python3-300x271.png 300w\" sizes=\"(max-width: 676px) 100vw, 676px\" \/><\/a><\/li>\n<li>VisualGDB will create a project consisting of a C++ Python module, a Makefile building the module using the Python includes and libraries and a simple Python script calling a function from the module. Build the module by pressing Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1619\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build1.png\" alt=\"05-build\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build1.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build1-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Set a breakpoint in the Python script on the line calling the hello() method and press F5 to start debugging. Once the breakpoint hits you will be able to look through the Python variables or step through the code:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-cppdebug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1620\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-pydebug.png\" alt=\"06-pydebug\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-pydebug.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-pydebug-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>If you step into the hello() method, VisualGDB will actually step into the internal Python function used to call C++ functions, so instead, set a breakpoint in the HelloMethod() and press F5. Once the breakpoint hits, you will see both C++ and Python frames in the Call Stack and will be able to step through the method:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-cppdebug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1621\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-cppdebug.png\" alt=\"07-cppdebug\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-cppdebug.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-cppdebug-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>You can navigate the Call Stack to view the Python code calling the C++ code and even the internal Python interpreter&#8217;s functions responsible for handling the calls. This is especially useful when debugging complex memory corruption problems:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-pycall.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1622\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-pycall.png\" alt=\"08-pycall\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-pycall.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-pycall-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Set a breakpoint on the print() line and resume debugging. Once the breakpoint is hit, see that the Python script has received the value of 123 returned by the C++ method:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-result.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1623\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-result.png\" alt=\"09-result\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-result.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-result-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to develop C++ modules that can be loaded into Python scripts under Cygwin. The Cygwin environment<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[37,122],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1614"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=1614"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1614\/revisions"}],"predecessor-version":[{"id":1882,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1614\/revisions\/1882"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}