I am trying to compile simple code with -fno-exceptions flag. Its giving error.
Please let me know how to suppress this. I am using gcc version 4.6.3
Code
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
Log
> g++ throw.cc -o out -fno-exceptions
throw.cc: In function ‘int main()’:
throw.cc:10:11: error: exception handling disabled, use -fexceptions to enable
throw.cc:14:56: error: ‘e’ was not declared in this scope
Edit
I have a client code which have lot throws like this. I have to integrate this in my project and I can't control compilation flags to build(which will come from config which has -fno-exceptions enabled). I wanted a quick work around that I can suggest.
EDIT
I found a workaround see below for my answer.
You cannot use -fno-exceptions flag with program, that use exceptions (try/catch/throw).
Quote from gcc manual
Before detailing the library support for -fno-exceptions, first a
passing note on the things lost when this flag is used: it will break
exceptions trying to pass through code compiled with -fno-exceptions
whether or not that code has any try or catch constructs. If you might
have some code that throws, you shouldn't use -fno-exceptions. If you
have some code that uses try or catch, you shouldn't use
-fno-exceptions.
You can't compile a program with this flag if you throw exceptions in your own code. The -fno-exceptions flag does the following:
All exception handling in STL libraries are removed; throws are replaced with abort() calls
Stack unwind data and code is removed. This saves some code space, and may make register allocation marginally easier for the compiler (but I doubt it'll have much performance impact). Notably, however, if an exception is thrown, and the library tries to unwind through -fno-exceptions code, it will abort at that point, as there is no unwind data.
This will, effectively, turn all exceptions into abort()s, as you would like. Note, however, that you will not be allowed to throw - any actual throws or catchs in your code will result in a compile-time error.
I found a workaround. Except for the "e" in the catch block the code can be rewritten like
#include <iostream>
using namespace std;
int main () {
__try
{
__throw_exception_again 20;
}
__catch (int e)
{
cout << "An exception occurred." << '\n';
}
return 0;
}
and can be compiled with
g++ throw.cc -o out -fno-exceptions.
Related
I am trying to use C++17 if constexpr feature but fail to compile a simple function.
Code:
template <auto B>
int foo()
{
if constexpr(B)
{
return 1;
}
else
{
return 2;
}
} // <- I get an error here
int main()
{
return foo<false>();
}
The error output by compiler:
<source>(12): error #1011: missing return statement at end of non-void function "foo<B>() [with B=false]"
}
Used -std=c++17 -O3 -Wall -Werror compiler flags and icc 19.0.1 compiler.
Is this valid C++17 code?
What is the reason behind this error?
Is this valid C++17 code?
Yes, it's valid. Exactly one return statement will be discarded, while the other will remain. Even if none remain, C++ still allows you to omit a return statement from a function. You get undefined behavior if the function's closing curly brace is reached, but that's a risk only if execution reaches that point.
In your case, execution cannot reach such a point, so UB is not possible.
What is the reason behind this error?
You used -Werror, thus turning the compiler's false positive warning into a hard error. One workaround is to disable this warning around that particular function. This is purely a quality of implementation problem.
I am compiling the code behind
class Test {
public:
Test() {}
int k;
};
int main() {
Test t;
std::cout << t.k << "\n";
}
like
g/clang++ main.cpp -Wall -Wextra --std=c++14 -o exe; ./exe
Why neither of the compilers does not warn me about indeterminate value of the integer is not it a very serious potential bug? How to enable a warning for indeterminate initializations?
For this example, GCC gives me the desired warning when I give it -O1 (or higher).
Presumably whatever mechanism it uses to detect this is tied into the optimisation effort level somehow. It's a notoriously hard thing to do.
Ensure that you heed your release-build warnings as well as debug-build warnings.
I have to debug a segfault in my C++ program using Code::Blocks.
Unfortunately, the stack trace isn't showing correctly, instead I see ?? ()
Here is a minimal example:
#include <iostream>
using namespace std;
int main()
{
int *plop;
cout << *plop << endl;
return 0;
}
The debugger says:
Program received signal SIGSEGV, Segmentation fault. In ?? () ()
But I was expecting something more useful like "In main ()"
EDIT: here is the build log, if it helps
-------------- Build: Debug in tests (compiler: GNU GCC Compiler)---------------
g++.exe -Wall -fexceptions -g -O -pedantic -Wextra -std=c++0x -std=c++14 -c D:\C\tests\main.cpp -o obj\Debug\main.o
D:\C\tests\main.cpp: In function 'int main()':
D:\C\tests\main.cpp:8:14: warning: 'plop' is used uninitialized in this function [-Wuninitialized]
cout << *plop << endl;
^
g++.exe -o bin\tests.exe obj\Debug\main.o -s
Output file is bin\tests.exe with size 542.00 KB
2nd EDIT: finally solved :)
For those who came here by google : strip symbols -s and Optimizer -O compiler options were checked in my case, theses options conflicts with -g as they removes debug symbols in compiled code.
Thanks for everyone for answering
you must initialize the int *plop; pointer like bellow, then print the value :
#include <iostream>
using namespace std;
int main()
{
int *plop = new int(15);
// *plop = 120; // you can change the plop value as custom
cout << *plop << endl;
return 0;
}
result will be : 15
You are dereferencing an uninitialized pointer. That's undefined behaviour and your program is meaningless.
The compiler is free to generate whatever it pleases (including code causing a segfault or not doing anything at all) - basically, all bets are off and you can trust nothing and you can't even count on your debugger showing you anything sane since it just has to work with what the compiler generated - which could be whatever.
Don't work with uninitialized variables / invoke UB.
Is there a way to know if exceptions have been disabled in C++?
I'm developing an application that uses exceptions and I want to avoid having the user compile the application with exceptions disabled, like a warning that you need exceptions.
The standard certainly does not entertain such a functionality.
Assuming you are using a modern and competent compiler to compile code that contains a throw, then the compiler will error. But you can of course compile one part of the code WITH exceptions, and another without, in which case you still get exceptions thrown, but no way of catching them (since there is no catch either). So the program will probably terminate on first exception being thrown.
If you supply headers, you could just add a dummy function in an unnamed namespace:
namespace {
inline void dummy_dummy_my_thing_to_check_exceptions()
{
throw 123;
}
}
and never even call that function, it will still fail to compile.
I doubt that any compiler that lets you turn off exceptions will accept code that does this - I have tried with g++ 4.8.2, g++ 4.6.3, clang++ 3.6.0 (as of three weeks ago) and clang++ 2.9. All give an error for the above function inside a headerfile. If I remove -fno-exceptions, the code compiles and runs (with a terminate becuase the code does throw 1 in a function.
Total code:
x.h:
extern int func();
namespace {
inline void dummy_dummy_my_thing_to_check_exceptions()
{
throw 123;
}
}
x.cpp:
int func()
{
throw 1;
}
except.cpp:
#include "x.h"
int main()
{
func();
}
Compiles with:
g++ -c except.cpp && g++ except.o x.cpp
or
clang++ -c except.cpp && clang++ except.o,
but won't compile with:
g++ -c except.cpp -fno-exceptions && g++ except.o x.cpp
or
clang++ -c except.cpp -fno-exceptions && clang++ except.o.
given this function:
int doThings() {
int x = 1;
return x;
x + 1;
cout << "x: " << x << '\n';
}
Is there a compiler warning flag (g++ or clang++) that can catch the fact the code after return won't be run?
UPDATE:
Running clang++ with -Wunreachable-code did the trick
The gcc compiler (up to 4.4) had a -Wunreachable-code option which should catch this.
This wasn't part of the -Wall group since you're more likely to have unreachable code during the development process, the time when you're most likely to be using -Wall.
It was removed as of 4.5 due to inconsistencies with the optimiser. By removed, I mean the compiler still accepts the flag but doesn't act on it. I believe Clang still includes that option, since it likes to be compatible with gcc.
Clang provides -Wunreachable-code, which will warn for this code (live example).
GCC also provides this option, but it has been silently disabled since gcc 4.5.
x + 1; does nothing, regardless of whether or not it is ever run. This is caught by -Wunused-value.