Linking to / from non-C ++ code

I used the snippet found on the Internet for this kind of link and it works. Now I would like to get more understanding on this topic, that is, that I should pay attention to the fact that my C ++ code will be exported / linked to code other than C ++. Can someone point me to any useful resources for this? Thank.

+3
source share
4 answers

The key concepts in interoperability with internal code are name mangling and calling conventions .

But the real point here is that in the general case, if you want your code to be able to be called from other languages ​​(you do not specify in your question), you should take the approach with the lowest common denominator. This usually means that you avoid objects and thinking functionally by wrapping your code in a DLL and using the C style interface. You will probably have to define your api functions for the DLL using the STDCALL calling convention.

In addition, if you use structures in your interface, you have to worry about the packaging structure. For example, to ensure proper interaction with Delphi using packed records, I think you need to set the alignment of the struct element to 1 byte in your C compiler.

+5
source

extern "C" ... . . "", , , "-/".

+5

.... ... #ifdef

#ifdef __cplusplus
extern "C" {
#endif

/* function prototypes and global variable declarations */

#ifdef __cplusplus
}
#endif

__cplusplus, <stddef.h>, , ...

, , , .

0

Source: https://habr.com/ru/post/1727602/


All Articles