C++ Question How exactly does Orbiter... well work?

Still working on this, Im creating a simple SFML test app to get comfortable with the idea of using types from external libraries linked at runtime:

https://github.com/BruceJohnJennerLawso/Dynamo

One part of the API that I am having difficulty making sense of:

Code:
extern "C" {
#include "lua\lua.h"
}

// ... //
#define DLLCLBK extern "C" __declspec(dllexport)

what the heck does that do? I would figure that all that would be needed would be

Code:
#define DLLCLBK extern __declspec(dllexport)

which uses extern so that DLLCLBK is visible everywhere in the source code somehow? But what the heck is the "C" part doing?
 
Last edited:
But what the heck is the "C" part doing?

The "C" part defines how the external method should get called in machine language. There are different calling standards in assembly language, the more popular are "C" and "Pascal"/"stdcall". Basically, the difference between the two and a half methods is, who is responsible for clearing the stack.

In C, the caller is responsible - you can throw as many parameters on the stack as you like, but the called function will only handle those that it is interested in - you for example use this for the printf or scanf functions. After calling the function, you have to clean up the stack again from the parameters, or you will get a stack overflow quickly.

In Pascal, the called function is responsible to remove all parameters from the stack again, so you can't use additional parameters there. stdcall is similar to Pascal, but pushes the parameters in inverse order on the stack, from right to left instead left to right.

Pascal convention was standard on the 16 bit windows versions, stdcall is used since for calling windows functions, C convention is the standard for most C/C++-compiler though.

Every calling style has its advantages and disadvantages, as you can imagine from the fact that they developed in parallel and keep on existing.
 
Last edited:
C++ functions cannot be exported from the DLL, only C functions can be exported. (OK, that's not entirely true, but close enough for now).

extern "C" tells the C++ compiler that it should generate function the C way, not the C++ way (so it can be exported). For details, see http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c

__declspec(dllexport) tells the compiler to generate all the magic needed to make the function DLL-exportable. This includes applying the appropriate calling convention (see Urwumpe's post above). See https://msdn.microsoft.com/en-us/library/z4zxe9k8.aspx for alternative methods of exporting a function from the DLL.
 
Last edited:
Back
Top