Compiler Issues

From WxWiki
Jump to: navigation, search

GCC

Proper use of pragma interface and pragma implementation

Warning: this section is probably outdated. Someone who knows this topic in depth needs to review this information and correct as necessary.

  • Note: It is not advised to use pragma interfaces at all, as for any reasonable gcc version (2.95 and up, or so) it perhaps only reduces debug enable build sizes, but other than that can lead to all kind of various problems. wxWidgets-2.7 development series will stop using these pragmas as well, until which time the chosen interface names could collide with application chosen names and cause even more interesting problems. - MR

By default, GCC will include debug code about every other .cpp file into each and every object file. This makes compile times, code size, and debug speed all worse.

It is thus strongly recommended to use the interface/implementation pragmas. There needs to be a interface directive in each header and a implementation directive in each .cpp file of your project. For example, say you had a PreferencesDialog class that you described in prefsdlg.cpp and prefsdlg.h:

  • Put a pragma interface "prefsdlg.h" at the top of the prefsdlg.h file (just below the standard #ifdef __PREFSDLG_H__ that you use to only include that header file once).
  • (Nitpicking here, but: You really shouldn't use identifiers like __PREFSDLH_H__ in your code. Identifiers containing __, or starting with _ and a capital letter (like _T), or _ followed by anything at global scope, are reserved to the implementation (compiler) and shouldn't be used.)
  • Put a pragma implementation "prefsdlg.h" at the very top of the prefsdlg.cpp file

Each of the pragma interface/implementations should go inside an ifdef so that they are only used by GCC.

Now the compiler knows when it includes prefsdlg.h from some other .cpp file besides prefsdlg.cpp, that it shouldn't worry about including any debugging info, that it can instead wait, and the debug info can just be done once in prefsdlg.cpp

Note that if you ever get a "virtual table" error on GCC, this is nothing to do with event tables. It means you haven't set up your pragma interface/implementation correctly.

Strange error about conversion

Was porting xMule to win32 using Cygwin, and got this strange error:

 invalid conversion from `int (*)(long int, long int, long int)` to `int(*)(long int, long int, long int)`

The line causing this error was:

 SortItems(SortProc, sortItem + (sortAscending ? 0:100));

What came out that SortProc was a function defined as follows:

 static int SortProc(long lp1,long lp2,long lpSort);

And that caused such strange error. Solution was to change the SortProc function definition to

 static int wxCALLBACK SortProc(long lp1,long lp2,long lpSort); 

I don't know why it had to be done like this, but I someone figures out *why*, then by all means, edit this text :).

  • The reason is that wxCALLBACK changes the calling convention from the default to __stdcall (on Windows). The rather useless gcc error is indicating (or failing to) that the calling conventions differ.