简体   繁体   中英

Linkage to/from non C++ code

I did used snippet found from Internet for this kind of linking, and it works. Now I would like to gain more understanding on this topic, ie what should I pay attention to if my C++ code is going to be export/linked by non-C++ code. Could somebody points me to any resources useful for this? Thanks.

use extern "C" functionality... see here . This allows interfacing at the "C" level which is probably much more "cross-platform/language".

The key concepts in native code interoperability are name mangling , and calling conventions .

But the real point here is that in general, if you want your code to be callable from other languages (you don't specify any in your question), you have to adopt a lowest-common-denominator approach. Usually that means avoiding objects and thinking functionally, wrapping your code in DLLs and using a C-style interface. You'll probably have to define your DLL's api functions using the STDCALL calling convention.

Also, if you use structures in your interface, you have to worry about structure packing. To get proper interop with Delphi using packed records, for instance, I think you'd have to set the struct member alignment to 1 byte in your C compiler.

Also.... In conjunction with Bob Moore's answer...you can prevent name mangling by using #ifdef

#ifdef __cplusplus
extern "C" {
#endif

/* function prototypes and global variable declarations */

#ifdef __cplusplus
}
#endif

That __cplusplus directive, found in <stddef.h> is defined as standard to the best of my knowledge...

Hope this helps, Best regards, Tom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM