Re: Is nobody using c++ and/or plugs-lib? was Re: nios c++ and ethernet [may by ot?]

The one thing I remember that was special was includes were inside an

> "extern "C" { } statement... like this:

And for those C++ "boneheads" wondering what this does: It is equivalent to putting an extern "C" modifier in front of all the function declarations in the contained header file. Why is this needed? Because C++ uses "name-mangling" to turn the user-friendly names you see in your code into nearly unreadable function names that incorporate function name, type information of the arguments, and likely some other stuff. The reason it does this is that you can have overloaded functions that have same name and different argument types, but the compiler wants to generate object code that any old linker can properly link (I think). There are probably other reasons (like, where else would it store the type info?) that I can't think of.

Bottom line, by saying extern "C" you're telling the compiler that the function will not have multiple variants and thus the compiler should use the straight-forward name, which is just the function name with a _ in front (assuming __cdecl calling convention) rather than the mangled name. This is needed because the library you are linking to (or C files) were compiled by a C-compliant compiler that did not mangle the names.

Besides linking to C code, this can be handy when cutting down on the size of the debug information in your executable.

And just in case you're a C/C++ coder and were confusing this with calling conventions (as I was until recently), C & C++ have the same default calling convention (__cdecl). That is, there is no difference in the way they pass function parameters and return values on the stack/registers -- they both pass parameters on the stack with the caller responsible for stack clean-up.

Paul Leventis Altera Corp.

Reply to
Paul Leventis (at home)
Loading thread data ...

Many thanx for that great insight!

Regards g.k.

Reply to
g.k.

This should have been done *inside* plugs.h:

#ifndef _plugs_ #define _plugs_

#ifdef __cplusplus extern "C" { #endif ... #ifdef __cplusplus } #endif #endif // _plugs_

This type of structure is used in excalibur.h so it should have been done in plugs.h too (at least for consistency).

Petter

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
 Click to see the full signature
Reply to
Petter Gustad

Yes, it should be. I will file a Software Problem Report on this today.

Paul Leventis Altera Corp.

Reply to
Paul Leventis (at home)

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.