473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 11544
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**@schwabcenter.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**@schwabcenter.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**@schwabcenter.comwrote:
Ole Nielsby wrote:
>Jeff Schwab <je**@schwabcenter.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 "lightweight 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...@tekare-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 objektorientierter Datenverarbeitung
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
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...
8
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...
1
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...
11
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
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...
4
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 >...
2
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...
10
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...
16
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...
2
by: Builder | last post by:
Hi, I imported following COM interface DECLARE_INTERFACE(IAxMediaCB) { STDMETHOD(BufferCB) ( LONGLONG theStartTime,
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.