Home > Blockchain >  How to debug a C dll with VisualStudio cmake
How to debug a C dll with VisualStudio cmake

Time:12-06

I'm trying to debug a dll with an external application not belonging to the solution. I'm using VisualStudio cmake I've set up the launch.vs.json with something like:

  {
  "version": "0.2.1",
  "defaults": {},1
  "configurations": [
    {
      "name": "mydll.dll",
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "mydll.dll",
      "program": "external_program.exe",
      "args": [ "path_to_input_file" ]
    }
  ]
}

It doesn't work. What is the correct way to do it?

CodePudding user response:

You need symbols for that library to debug or the library must be built with debug symbols attached (i.e a debug dll). After that you can 'attach to that process' which is loading the dll. how to attach to a process

CodePudding user response:

I think the VS_DEBUGGER_COMMAND and VS_DEBUGGER_COMMAND_ARGUMENTS properties do what you want.

Using MSVC, the following cmake project will build a mydll.dll library that, when debugged, will run C:/path/to/external_program C:/path/to/mydll.dll.

cmake_minimum_required(VERSION 3.12)
project(mydll)

find_program(EXTERNAL_PROGRAM NAMES external_program)
if(NOT EXTERNAL_PROGRAM)
    message(FATAL_ERROR "Could not find external_program")
endif()

add_library(mydll SHARED source1.c)
set_target_properties(mydll
    PROPERTIES
        VS_DEBUGGER_COMMAND "${EXTERNAL_PROGRAM}"
        VS_DEBUGGER_COMMAND_ARGUMENTS "$<TARGET_FILE:mydll>"
)

To create a MSVC project, run the following command from the directory containing the above CMakeLists.txt. This will configure the CMake project, and open Visual Studio.

cmake -S . -B build -DEXTERNAL_PROGRAM="C:/path/to/external_program.exe"
cmake --open build

Because external_program.exe is external code, you need to enable debugging of external sources. This can be done as follows:

- In the Tools -> Options -> Debugging options
  - Disable Just My Code: This will allow the debugger to attempt to locate symbols for code outside your solution.
  • Related