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.

  1. In order to make your Linux machine visible to Visual Studio, please install VisualGDB 3.0 or later on your Windows machine.
  2. 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.00-simpleapp
  3. Right-click on the solution item in the Solution Explorer and select “Add->New Project”:01-addprj
  4. We will now create a static library project. Ensure that VisualGDB wizard is selected, enter MyStaticLib as the project name and press OK:02-newprj
  5. Select “Create a new project” and “Static library”. Press on ‘Next’.04-new
  6. On the next page select the remote Linux machine. Ensure that the machine is the same as for the application project.06-toolchain
  7. Finish with the default source transfer.07-source
  8. You have now created a new static library project. Open the MyStaticLib.cpp file.08-prj2
  9. We need to add an inclusion and a function call to the main application.08-include
  10. Now you need to make LinuxApp project depend on the library. Right-click on the LinuxApp project and select “Project Dependencies”:09-deps
  11. Check the MyStaticLib checkbox in the Project Dependencies window:10-staticlib
  12. Build the solution.11-build
  13. Put a breakpoint to MyStaticLibTest() and start debugging.12-breakpoint
  14. The breakpoint will be hit. You can use call stack window to see the the static library function was called from main().13-staticcall
  15. Stop debugging and open the Makefile inside the LinuxApp project. Find the EXTERNAL_LIBS line:14-makefile
  16. As you can see, VisualGDB has added MyStaticLib to the library list. The EXTERNAL_LIBS will be checked every time you build your project and will reflect the dependencies specified in the Project Dependencies dialog.
    Warning! Ensure that the relative paths between projects on the Windows and Linux machines are the same. The path on the Linux machine can be set in VisualGDB Project Properties. Examples:
    App on Windows Lib on Windows App on Linux Lib on Linux
    c:\projects\App1 c:\projects\Lib1 /tmp/App1 /tmp/Lib1
    c:\projects\App1 c:\projects\App1\Lib1 /tmp/App1 /tmp/App1/Lib1

    Bad example:

    App on Windows Lib on Windows App on Linux Lib on Linux
    c:\projects\App1 c:\projects\App1\Lib1 /tmp/App1 /tmp/Lib1

    Here the relative path from App1 to Lib1 is just “Lib1″ on Windows and “../Lib1″ on Linux. VisualGDB will add “Lib1/$(BinaryDir)/libname.a” to the Makefile, however this will won’t work on Linux machine as the relative paths are different.

  17. Add a new dynamic library to the solution. Use the MyDynamicLib name and select “Shared Library” on the Project Type page.
  18. Add MyDynamicLib to project dependencies of LinuxApp:15-dynamic
  19. Add a call to MyDynamicLibTest() to LinuxApp.cpp file:16-main
  20. 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:17-modules