This repo can be used for educational purpose only. Please don't copy/pase the code directly to URI. After trying the problem yourself if you can't solve that problem then you can check my solutions.
## VSCode Tasks for C++ Compilation and Execution
This project uses a `tasks.json` file in Visual Studio Code to streamline the process of compiling and running C++ programs. Below is a breakdown of the tasks and how to use them.
## Prerequisites
- **g++**: Ensure that `g++` is installed on your system to compile C++ programs.
You can install it using the following command:
```bash
sudo apt install g++
```
-
Visual Studio Code: You should have VSCode installed with the C++ extensions for syntax highlighting and better development experience.
-
input.txt: This file should exist in the same directory as your C++ file if you want to redirect input during execution.
The tasks.json
provides two tasks: one for compiling the C++ code, and another for compiling and running it in one step.
-
Label:
compile
-
Command: This task compiles the currently open C++ file using the following command:
g++ -std=c++17 -o <filename without extension> <file path>
- Input: The open C++ file in the editor.
- Output: An executable file with the same name as the C++ source file (but without the
.cpp
extension).
- Open your
.cpp
file in VSCode. - Press
Ctrl+Shift+B
and select thecompile
task. - The compiled executable will be created in the same directory as your source file.
-
Label:
compile and run
-
Command: This task compiles and then runs the program, redirecting input from
input.txt
and outputting tooutput.txt
.g++ -std=c++17 -o <filename without extension> <file path> && ./<filename without extension> < input.txt > output.txt
- Input: The currently open
.cpp
file in the editor, andinput.txt
if present. - Output: The output will be saved to
output.txt
in the same directory as the C++ file.
- Input: The currently open
- Open your
.cpp
file in VSCode. - Make sure you have an
input.txt
file in the same directory. - Press
Ctrl+Shift+B
and select thecompile and run
task. - The program will be compiled and executed, with input read from
input.txt
and output written tooutput.txt
.
- File Paths: This
tasks.json
file handles file paths with spaces (e.g., directories likeProgramming for Career
), so ensure that your project files are structured correctly. - Input/Output Redirection: The program expects
input.txt
to be present for input redirection. You can modify the task for different input/output needs.
- No such file or directory: Ensure that the paths in the
input.txt
andoutput.txt
files are correct. - Compile Errors: Check that your C++ code does not contain syntax errors by looking at the terminal output when running the
compile
task.