If conditions for compiler options

Jan 7, 2012 at 9:24am
Hello guys,

I was once programming an OpenGL simulation on Qt. I wanted to do a static linking to the Qt libraries, so I compiled the Qt libraries all over again but without exceptions for some reason (don't wanna go to that, that's another story).
Now the problem is, that the classes involved in the simulation have exceptions and use them. So when I compiled the program the compiler complained that I'm using exceptions while they're disabled in the compiler command-line.

I would like to find a fancy method for writing my classes, in a way that makes it possible to disable or enable exceptions according to one variable (something like a static or macro variable). So for example, I could define the value of a variable to be 1 and have exceptions (throw commands) enabled, or 0 to have them "overlooked" by the compiler and use simple C return values for debugging which is available anyway whether exceptions are enabled or not. Is that ever possible?

The problem is, that whenever the word "throw" is there in the code with exceptions disabled, the compiler complains. How get some if condition to overlook throw commands so I won't have conflicts like the one I mentioned?

Thank you for any efforts :)
Last edited on Jan 7, 2012 at 9:26am
Jan 7, 2012 at 9:33am
throw command aside, you don't change the way you write your code. C++ exceptions are designed to cope with modules that don't support exceptions.

Now, it could be that one library is expecting a build of another that was built with exceptions one, but in they're off. So that's a legitimate complaint. But that's not a coding issue, it's a build issue.
Jan 7, 2012 at 9:44am
Thank you for your reply.

Yes, exactly. I know it's a build issue. That's why I'm asking whether there's a way to talk to the compiler through the code, and tell it to overlook "throw" commands when some flag is there.

It's not a library issue, btw. The complaint was clearly like "throw command with exceptions disabled".

Is such a solution available?
Last edited on Jan 7, 2012 at 9:46am
Jan 7, 2012 at 10:32am
If there is, it'll be compiler specific switch.
Jan 9, 2012 at 9:45am
Thank you for your answer.

What do you mean with "switch"? please explain further.
Jan 9, 2012 at 11:00am
Usualy there is a macro which define if exceptions are used or not. For example it is __EXCEPTIONS on gcc.
So you can define a macro which tell if there is exception compiler independant and use it to make custom throw/try/catch blocks like this:
1
2
3
4
5
6
7
8
9
10
11
12
#ifdef __EXCEPTIONS
#define EXCEPTIONS_ON
#elif // macro utilise pour MVSC
#define EXCEPTIONS_ON
...
#endif

#ifdef EXCEPTION_ON
#define THROW_IFP(Except, ...) throw Except(__VA_ARGS__)
#elif
#define THROW_IFP(Except, ...) exit(1)
#endif 
Jan 9, 2012 at 11:01am
It's another way of saying "command line option".
Topic archived. No new replies allowed.