将指针从C传递到C ++,反之亦然 [英] passing pointers from C to C++ and vice versa
问题描述
有没有任何提示可以给我关于传递指针到结构,双精度,函数,从一个C程序到一个C ++库和回来?
Is there any tips one can give me about passing pointers to structs, doubles, functions, ... from a C program to a C++ library and back?
推荐答案
假设你在两个不同的库静态或动态编译这些(在Linux上的DLL共享库和其他* nix变体)我最关心的问题如下:
Assuming you're coding these in two different libraries static or dynamic (DLLs on windows shared libraries on Linux and other *nix variants) The biggest concerns I have are as follows:
-
它们是使用相同的编译器编译的。虽然如果所有C ++导出都以C风格的命名约定导出,则不需要这样做,但是需要在两个C ++模块之间对C ++和C ++调用类实例。这是必要的,因为不同的编译器mangle C ++导出不同。
They are compiled with the same compiler. While this isn't necessary if all C++ exports are exported with a C-style naming convention it is necessary for C++ to C++ calls to class instances between the two C++ modules. This is necessary due to how different compilers mangle C++ exports differently.
不要将C ++类作为C结构。它们在封面下是不一样的,即使字段的布局是相同的。 C ++类有一个v表,如果他们有任何虚拟成员;这个v表允许正确调用继承或基类方法。
Do not cast a C++ class as a C struct. They aren't the same under the covers, even if the layout of fields are the same. C++ classes have a "v-table" if they have any virtual members; this v-table allows the proper calling of inherited or base class methods.
C到C或C ++到C ++以及C到C ++ 。确保两者对输出库使用相同的字节对齐。您只能通过阅读您的编译器或开发环境文档来确定这一点。
This is true of C to C or C++ to C++ as well as C to C++. Ensure both use the same byte alignment for the output library. You can only determine this by reading your compiler or development environments documentation.
不要将malloc / free与new / delete混合。更具体地说,不分配具有空闲的新的和空闲存储器的存储器,反之亦然。许多编译器和操作系统在两者之间处理不同的内存管理。
Don't mix malloc/free with new/delete. More specifically don't allocate memory with new and free memory with "free" and vice versa. Many compilers and operating systems handle memory management differently between the two.
传递函数指针:只要它们作为extern C这应该很好。 (你需要引用你的编译器文档,如何确定何时一个头被编译为C或C ++来保持在一个文件,或者你将需要在每个项目中相同的函数声明的两个单独的副本 - I推荐第一个)
Passing function pointers: So long as they are exposed to/from C++ as ''extern "C"'' this should be fine. (You'll either need to reference your compilers documentation on how to determine when a header is being compiled as C or C++ to maintain this in one file, or you will need two separate copies of the same function declaration in each project -- I recommend the first)
传递双重:这是C和C ++中的内置类型,应该处理相同。
Passing doubles: This is a built-in type in both C and C++ and should be handled the same.
如果您必须与C函数共享一个C ++对象的实例,并从C代码中对其进行操作,则公开一组C语言导出的辅助函数,它们调用相应的方法对C ++对象。纯C代码无法正确调用C ++对象上的方法。
If you must share an instance of a C++ object with a C function, and act on it from within C code, expose a set of C-exported helper functions which call the appropriate methods on the C++ object. Pure C code cannot properly call methods on C++ objects.
Pseudocode-ish Example:
// C++ class
class foo {
public:
void DoIt();
};
// export helper declarations
extern "C" void call_doit(foo* pFoo);
extern "C" foo* allocate_foo();
extern "C" deallocate_foo(foo* pFoo);
// implementation
void call_doit(foo* pFoo)
{
pFoo->DoIt();
}
foo* allocate_foo()
{
return new foo();
}
deallocate_foo(foo* pFoo)
{
delete pFoo;
}
// c consumer
void main()
{
foo* pFoo= allocate_foo();
call_doit(pFoo);
dealocate_foo(pFoo);
}
这篇关于将指针从C传递到C ++,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!