从 C 优雅地调用 C++

2022-01-04 00:00:00 compilation c shared-libraries c++

我们用普通的 C (C99) 开发了一些项目.但是,我们有一个库作为 C++ 的源代码(数学库).我们需要这个库所以我想问一下,集成这个源代码的最优雅的方式是什么?

We develop some project in plain C (C99). But, we have one library as source codes (math library) in C++. We need this library so I would like to ask, what is the most elegant way to integrate this source codes?

CC++ 的大小之比为 20:1 所以移到 C++ 不是选项.我们应该使用静态库吗?动态链接库?(这一切都在 Windows 上).

Ratio between sizes of C and C++ is 20:1 so moving to C++ is not the option. Should we use static library? DLL? (It's all on Windows).

推荐答案

根据评论中的讨论,我应该指出将事物分离为 C 兼容的 struct 鸭 和派生的 class Duck 可能是不必要的.您可能可以安全地将实现铲入 structuck 并消除 class Duck,从而避免 real(...).但我不太了解 C++(特别是它与 C 语言世界的交互方式),无法就此给出明确的答案.

Based on discussion in the comment, I should point out that separating things into a C-compatible struct duck and a derived class Duck is probably unnecessary. You can probably safely shovel the implementation into struct duck and eliminate class Duck, thus obviating real(…). But I don't know C++ well enough (in particular, the way it interacts with the C universe) to offer a definitive answer on this.

没有理由不能简单地将所有 C 和 C++ 代码链接到一个二进制文件中.

There is no reason you can't simply link all your C and C++ code together into a single binary.

与 C++ 代码的接口要求您将 C++ API 包装在 C API 中.您可以通过在编译 C++ 代码时在 extern "C" { ... } 中声明一堆函数来实现这一点,而在编译 C 客户端代码时不使用 extern 声明.例如:

Interfacing to the C++ code requires that you wrap the C++ API in a C API. You can do this by declaring a bunch of functions inside extern "C" { ... } when compiling the C++ code, and without the extern declaration when compiling the C client code. E.g.:

#ifdef __cplusplus
extern "C" {
#endif

typedef struct duck duck;

duck* new_duck(int feet);
void delete_duck(duck* d);
void duck_quack(duck* d, float volume);

#ifdef __cplusplus
}
#endif

你可以在你的 C++ 源代码中定义duck 结构,甚至从它继承真正的Duck 类:

You can define the duck struct in your C++ source, and even inherit the real Duck class from it:

struct duck { };

class Duck : public duck {
public:
    Duck(int feet);
    ~Duck();

    void quack(float volume);
};

inline Duck* real(duck* d) { return static_cast<Duck*>(d); }

duck* new_duck(int feet) { return new Duck(feet); }
void delete_duck(duck* d) { delete real(d); }
void duck_quack(duck* d, float volume) { real(d)->quack(volume); }

相关文章