debugging Visual Studio 2022如何从NET 6 C#多平台控制台应用程序调试本机C/C++库(使用gcc gnu编译器生成)

xkftehaa  于 2023-03-23  发布在  C#
关注(0)|答案(1)|浏览(219)

在Visual Studio 2022中必须配置什么才能进行调试(单步执行)对C/C本机库的调用(使用gcc gnu编译器生成,在CMake c模板项目的帮助下),从加载/使用本机库的主NET 6 C#多平台控制台应用程序中调用?本机库中的函数在控制台应用程序中使用DllImport声明。我不知道如何配置Visual Studio 2022以使用.NET核心调试器(vsdbg)用于控制台应用程序和gdb调试器(对于Windows - mingw和WSL/Ubuntu),这是示例代码,在Visual Studio 2022中调试NET 6控制台应用程序时,我想进入MyTest()函数:

using System.Reflection;
using System.Runtime.InteropServices;

namespace LibTest
{
    internal class Program
    {
        private const string szDLL_Win_x64_Resource = @"C:\Code\Test\out\build\Mingw64-Debug\TestLib\TestLib.dll";
        private const string szDLL_Linux_x64_Resource = @"/home/xxx/.vs/Test/out/build/Linux-GCC-Debug/TestLib/TestLib.dll";

        [DllImport("TestLib.dll")]
        private static extern int MyTest(string strParam);

        private static void Main(string[] args)
        {
            string osKind = "Unknown";
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                osKind = "Linux";
                // Load the Linux x64 DLL into the current process
                NativeLibrary.Load(szDLL_Linux_x64_Resource);
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                osKind = "Windows";
                // Load the Windows x64 DLL into the current process
                NativeLibrary.Load(szDLL_Win_x64_Resource);
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { osKind = "MacOSX"; }

            string str = "abcdef";

            try
            {
                var res = MyTest(str);
            }
            catch (Exception exc)
            {
            }
        }
    }
}
wlwcrazw

wlwcrazw1#

我已经测试了你的代码(用C++ .dll库),应该能达到你的要求。
请按照以下步骤操作:
1、进入Tools -〉Options -〉Debugging -〉General:

2,请取消选择以下设置:
仅我的代码
要求源文件与原始版本完全匹配
单步执行属性和运算符
3、右键单击您的C#项目,然后单击属性;之后,转到以下位置并选择“启用本机代码调试”:

完成上述步骤后,我就能够调试CPP代码的逻辑了。

相关问题