{"id":1594,"date":"2016-03-29T20:38:04","date_gmt":"2016-03-30T03:38:04","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1594"},"modified":"2016-03-29T20:38:04","modified_gmt":"2016-03-30T03:38:04","slug":"developing-linux-c-python-modules-with-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/linux\/python\/","title":{"rendered":"Developing Linux C++ Python Modules with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to develop a basic C++ module used by a Python script using Visual Studio and debug both of them with VisualGDB. Before you begin, install VisualGDB 5.1 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open VisualGDB Linux Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-modproj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1609\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-modproj.png\" alt=\"01-modproj\" width=\"786\" height=\"443\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-modproj.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-modproj-300x169.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Select &#8220;Python-based project&#8221; and choose the project template. In this example we will show how to debug a C++ module loaded into a Python script:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-pymodule.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1608\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-pymodule.png\" alt=\"02-pymodule\" width=\"822\" height=\"641\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-pymodule.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-pymodule-300x234.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the next page select your Linux computer and click &#8220;Next&#8221; to continue. VisualGDB will automatically check the Linux system and install the missing Python symbols:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1607\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-target.png\" alt=\"03-target\" width=\"822\" height=\"641\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-target.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-target-300x234.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>The third page allows overriding the Python executable (e.g. switch from Python 2.x to Python 3.x by selecting <strong>python3<\/strong>)\u00a0 and the include directory used to build the modules. In this example we will simply continue with the default settings:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1606\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python.png\" alt=\"04-python\" width=\"822\" height=\"641\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-python-300x234.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the last page you can change the location of the source files on the Linux side. Proceed with the default settings and press &#8220;Finish&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-transfer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1605\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-transfer.png\" alt=\"05-transfer\" width=\"822\" height=\"641\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-transfer.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-transfer-300x234.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>VisualGDB will generate a project consisting of a Python script, a C++ file and a Makefile. Build the C++ module by pressing Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1604\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-build.png\" alt=\"06-build\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-build.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-build-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Now you can step into the Python code by simply pressing F10:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-step.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1603\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-step.png\" alt=\"07-step\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-step.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-step-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Set a breakpoint inside the HelloMethod() function in the C++ file and press F5. Once the breakpoint triggers, you can see that the function was called from the Python code and the args variable contains an array of 1 string (&#8220;World&#8221;). VisualGDB will display the contents of the Python objects when debugging both C++ and Python code, so you don&#8217;t need to worry about their internal representation:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-hello.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1602\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-hello.png\" alt=\"08-hello\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-hello.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-hello-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Use the Call Stack to navigate to the python frame calling the HelloMethod(). You can view the contents of various variables there via the Watch window. Note how VisualGDB allows viewing both the meaningful representation of the variables and the internal view useful when debugging memory corruption problems:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-watch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1601\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-watch.png\" alt=\"09-watch\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-watch.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-watch-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Set a breakpoint on the print() line and press F5. The breakpoint will trigger and VisualGDB will show the value returned by the C++ function:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-return.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1600\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-return.png\" alt=\"10-return\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-return.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-return-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a>Press Shift-F5 to stop debugging.<\/li>\n<li>Now we will overview the structure of the files in the project. The Makefile is a standard VisualGDB Makefile for building a shared library and can be configured visually via VisualGDB Project Properties:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/11-makefile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1599\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/11-makefile.png\" alt=\"11-makefile\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/11-makefile.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/11-makefile-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Note that VisualGDB has automatically added the Python include directory and library name so that the C++ module can access the Python API: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/pylib.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1610\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/pylib.png\" alt=\"pylib\" width=\"844\" height=\"594\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/pylib.png 844w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/pylib-300x211.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/pylib-130x90.png 130w\" sizes=\"(max-width: 844px) 100vw, 844px\" \/><\/a><\/li>\n<li>The Debug setup is slightly different from the one for debugging normal C++ applications: it allows specifying a Python interpreter and a Python script that will be launched. It also adds your project&#8217;s output directory to PYTHONPATH so that Python can discover your module:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/12-dbgsettings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1598\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/12-dbgsettings.png\" alt=\"12-dbgsettings\" width=\"844\" height=\"594\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/12-dbgsettings.png 844w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/12-dbgsettings-300x211.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/12-dbgsettings-130x90.png 130w\" sizes=\"(max-width: 844px) 100vw, 844px\" \/><\/a><\/li>\n<li>The module in turn provides the initialization function (names vary between Python 2.x and 3.x) that reports the &#8220;hello&#8221; function to the interpreter:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/initfunc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1611\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/initfunc.png\" alt=\"initfunc\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/initfunc.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/initfunc-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>When you launch the Python script, it will locate the library in the Debug directory:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/13-tree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1597\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/13-tree.png\" alt=\"13-tree\" width=\"912\" height=\"410\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/13-tree.png 912w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/13-tree-300x135.png 300w\" sizes=\"(max-width: 912px) 100vw, 912px\" \/><\/a>Then it will load the module, call the initialization function, see the &#8220;hello&#8221; method and call it.<\/li>\n<li>Now we will change the HelloMethod() to call the <strong>gethostbyname()<\/strong> function and return the host name to the caller:\n<pre class=\"\">static PyObject *HelloMethod(PyObject *self, PyObject *args)\r\n{\r\n\u00a0\u00a0\u00a0 char hostname[512] = { 0, };\r\n\u00a0\u00a0\u00a0 gethostname(hostname, sizeof(hostname));\r\n\u00a0\u00a0\u00a0 return Py_BuildValue(\"s\", hostname);\r\n}<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/14-hostname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1596\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/14-hostname.png\" alt=\"14-hostname\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/14-hostname.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/14-hostname-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Change the Python module to call the new method:\n<pre class=\"\">import LinuxPythonModuleDemo\r\n\r\ndef printhostname():\r\n\u00a0\u00a0\u00a0 name = LinuxPythonModuleDemo.hostname()\r\n\u00a0\u00a0\u00a0 print(\"Host name: \" + name)\r\n\r\nprinthostname()<\/pre>\n<p>Then press F5 to build your project and start debugging it. See how the host name returned by the C++ method is how shown in the Python code:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/15-pyhostname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1595\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/15-pyhostname.png\" alt=\"15-pyhostname\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/15-pyhostname.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/15-pyhostname-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>You can also launch your program without debugging by pressing Ctrl-F5. VisualGDB will deploy it, run it and display the output:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/launch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1612\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/launch.png\" alt=\"launch\" width=\"869\" height=\"410\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/launch.png 869w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/launch-300x142.png 300w\" sizes=\"(max-width: 869px) 100vw, 869px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to develop a basic C++ module used by a Python script using Visual Studio and debug<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[33,122],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1594"}],"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=1594"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1594\/revisions"}],"predecessor-version":[{"id":1613,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1594\/revisions\/1613"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}