Creating Linux Libraries with Visual Studio

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.

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 VirtualBox/VMWare or vice versa.

Before you begin, install VisualGDB on your Windows machine.

  1. Follow the Linux Application tutorial to create a simple Linux app with Visual Studio. We will use LinuxApp as the project name. Build the app and ensure that you can debug it.05-debug
  2. Right-click on the solution item in the Solution Explorer and select “Add->New Project”:05-newprj
  3. We will now create a static library project. Ensure that VisualGDB wizard is selected, enter MyStaticLib as the project name and press OK:06-lib2
  4. Select “Create a new project” ->”Static library” -> Use MSBuild”. Press on ‘Next’.07-static
  5. On the next page select the remote Linux machine. Ensure that the machine is the same as for the application project.08-build
  6. Finish with the default source transfer.09-upload
  7. You have now created a new static library project. Modify the main source file of your application as follows:
  8. Before this code can compile, we need to add a reference from the application project to the library project:10-ref
  9. Check the MyStaticLib checkbox in the Add Reference window:11-name
  10. Build the solution:12-built
  11. Put a breakpoint to MyStaticLibTest() and start debugging. The breakpoint will be hit. You can use call stack window to see the the static library function was called from main():13-debug
  12. Add a new dynamic library to the solution. Use the MyDynamicLib name and select “Shared Library” on the Project Type page.
  13. Reference the dynamic library from the main project:14-ref2
  14. Add a call to MyDynamicLibTest() to the applications’ main source file:
  15. 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:15-bkpt
  16. You can configure a VisualGDB-based library to “export” some build settings so that they are automatically added to all projects referencing it. Try adding “USING_MY_DYNAMIC_LIB” to the exported Preprocessor Definitions for the library project:16-export
  17. Try checking the new macro with an #ifdef in the main project. Ensure that it builds:17-build
  18. You can configure various VisualGDB-related settings for each project via right-click -> VisualGDB Project Properties: 18-settings

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 this tutorial. VisualGDB transparently integrates CMake into Visual Studio and will automatically edit CMakeLists.txt files in response to various changes in Solution Explorer.