Stop all makefile actions when C++ hits error call in unix
Since you're talking about Makefiles, I'm assuming that you want to stop the Makefile during the compilation of the C++ program (and not the execution of the resulting program).
If that's the case, use the #error
preprocessor directive.
#if !defined(FOO) && defined(BAR) #error "BAR requires FOO." #endif
Reference:
To signify an error status during runtime of a C++-program, you can do the following:
Printing an error to the error stream is done with std::cerr
:
std::cerr << "This will not do!\n";
Passing an error status that make will understand is done by returning a non-zero value from main
:
int main() { // Your program if (error_condition) { std::cerr << "This will not do!\n"; return 1; }}
You can also terminate the program, optionally with an error status, from anywhere in the program using the exit
function from #include <cstdlib>
:
#include <cstdlib>void not_main() { std::exit(1); // Non-zero value here indicates error status to make}int main() { not_main();}