简体   繁体   中英

Debug Command line in Visual Studio code

I am now using Visual studio code and I want to debug C/C++ program which runs as a command line. However, debug of Visual studio code just supports the normal program (not a command line program). When I want to debug a command line program, I need to edit my code in which I initialize the argument before running debugger. For example

int main(int argv, char *argc[])
{
    if (argv != 2)
    {
        printf("Wrong syntax\nCorrect Syntax: readfile <source file>\n");
        exit(1);
    }
    FILE *fp = fopen(argc[1], "rb");
}

When I want to debug, I need to initialize like this:

int main()
{
    /*if (argv != 2)
    {
        printf("Wrong syntax\nCorrect Syntax: readfile <source file>\n");
        exit(1);
    }*/ //eliminate this part
    argc[1] = "phonebook.dat";
    FILE *fp = fopen(argc[1], "rb");

I find it annoying. So how do I change the debugger on VS code to debug C and C++ command line programs?

You need to edit your configuration to receive command line arguments.

See this: Configuring launch.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM