{"id":151,"date":"2017-06-30T09:39:31","date_gmt":"2017-06-30T16:39:31","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=151"},"modified":"2017-06-30T13:56:39","modified_gmt":"2017-06-30T20:56:39","slug":"linux-libraries","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/linux\/libraries\/","title":{"rendered":"Creating Linux Libraries with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how create static and dynamic Linux libraries with Visual Studio and debug them with VisualGDB. We will create a simple Linux application with VisualGDB, add one static and one dynamic library to it and test both in the debugger.<\/p>\n<p>To proceed with the tutorial you will need a Windows machine and a Linux machine. You can use 2 physical computers, a Windows computer running Linux inside <a href=\"http:\/\/virtualbox.org\/\"> VirtualBox<\/a>\/<a href=\"http:\/\/vmware.com\/\">VMWare<\/a> or vice versa.<\/p>\n<p>Before you begin, install <a href=\"\/download\">VisualGDB<\/a> on your Windows machine.<\/p>\n<ol>\n<li>Follow the <a href=\"http:\/\/visualgdb.com\/tutorials\/linux\/libraries\/..\/\">Linux Application tutorial<\/a> to create a simple Linux app with Visual Studio. We will use <strong>LinuxApp<\/strong> as the project name. Build the app and ensure that you can debug it.<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2978\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-debug.png\" alt=\"05-debug\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-debug.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-debug-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-debug-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Right-click on the solution item in the Solution Explorer and select &#8220;Add-&gt;New Project&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2980\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-newprj.png\" alt=\"05-newprj\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-newprj.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-newprj-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-newprj-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>We will now create a static library project. Ensure that VisualGDB wizard is selected, enter <strong>MyStaticLib <\/strong>as the project name and press OK:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/06-lib2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2981\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/06-lib2.png\" alt=\"06-lib2\" width=\"909\" height=\"619\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/06-lib2.png 909w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/06-lib2-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/06-lib2-130x90.png 130w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project&#8221; -&gt;&#8221;Static library&#8221; -&gt; Use MSBuild&#8221;. Press on &#8216;Next&#8217;.<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/07-static.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2982\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/07-static.png\" alt=\"07-static\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/07-static.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/07-static-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the next page select the remote Linux machine. Ensure that the machine is the same as for the application project.<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/08-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2983\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/08-build.png\" alt=\"08-build\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/08-build.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/08-build-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Finish with the default source transfer.<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/09-upload.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2984\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/09-upload.png\" alt=\"09-upload\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/09-upload.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/09-upload-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>You have now created a new static library project. Modify the main source file of your application as follows:\n<pre class=\"\">#include &lt;iostream&gt;\r\n#include \"MyStaticLib\/MyStaticLib.h\"\r\n\r\nusing namespace std;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n\u00a0\u00a0 \u00a0char sz[] = \"Hello, World!\";\u00a0\u00a0 \u00a0\/\/Hover mouse over \"sz\" while debugging to see its contents\r\n\u00a0\u00a0 \u00a0cout &lt;&lt; sz &lt;&lt; MyStaticLibTest() &lt;&lt; endl;\u00a0\u00a0 \u00a0\/\/&lt;================= Put a breakpoint here\r\n\u00a0\u00a0 \u00a0return 0;\r\n}<\/pre>\n<\/li>\n<li>Before this code can compile, we need to add a reference from the application project to the library project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/10-ref.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2985\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/10-ref.png\" alt=\"10-ref\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/10-ref.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/10-ref-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/10-ref-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Check the MyStaticLib checkbox in the Add Reference window:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/11-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2986\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/11-name.png\" alt=\"11-name\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/11-name.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/11-name-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/11-name-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Build the solution:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/12-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2987\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/12-built.png\" alt=\"12-built\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/12-built.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/12-built-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/12-built-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Put a breakpoint to <strong>MyStaticLibTest()<\/strong> and start debugging. The breakpoint will be hit. You can use call stack window to see the the static library function was called from <strong>main()<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/13-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2988\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/13-debug.png\" alt=\"13-debug\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/13-debug.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/13-debug-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/13-debug-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Add a new dynamic library to the solution. Use the <strong>MyDynamicLib<\/strong> name and select &#8220;Shared Library&#8221; on the Project Type page.<\/li>\n<li>Reference the dynamic library from the main project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/14-ref2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2989\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/14-ref2.png\" alt=\"14-ref2\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/14-ref2.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/14-ref2-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/14-ref2-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Add a call to MyDynamicLibTest() to the applications&#8217; main source file:\n<pre class=\"\">#include &lt;iostream&gt;\r\n#include \"MyStaticLib\/MyStaticLib.h\"\r\n#include \"MyDynamicLib\/MyDynamicLib.h\"\r\n\r\nusing namespace std;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n\u00a0\u00a0 \u00a0char sz[] = \"Hello, World!\";\u00a0\u00a0 \u00a0\/\/Hover mouse over \"sz\" while debugging to see its contents\r\n\u00a0\u00a0 \u00a0cout &lt;&lt; sz &lt;&lt; MyStaticLibTest() &lt;&lt; MyDynamicLibTest() &lt;&lt; endl;\u00a0\u00a0 \u00a0\/\/&lt;================= Put a breakpoint here\r\n\u00a0\u00a0 \u00a0return 0;\r\n}<\/pre>\n<\/li>\n<li>Build the solution. Put a breakpoint to MyDynamicLibTest() and start debugging. When the breakpoint is hit you can use the Module window in Visual Studio to see your dynamic library among the loaded modules:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/15-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2990\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/15-bkpt.png\" alt=\"15-bkpt\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/15-bkpt.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/15-bkpt-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/15-bkpt-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>You can configure a VisualGDB-based library to &#8220;export&#8221; some build settings so that they are automatically added to all projects referencing it. Try adding &#8220;USING_MY_DYNAMIC_LIB&#8221; to the exported Preprocessor Definitions for the library project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/16-export.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2991\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/16-export.png\" alt=\"16-export\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/16-export.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/16-export-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/16-export-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Try checking the new macro with an #ifdef in the main project. Ensure that it builds:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/17-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2992\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/17-build.png\" alt=\"17-build\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/17-build.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/17-build-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/17-build-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>You can configure various VisualGDB-related settings for each project via right-click -&gt; VisualGDB Project Properties: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/18-settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2993\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/18-settings.png\" alt=\"18-settings\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/18-settings.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/18-settings-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/18-settings-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<\/ol>\n<p>If you prefer an environment where multiple libraries share the same common settings (e.g. deployment paths), try creating CMake-based projects instead by following <a href=\"https:\/\/visualgdb.com\/tutorials\/linux\/cmake\/\">this tutorial<\/a>. VisualGDB transparently integrates CMake into Visual Studio and will automatically edit CMakeLists.txt files in response to various changes in Solution Explorer.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how create static and dynamic Linux libraries with Visual Studio and debug them with VisualGDB. We will<\/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":[79,33,131],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/151"}],"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=151"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":2994,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/151\/revisions\/2994"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}