【问题标题】:Launching a program before Debugging in VSCode在 VSCode 中调试之前启动程序
【发布时间】:2016-01-26 10:50:23
【问题描述】:

我正在使用 VSCode 0.10.6 进行 C++ 编程。我正在尝试在调试之前启动一个程序。程序 OpenOCD 是 GDB 所连接的。如果我通过终端手动打开和关闭它,它工作正常,但似乎应该有一种简单的方法让 VSCode 为我启动它。

我玩过 tasks.json,看来您需要使用一些丑陋的 bat/sh 文件结合 launch.json 中的 preLaunchTasks 来完成此任务。

【问题讨论】:

    标签: debugging visual-studio-code


    【解决方案1】:

    目前的答案是您确实必须使用preLaunchTasks 才有机会完成这项工作。如果确实可行,我会很乐意使用丑陋的脚本-但事实并非如此。就我而言,我需要在后台运行一个或多个可执行文件,以允许 VSCode 继续调试。

    不幸的是,我尝试启动的每个可执行文件(通过start)实际上并没有作为“分离”进程运行,因此 VSCode 会等待每个可执行文件完成运行,然后才能完成 preLaunchTasks 并开始调试.不是我想要的。

    我找到了an article by someone having a similar "detached process" problem with subversion,并使用他的 C++ 代码解决了与 Visual Studio Code 相同的问题。我在该代码中发现了一两个错误,我已修复。这是我目前正在使用的:

    // dstart.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <windows.h> 
    #include <stdio.h> 
    #include <tchar.h> 
    
    //http://stackoverflow.com/questions/1536205/running-another-program-in-windows-bat-file-and-not-create-child-process
    //http://svn.haxx.se/users/archive-2008-11/0301.shtml
    int _tmain()
    {
        //https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156(v=vs.85).aspx
        LPWSTR pCmd = ::GetCommandLine();
        // skip the executable 
        if (*pCmd++ == L'"') 
            while (*pCmd++ != L'"');
        else 
            while (*pCmd != NULL && *pCmd != L' ') ++pCmd;
        while (*pCmd == L' ') pCmd++;
        STARTUPINFO si;
        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);
        PROCESS_INFORMATION pi;
        ZeroMemory(&pi, sizeof(pi));
        // Start the child process. 
        BOOL result = CreateProcess
            (
                NULL, // No module name (use command line) 
                pCmd, // Command line 
                NULL, // Process handle not inheritable 
                NULL, // Thread handle not inheritable 
                FALSE, // Set bInheritHandles to FALSE 
                CREATE_NEW_CONSOLE, // Detach process 
                NULL, // Use parent's environment block 
                NULL, // Use parent's starting directory 
                &si, // Pointer to STARTUPINFO structure 
                &pi // Pointer to PROCESS_INFORMATION structure (returned)
                );
        if (result) return 0;
        wchar_t msg[2048];
        FormatMessage
            (
                FORMAT_MESSAGE_FROM_SYSTEM,
                NULL,
                ::GetLastError(),
                MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
                msg, sizeof(msg),
                NULL
                );
        fputws(msg, stderr);
        _flushall();
        return -1;
    }
    

    编译后,您可以像在 DOS 提示符下使用 start 命令一样使用它。将其放在您附加到preLaunchTasks 的脚本中 在 Visual Studio Code 中。

    【讨论】:

      猜你喜欢
      • 2019-04-20
      • 2019-10-21
      • 1970-01-01
      • 2022-11-24
      • 2021-01-15
      • 2014-10-13
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多