Derek Zhang
Derek Zhang

Reputation: 47

How do I check if a compiler error shows in cmd?

I decided to make it easier for me to compile and run my C++ programs through command prompt (I use cmd in conjunction with Sublime Text).

I added a cmd command (dragged .bat file in system32) to jump to my C++ files directory, and then a few more commands to compile my three work files.

I wanted cmd to output "Compiling work.cpp..." (which I've implemented) and if it doesn't return an error, output "Successfully Compiled" (What I don't know how to do). Is there anyway to implement this in cmd? I'm using g++ (tdm64-1) 5.1.0

Upvotes: 1

Views: 949

Answers (1)

anatolyg
anatolyg

Reputation: 28251

Use the errorlevel "environment variable":

g++ work.cpp
if %errorlevel% == 0 echo "Successfully Compiled"

This uses the syntax of environment variables (the % part), but errorlevel is a special "variable", which holds the status of recently finished command. If the status is 0, it was successful, otherwise an error occurred.

Upvotes: 1

Related Questions