Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force cdecl calling convention for functions declared in specific header file

Tags:

c++

c

visual-c++

Hi My VC2008 project uses the stdcall calling conventions. I have an external library that I am using which has been built with cdecl naming convention, however they didn't mention the calling convention in the function declaration of the functions.

I would like to know if VC has some kind of #pragma or other keyword that would force specific calling convention for the entire header file

kinda like the extern "C" trick but for calling conventions:

extern "C" 
{
#include <file1.h>
#include <file2.h> 
}

Anyone knows of such?

like image 852
Grim Avatar asked Mar 15 '11 14:03

Grim


People also ask

What is the default calling convention for a compiler in C ++?

__cdecl is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions.

What is the difference between Stdcall and Cdecl?

In CDECL arguments are pushed onto the stack in revers order, the caller clears the stack and result is returned via processor registry (later I will call it "register A"). In STDCALL there is one difference, the caller doeasn't clear the stack, the calle do.

What does __ Stdcall mean?

The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl . Functions that use this calling convention require a function prototype. The __stdcall modifier is Microsoft-specific.

What is __ Fastcall in C++?

The __fastcall calling convention specifies that arguments to functions are to be passed in registers, when possible. This calling convention only applies to the x86 architecture.


1 Answers

You can specify calling convention by:

  • Do nothing and you get the default of cdecl.
  • Specify __cdecl explicitly (or perhaps through a macro).
  • Elect to use cdecl throughout a translation unit by compiling with /Gd.

There's no pragma or anything similiar to control calling convention.

like image 78
David Heffernan Avatar answered Sep 21 '22 10:09

David Heffernan