[VSCode] Change the generation location of the .exe file after C/C++ compilation (2022/1/29)

[VSCode] Change the generated location of the .exe file after C/C++ compilation

foreword

C/C++The executable file and source code generated by the program running are mixed together. As an obsessive-compulsive disorder, how can we tolerate it "so presumptuous".
insert image description here

It's really annoying. If there are too many source codes, the folder will be full, and then the generated executable file and source code will be placed in the same directory. If they still have the same name, it is easy to make mistakes, so I thought about changing them.

Therefore, when writing in VSCodeC/C++ , different source files are placed in one folder for convenience. The following is my second attempt to modify it successfully (the first time I gave up, and I tried again after a long time). After referring to many blog articles, I finally solved it.

Before reading this blog, you must ensure that you have configured the C/C++ environment of VSCode, and it can run normally (F10) and debug (F5)

If necessary, please refer to : Zero Foundation | How to Write C/C++ Programs with VS Code - Installation and Configuration

1. Install the plugin Code Runner

insert image description here

2. settings.json

Create a new file in the .vscodefolder settings.json(in fact, you can also configure it in the general settings.json file)

Do not spell the file name of settings.json wrong!

insert image description here
Add the settings.jsonfollowing (with your own path)

  • Compile the C program adding the following:
{
    
    
    "code-runner.executorMap":{
    
    
        "c" :"cd $dir && gcc $fileName -o E:\\Java\\C\\C++\\luogu\\Build\\$fileNameWithoutExt && E:\\Java\\C\\C++\\luogu\\Build\\$fileNameWithoutExt"
    }
}
  • To compile the C++ program add the following:
{
    
    
	"code-runner.executorMap":{
    
    
        "cpp" :"cd $dir && g++ $fileName -o E:\\Java\\C\\C++\\luogu\\Build\\$fileNameWithoutExt && E:\\Java\\C\\C++\\luogu\\Build\\$fileNameWithoutExt"
    }
}

E:\\Java\\C\\C++\\luogu\\Build\\.exeis the folder path where I want to put the file, $fileNameWithoutExtis the execution file name

You need to change the former to your own path, and &&there are two paths before and after you need to change

3. launch.json

Open launch.json, modify programthe configuration of " " as follows (similarly, you have to modify it to your own path, and it must be settings.jsonthe same as the path in):

// "program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
"program": "E:\\Java\\C\\C++\\luogu\\Build\\${fileBasenameNoExtension}.exe", 

insert image description here

4. tasks.json

Modify the path tasks.jsonin the options:args

"E:\\Java\\C\\C++\\luogu\\Build\\${fileBasenameNoExtension}.exe"

insert image description here
You're done, the generated .exebinaries are in other directories!

insert image description here

Reference : Link


come on!

grateful!

effort!

Guess you like

Origin blog.csdn.net/qq_46092061/article/details/122747885