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.
- In order to make your Linux machine visible to Visual Studio, please install VisualGDB 3.0 or later on your Windows machine.
- 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.
- Right-click on the solution item in the Solution Explorer and select “Add->New Project”:
- We will now create a static library project. Ensure that VisualGDB wizard is selected, enter MyStaticLib as the project name and press OK:
- Select “Create a new project” and “Static library”. Press on ‘Next’.
- On the next page select the remote Linux machine. Ensure that the machine is the same as for the application project.
- Finish with the default source transfer.
- You have now created a new static library project. Open the MyStaticLib.cpp file.
- We need to add an inclusion and a function call to the main application.
- Now you need to make LinuxApp project depend on the library. Right-click on the LinuxApp project and select “Project Dependencies”:
- Check the MyStaticLib checkbox in the Project Dependencies window:
- Build the solution.
- 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().
- Stop debugging and open the Makefile inside the LinuxApp project. Find the EXTERNAL_LIBS line:
- 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
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.
- Add a new dynamic library to the solution. Use the MyDynamicLib name and select “Shared Library” on the Project Type page.
- Add MyDynamicLib to project dependencies of LinuxApp:
- Add a call to MyDynamicLibTest() to LinuxApp.cpp file:
- 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: