473,756 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling convention: is __cdecl cross-platform?

VC has a __cdecl specifier which allows functions and methods to
be called with varying parameter count.

(I understand this is the default for functions in general but in VC,
instances use another convention unless they have an ellipsis
argument.)

I can force GCC and other compilers to use such a convention by
declaring all methods with an ellipsis, but I'd rather not clutter my
method definitions with these.

So, is the __cdecl specifier generally available in some form
(at least on GCC with different CPU types, Mac compilers)
or do I need the ellipsis?

Why I want it:

I'm designing simple plugin model for using C++ plugins in
an interpreted language. I need to handle method calls with
generic code.

It is to be used on MSW as well as embedded Linux.

I know the Qt framework uses a similar technique for its
slot/signal dispatch mechanism - general functions are defined
which take an object and 9 parameters. But I don't know how
Qt makes this work on various compilers.
Oct 27 '08 #1
6 11580
Ole Nielsby wrote:
VC has a __cdecl specifier which allows functions and methods to
be called with varying parameter count.
So, is the __cdecl specifier generally available in some form
(at least on GCC with different CPU types, Mac compilers)
or do I need the ellipsis?
I'm designing simple plugin model for using C++ plugins in
an interpreted language. I need to handle method calls with
generic code.
Rather than passing variable-length argument lists, why not just pass
standard collections of arguments, e.g. in std::vectors?
Oct 27 '08 #2
Jeff Schwab <je**@schwabcen ter.comwrote:
Ole Nielsby wrote:
>VC has a __cdecl specifier which allows functions and methods to
be called with varying parameter count.
>So, is the __cdecl specifier generally available in some form
(at least on GCC with different CPU types, Mac compilers)
or do I need the ellipsis?
>I'm designing simple plugin model for using C++ plugins in
an interpreted language. I need to handle method calls with
generic code.

Rather than passing variable-length argument lists, why not just pass
standard collections of arguments, e.g. in std::vectors?
I want to keep the C++ plugins close to normal C++ style so
that my C++ trained colleagues can create the plugins easily.

Therefore, the plugin host must translate between
as-normal-as-possible C++ style calls and interpreted calls.

The host is compiled without specific knowledge of the signatures
of the plugins.

Frameworks like UNO, COM, Qt... do this. I know Qt uses a
dummy-argument technique, and that's what I intend to do too.
I just need to be sure a caller-pops calling convention is used
for methods - preferably without using the ellipsis in the method
parameters of the plugins.
Oct 27 '08 #3
Ole Nielsby wrote:
Jeff Schwab <je**@schwabcen ter.comwrote:
>Ole Nielsby wrote:
>>VC has a __cdecl specifier which allows functions and methods to
be called with varying parameter count.
So, is the __cdecl specifier generally available in some form
(at least on GCC with different CPU types, Mac compilers)
or do I need the ellipsis?
I'm designing simple plugin model for using C++ plugins in
an interpreted language. I need to handle method calls with
generic code.
Rather than passing variable-length argument lists, why not just pass
standard collections of arguments, e.g. in std::vectors?

I want to keep the C++ plugins close to normal C++ style so
that my C++ trained colleagues can create the plugins easily.
Passing a statically typed container by reference is "normal C++ style."
Circumventing the formal type system (e.g. with ellipses) is unusual.
Therefore, the plugin host must translate between
as-normal-as-possible C++ style calls and interpreted calls.
Right, so far so good. Shouldn't the host know, when it is compiled,
how many arguments each plugin function will expect? Plugins typically
have to conform to a fairly stringent API so that the host will know how
to invoke them.
The host is compiled without specific knowledge of the signatures
of the plugins.
It doesn't need to be too specific, but the quantity of arguments
expected by each function is pretty fundamental.

How about this: If the host provides a function template (rather than a
raw function) for registering the callbacks, then the static type of
each callback will be available to the host, and the number of arguments
can be gathered implicitly as part of the registration. Rather than
just registering a function pointer, the template instantiation (which
knows from the static type system how many arguments the callback
expects) can call an underlying registration function with both the
callback function's address and the number of arguments expected.
Frameworks like UNO, COM, Qt... do this. I know Qt uses a
dummy-argument technique, and that's what I intend to do too.
The only one of those I've used is Qt, and I'm not aware of it using any
kind of "dummy-argument technique." Qt uses a heavyweight preprocessor
(moc) to collect information about function signature before the C++ is
even compiled. The per-signal/slot signature information has to be
maintained at run-time by member lists of the relevant QObjects; that's
why you can only use the signal/slot mechanism with QObjects. It's a
slow, memory-hungry, error-prone technique; for example, the "types" of
function parameters are represented solely by the strings in the
function declarations, without any regard for context. If different
strings are used in the signal and slot declarations (e.g. to use local
typedefs), Qt gets confused. Qt also delays the parameter matching
until the signal/slot connections are attempted, at run-time, so you get
signal/slot mismatch errors at run-time that should have been caught at
compile-time. If you really want to go that route, be very sure you've
got test coverage for every possible connection. (Of course, that would
be difficult, since you're writing a plugin architecture.)
I just need to be sure a caller-pops calling convention is used
for methods - preferably without using the ellipsis in the method
parameters of the plugins.
That's an awfully low-level set of details to be worrying about for
something as high-level as a plugin architecture, which by its nature
should probably be defined as abstractly as possible (so that a wide
variety of client code will work with it).
Oct 27 '08 #4
Ole Nielsby wrote:
VC has a __cdecl specifier which allows functions and methods to
be called with varying parameter count.

(I understand this is the default for functions in general but in VC,
instances use another convention unless they have an ellipsis
argument.)

I can force GCC and other compilers to use such a convention by
declaring all methods with an ellipsis, but I'd rather not clutter my
method definitions with these.

So, is the __cdecl specifier generally available in some form
(at least on GCC with different CPU types, Mac compilers)
or do I need the ellipsis?
No, it's windows specific.

--
Ian Collins
Oct 27 '08 #5
Jeff Schwab <je**@schwabcen ter.comwrote:
Ole Nielsby wrote:
>Jeff Schwab <je**@schwabcen ter.comwrote:
>>Ole Nielsby wrote:
VC has a __cdecl specifier which allows functions and methods to
be called with varying parameter count.
So, is the __cdecl specifier generally available in some form
(at least on GCC with different CPU types, Mac compilers)
or do I need the ellipsis?
I'm designing simple plugin model for using C++ plugins in
an interpreted language. I need to handle method calls with
generic code.
Rather than passing variable-length argument lists, why not just pass
standard collections of arguments, e.g. in std::vectors?

I want to keep the C++ plugins close to normal C++ style so
that my C++ trained colleagues can create the plugins easily.

Passing a statically typed container by reference is "normal C++ style."
Circumventing the formal type system (e.g. with ellipses) is unusual.
A statically typed container is fine if the elements have the same
type, which is not the case here.

Circumventing the type system may be unusual in C++ programming
in general, but when interfacing with dynamically typed languages, it's
bread and butter. Many component systems work that way.
>Therefore, the plugin host must translate between
as-normal-as-possible C++ style calls and interpreted calls.

Right, so far so good. Shouldn't the host know, when it is compiled, how
many arguments each plugin function will expect? Plugins typically have
to conform to a fairly stringent API so that the host will know how to
invoke them.
But I'm going for a generic plugin mechanism. The APIs of individual
plugins are specified by a simple IDL which is interpreted by the host.
(Perhaps I should call it a "lightweigh t component mechanism".
>The host is compiled without specific knowledge of the signatures
of the plugins.

It doesn't need to be too specific, but the quantity of arguments expected
by each function is pretty fundamental.

How about this: If the host provides a function template (rather than a
raw function) for registering the callbacks, then the static type of each
callback will be available to the host, and the number of arguments can be
gathered implicitly as part of the registration. Rather than just
registering a function pointer, the template instantiation (which knows
from the static type system how many arguments the callback expects) can
call an underlying registration function with both the callback function's
address and the number of arguments expected.
I'm having trouble understanding this.

The host and the plugin are compiled as separate projects. In which
of these would the callback registration function template be
instantiated?

If it is instantiated in the host, the host would have to be recompiled
when a new type of plugin is added, and this is precisely what I
want to avoid. The host is agnostic of the details of the plugin
interfaces until it gets the IDL at run time.

If its' instantiated in the plug - well, the template would either have
to construct a middleware layer between the plug and a generic
interface in the host, or somehow pass the type information to
the host in a format the host can interpret.

The "middleware layer" is above my template metaprogramming
skills - I think the PyBoost library does something like that but
I haven't found a description of the technique I understand.

The "type information passing" would not differ much from the
IDL based solution on the host side.
>Frameworks like UNO, COM, Qt... do this. I know Qt uses a
dummy-argument technique, and that's what I intend to do too.

The only one of those I've used is Qt, and I'm not aware of it
using any kind of "dummy-argument technique."
If you check the QMetaObject Class Reference
http://doc.trolltech.com/4.4/qmetaobject.html
you'll find invocation methods that use dummy arguments.
I don't know for sure but it's my understanding that the
signal/slot mechanism uses these methods.
Qt [...] signal/slot mechanism [is] a slow, memory-hungry,
error-prone technique.
Agree, and I don't intend to copy it. I have much faster and safer
ways of doing the dynamic calls. I'm just having a problem with
dummy parameters and calling conventions.
>I just need to be sure a caller-pops calling convention is used
for methods - preferably without using the ellipsis in the method
parameters of the plugins.

That's an awfully low-level set of details to be worrying about
for something as high-level as a plugin architecture, which by
its nature should probably be defined as abstractly as possible
(so that a wide variety of client code will work with it).
"As abstractly as possible" may be fine when you combine things
at compile time, in an all C++ setting. But under the conditions
of having a host that is compiled to executable code without
knowledge of the plug API details, a low level interface is
required.

Btw what led me to design the plugin scheme was this case:

A collegue of mine implemented a glucose level simulation for
diabetes patients using Mathlab and some tools that produced
C code. I implemented a GUI for it using my interpreted PILS
language which comes with bindings to the juce GUI framework.
To make it work in a rush, I wrapped the glucose model C
code in a C++ class and fed it to the homebrew interface
generator I used to interface PILS to juce, and compiled it
into the PILS interpreter. But it doesn't really make sense
that my PILS language - which is rather general-purpose
- should come with a glucose simulation - and the app
shows some weird compiler- and optimization dependent
problems (oddly enough, with VC2008/WinXP, debug mode
works fine but release mode needed some optimizations turned
off for it to work, with GCC/Ubuntu, debug mode is flawed but
release mode works fine - this would be easier to diagnose if I
could compile and test the plugin separately.) ... so I need a
plugin system. I investigated UNO, Ice... but they are too
heavyweight, we want to be able to deploy on wrist watches
etc. so I need something light.
Oct 27 '08 #6
On Oct 27, 11:06 am, "Ole Nielsby"
<ole.niel...@te kare-you-spamminglogisk. dkwrote:
VC has a __cdecl specifier which allows functions and methods
to be called with varying parameter count.
__cdecl is not part of C++ (as it's name indicates---names
starting with __ are in the implementor's namespace). Anything
you do using __cdecl is pure VC++: compiled with VC++, it works
as Microsoft specified, and it probably can't be compiled with
any other compiler.
(I understand this is the default for functions in general but
in VC, instances use another convention unless they have an
ellipsis argument.)
You understand wrong. Unless the function parameter list ends
with an elipsis, you must call it with the number and type of
arguments indicated.
I can force GCC and other compilers to use such a convention
by declaring all methods with an ellipsis, but I'd rather not
clutter my method definitions with these.
A lot of compilers (including g++, I think, at least under Linux
or Solaris) only support one calling convention. And use that
convention regardless of whether there is an ellipsis or not, or
whether the function is ``extern "C"'' or not. Other compilers
may change the convention according to the langauge---I've used
compilers which used a different convention for C and C++.
So, is the __cdecl specifier generally available in some form
(at least on GCC with different CPU types, Mac compilers) or
do I need the ellipsis?
It depends on what you want to do. If you want to pass a
variable number of arguments, you need the elipsis.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2323
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between static and non-static class member function? 4. Could it be possible to specify different calling convention for each "global" or each class member function? Even in a single file scope?
8
2963
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
1
1580
by: Mike | last post by:
I want to pass the address of a function to a timeout type class so that it can be used as a callback function. The function I want to use as a callback is defined like... void rf04::rc_proc(void) { } The call to the the function that uses this functions' address is like...
11
1973
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
11
543
by: RichN | last post by:
I am developing a c program in Visual Studio .NET 2003. I also have an Intel(R) Fortran compiler for MVS .NET My fortran sourcecode already existed. I started a new fortran project and chose to create a dynamic link library. The beginning of the fortran code looks like: SUBROUTINE SFTCK3 !DEC$ ATTRIBUTES DLLEXPORT::SFTCK3
4
2425
by: smarguer | last post by:
I am using Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40121 for AMD64 to port a dll (C, C++ and fortran files) for WS2003. I am working with VS2003, with customized paths in Tools > Options > Project > VC++ Directories in order to correctly target the compiler executables and associated libs/headers (Program Files\Microsoft Visual Cpp\Bin\x86_amd64, etc). I am actually facing a link issue because C symbols are not generated...
2
3150
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean the stack pointer and it does it by the command "add esp,8" after returning from the called function. My questions: 1. Is the stack pointer common in a certain thread(or process)? 2. How does the called function get the parameters, is it by...
10
3262
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a programming system places on how a procedure is called and how data is passed between a calling program and procedures are called calling conventions"
16
3785
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've compiled a DLL of the Python source code using the pythoncode VC++ project in the PCbuild folder of the source download and this works 100% without any warnings etc. I've done this in Debug and Release mode without any problems.
2
2413
by: Builder | last post by:
Hi, I imported following COM interface DECLARE_INTERFACE(IAxMediaCB) { STDMETHOD(BufferCB) ( LONGLONG theStartTime,
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.