Coding⏱️ 3 min read📅 2026-06-19

How to Fix Error code 2 Error – Error code 2 when compiling a cpp file in notepad++

Error code 2 when compiling a cpp file in notepad++, how to fix and include only the file name without extension.

Quick Answer: Use the filename without extension by using the basename function from the cstdlib library, e.g. g++ basename(FILE_NAME).cpp -o basename.exe

Error code 2 occurs when compiling a C++ file in Notepad++ due to an incorrect syntax. This issue affects users who rely on the program for coding and compilation purposes.

It can be frustrating to encounter this error, especially when trying to run a simple C++ script. In this guide, we will explore the root causes of Error code 2 and provide step-by-step solutions to resolve the issue.

🔍 Why This Happens

  • The primary reason for Error code 2 is the incorrect use of quotes around the file name in the compilation command. When using double quotes, the compiler expects a file extension, which leads to the error.
  • Another possible cause could be the presence of special characters or spaces in the file name, causing the compiler to misinterpret the syntax.

🔧 Proven Troubleshooting Steps

Using File Name Without Extension

  1. Step 1: To fix this issue, remove any quotes around the file name in the compilation command. This will allow the compiler to use only the file name without the extension.
  2. Step 2: Replace the line `cd "$(CURRENT_DIRECTORY)"` with `cd $(CURRENT_DIRECTORY)` and the line `g++ $(FILE_NAME) -o $(FILE_NAME).exe` with `g++ $(FILE_NAME) -o $(FILE_NAME)`
  3. Step 3: This will ensure that only the file name without extension is used in the compilation command.

Using File Name Without Extension Using Shell Expansions

  1. Step 1: Alternatively, you can use shell expansions to remove the file extension from the file name.
  2. Step 2: Use the `$(basename $FILE_NAME)` function in bash to extract the file name without extension. Replace the line `g++ $(FILE_NAME) -o $(FILE_NAME).exe` with `g++ $(basename $FILE_NAME) -o $(basename $FILE_NAME).exe`
  3. Step 3: This method is more suitable for users who are familiar with shell expansions and scripting.

💡 Conclusion

By following these steps, you should be able to resolve the Error code 2 issue in Notepad++. Remember to always verify yourfile names and extensions to avoid similar errors in the future.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions