允许我的 C++与 C 一起使用的库(减少的函数集)

发布于 10-27 17:03 字数 164 浏览 2 评论 0原文

我一直想构建一个共享库,其中将利用类来保持其功能干净(它可能需要许多输入或处理),尽管我仍然希望针对 C 平台。

如果我在所有原型上应用 extern "C" {} ,并在示例中提供一组模仿类函数的公开函数,以便对象不需要“需要”使用我的库,那么这些普通函数是否可以在 C 程序中工作会链接到它吗?

I've been wanting to build a shared library of which will utilize classes to keep its functioning clean (it may require many inputs or processing), although I still wish to target for C platforms.

If I apply extern "C" {} over all my prototypes, and provide in example a set of exposed functions that mimick the class functions so that objects are not "required" to use my library, will those normal functions work in C programs that will link to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零崎曲识2024-11-03 17:03:37

如果您以您所描述的方式创建一个 dll,即所有接口都是 extern“C”并且接口中的所有类型都是 POD,那么是的,您在 C 或 .NET 中使用它实际上不会有任何问题

示例:

class MyClass
{
 public:
    MyClass(float x) {...}
    void method(int x) {...}
    ~MyClass() {...}
};

#define MYAPI extern "C" __declspec(dllexport) //or whatever the syntax is for your compiler

MYAPI MyClass* CreateMyClass(float x)
{
    return new MyClass(x);    
}

MYAPI void MyClass_method(MyClass* p, int x)
{
   return p->method(x);
}

MYAPI void DestroyMyClass(MyClass* p )
{
   delete p;
}

If you create a dll in the fasion you described, that is, all interfaces are extern "C" and all types in interfaces are POD's then yes, you'll practically have no problem using it in C or .NET

Example:

class MyClass
{
 public:
    MyClass(float x) {...}
    void method(int x) {...}
    ~MyClass() {...}
};

#define MYAPI extern "C" __declspec(dllexport) //or whatever the syntax is for your compiler

MYAPI MyClass* CreateMyClass(float x)
{
    return new MyClass(x);    
}

MYAPI void MyClass_method(MyClass* p, int x)
{
   return p->method(x);
}

MYAPI void DestroyMyClass(MyClass* p )
{
   delete p;
}
樱娆2024-11-03 17:03:37

您可能需要为 C 源代码包含单独的头文件。 C 标头不应包含 C 编译器可能需要解析的任何 C++ 代码。在那里,您将向 C++ 库提供 C 接口。通过向此标头中的函数声明提供 extern "C" 修饰符(正如您所指示的那样),您可以告诉编译器使链接适用于 C 程序。

因此,如果您需要 C 程序保存对对象的引用,只需将其作为 void * 指针通过 C 接口来回传递即可。在 C 语言中,你只需将其视为一块神奇的饼干。

其他帖子谈到通过 DLL 公开接口,这也是有用的知识,但严格来说并不涉及(标准)语言方面。

编辑

哎呀。 extern "C" 部分是 C++ 语法。因此,在 C++ 端,您必须使用 extern "C" 声明 C 接口函数,但在 C 端,您不能使用该语法。您可以通过常见的习惯用法来实现这一点:

#ifdef __cplusplus
extern "C" {
#endif
void foo();
#ifdef __cplusplus
}
#endif

You may need to have separate header files for your C sources to include. The C headers should not have any C++ code that the C compiler might be expected to parse. In there, you would provide the C interface to your C++ library. By providing the extern "C" modifier, as you indicate you would do, to the function declarations in this header you tell the compiler to make the linkage work for C programs.

So if you need the C program to hold a reference to an object, just pass it back and forth through the C interface as a void * pointer. In C you just treat it as a magic cookie.

Other posts speak to exposing the interface through a DLL, which is also useful knowledge but not strictly speaking to the (standard) language aspects.

Edit

Aw heck. The extern "C" part is C++ syntax. So, on the C++ side you must declare the C interface functions with extern "C" but on the C side you can't have that syntax. You can make this happen by the common idiom:

#ifdef __cplusplus
extern "C" {
#endif
void foo();
#ifdef __cplusplus
}
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文