[OpendTect_Developers] gcc warnings; plugin function definition
Bert Bril
bert at opendtect.org
Fri Apr 22 13:11:09 CEST 2011
Hi all,
Windows developers may not be interested in the first part (either).
I've just checked in a huge bunch of files (in the current CVS, so
expect it in 4.3). Reason is that I've enabled a couple of gcc warnings.
At the moment it's extended to:
-Wimplicit -Wparentheses -Wreturn-type -Wpointer-arith
-Wwrite-strings -Wno-non-template-friend -Winline
-Wformat -Wshadow -Wmissing-declarations -Werror
(busy adding -Woverloaded-virtual)
The new ones are 'shadow' (warns that a certain variable of function is
shadowed by a new declaration) and 'missing-declarations' (warns for
functions appearing without a previous protoype). Both can prevent a lot
of trouble, but have a cost because they also trigger in many harmless
situations.
One of these harmless situations is defining plugin functions like
'InitxxxPlugin()'. Because of missing-declaration you need to have a
prototype first. To make life easier, and even generally improving the
situation, I've made a new header file 'odplugin.h' with macros to
define the plugin functions. This header file will already be in coming
4.2 release. You include it instead of 'plugins.h' (which was a useless
include for plugins anyway).
Here is the body:
/* The following function MUST be defined: */
#define mDefODInitPlugin(pinm) \
mExternC const char* Init##pinm##Plugin( int argc, char** argv )
/* The following function SHOULD be defined: */
#define mDefODPluginInfo(pinm) \
mExternC PluginInfo* Get##pinm##PluginInfo()
/* Define ONLY if your plugin needs early loading (i.e. before any UI)
This is common for 'Batch'-type plugins.
*/
#define mDefODPluginEarlyLoad(pinm) \
mExternC int Get##pinm##PluginType() { return PI_AUTO_INIT_EARLY; }
Note the last one: if your plugin loads late (usually the UI plugins),
then you don't need to define anything. Late loading is the default.
Thus, I changed the tutpi.cc and uitutpi.cc like:
mDefODPluginEarlyLoad(Tut)
mDefODPluginInfo(Tut)
{/* stuff here */ };
mDefODInitPlugin(Tut)
{ /* stuff here */ }
and:
mDefODPluginInfo(uiTut)
{/* stuff here */ };
mDefODInitPlugin(uiTut)
{ /* stuff here */ }
Advantages:
- simpler syntax, easier to understand
- easier to maintain (will automatically work when the warning flags are
strengthened)
Lastly, if you find that these new warning flags in 4.3 are impossible
to have with your code, then you can (this is also new in 4.3) filter
them away by defining something like:
COPTSFILTER := -Wshadow -Wmissing-declarations
Good luck.
Bert
--
-- Bert Bril / OpendTect developer at dGB
-- mailto:Bert.Bril at opendtect.org , http://opendtect.org
More information about the Developers
mailing list