{"id":163,"date":"2014-02-04T17:39:31","date_gmt":"2014-02-04T17:39:31","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=163"},"modified":"2017-10-11T16:09:43","modified_gmt":"2017-10-11T23:09:43","slug":"porting-linux","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/porting\/linux\/","title":{"rendered":"Porting a Windows App to Linux with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to port a simple Windows application to Linux by creating a Linux configuration in a Visual Studio project with VisualGDB.<\/p>\n<p>Before we begin, ensure that <a href=\"\/download\">VisualGDB 4.0<\/a> or later is installed.<\/p>\n<ol>\n<li>Our first step will be to create a simple console application for Windows. Start Visual Studio and select File-&gt;New-&gt;Project. <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/01-newproj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/01-newproj.png\" alt=\"01-newproj\" width=\"698\" height=\"612\" \/><\/a><\/li>\n<li>\u00a0Select &#8220;Win32 Console Application&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/02-consoleapp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/02-consoleapp.png\" alt=\"02-consoleapp\" width=\"700\" height=\"421\" \/><\/a><\/li>\n<li>Proceed with the default settings and press &#8220;Finish&#8221;. Visual Studio will create a basic console application. Add the following code to the main file:\n<pre class=\"\">#include \"stdafx.h\"\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint _tmain(int argc, _TCHAR* argv[])\r\n{\r\n    printf(\"HOMEPATH=%s\\n\", getenv(\"HOMEPATH\"));\r\n    printf(\"HOME=%s\\n\", getenv(\"HOME\"));\r\n    return 0;\r\n}<\/pre>\n<\/li>\n<li>Build the project. If the build complains about the getenv() function, add the following line to the beginning of <strong>stdafx.h<\/strong>:\n<pre>#define _CRT_SECURE_NO_WARNINGS<\/pre>\n<p>Run the project and observe its output. Note that on Windows the HOME variable is undefined:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/03-sourcecode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/03-sourcecode.png\" alt=\"03-sourcecode\" width=\"697\" height=\"664\" \/><\/a><\/li>\n<li>Now we will create a new Linux configuration for the project. Select <strong>Project-&gt;Add VisualGDB Configuration-&gt;New Linux Configuration<\/strong>:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/04-newcfg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/04-newcfg.png\" alt=\"04-newcfg\" width=\"697\" height=\"664\" \/><\/a><\/li>\n<li>VisualGDB will start the Linux Configuration Wizard. Enter &#8220;LinuxDebug&#8221; as the configuration name:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/05-summary.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/05-summary.png\" alt=\"05-summary\" width=\"698\" height=\"628\" \/><\/a><\/li>\n<li>On the next page select the Linux computer you want to use. You can also select a different toolchain:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/06-linuxcomp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/06-linuxcomp.png\" alt=\"06-linuxcomp\" width=\"698\" height=\"628\" \/><\/a><\/li>\n<li>The last page allows specifying the location of the source files on the Linux machine. You can proceed with the default settings:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/07-sourcelocation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/07-sourcelocation.png\" alt=\"07-sourcelocation\" width=\"698\" height=\"628\" \/><\/a><\/li>\n<li>When you press Finish, VisualGDB will create a new project configuration and a corresponding solution configuration. As certain Windows-specific files are missing on the Linux machine, the compilation will fail with several errors. We will fix them in the next steps:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/12-errors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/12-errors.png\" alt=\"12-errors\" width=\"696\" height=\"662\" \/><\/a><\/li>\n<li>Open the stdafx.h file and remove references to <strong>SDKDDKVer.h<\/strong> and <strong>tchar.h<\/strong>:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/13-linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/13-linux.png\" alt=\"13-linux\" width=\"696\" height=\"662\" \/><\/a><\/li>\n<li>Rename <strong>_tmain()<\/strong> to <strong>main()<\/strong> and replace <strong>_TCHAR<\/strong> with <strong>char<\/strong>. Build the project. You will notice that the modified source files will be transferred to the Linux machine and built there using GCC, the standard compiler on Linux systems:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/14-buildok.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/14-buildok.png\" alt=\"14-buildok\" width=\"696\" height=\"662\" \/><\/a><\/li>\n<li>Set a breakpoint on the <strong>return<\/strong> statement and run the program. Observe the output:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/15-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/15-debug.png\" alt=\"15-debug\" width=\"696\" height=\"662\" \/><\/a><\/li>\n<li>As Windows and Linux use different variable names (HOMEPATH vs HOME), we had to write two separate <strong>getenv()<\/strong> lines. Now we will add conditional compilation statements to ensure that only one line is used on each OS. We could test for the <strong>_MSC_VER<\/strong> macro on Windows (that is set by the Visual C++ compiler) or the <strong>LINUX<\/strong> macro that is defined by Linux headers, however in this tutorial we will show how to add your own macro. Right-click on the project, select VisualGDB Project Properties and go to the Makefile Settings page. Add <strong>BUILDING_FOR_LINUX<\/strong> to Preprocessor Macros:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/16-macro.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/16-macro.png\" alt=\"16-macro\" width=\"699\" height=\"782\" \/><\/a><\/li>\n<li>Now press OK and add the <strong>#ifndef BUILDING_FOR_LINUX<\/strong> statement to your code. Build and run the program. Observe that the output only contains one line now:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/17-ifdef.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/17-ifdef.png\" alt=\"17-ifdef\" width=\"696\" height=\"662\" \/><\/a><\/li>\n<li>Now switch the current configuration back to Debug and hit F5 to run the Windows version of your program:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/18-windows.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/porting\/linux\/18-windows.png\" alt=\"18-windows\" width=\"701\" height=\"664\" \/><\/a><\/li>\n<li>As most of the API (except for the basic standard functions) is different between Linux and Windows, you may want avoid having too many <strong>#ifdef<\/strong> statements and simplify your porting. You can accomplish this in one of the following ways:\n<ul>\n<li>Use a multi-platform framework like QT that abstracts out the differences between platforms<\/li>\n<li>Install Cygwin and create a Cygwin configuration via <strong>Project-&gt;Add VisualGDB Configuration-&gt;Windows<\/strong>. Cygwin provides equivalents to most Linux APIs on Windows so once you have ported your app to Linux you can build the Windows version of it without too many changes.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>If you are using Windows 10, you can also use the Windows Subsystem for Linux to build, debug and test your Linux code. See <a href=\"https:\/\/visualgdb.com\/tutorials\/porting\/lxss\/\">this tutorial<\/a> for a detailed description.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to port a simple Windows application to Linux by creating a Linux configuration in a Visual<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[33,84],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/163"}],"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=163"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":3311,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/163\/revisions\/3311"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}