混合来自不同编译器的 C++ 代码 [英] Mixing C++ code from different compilers

查看:37
本文介绍了混合来自不同编译器的 C++ 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想将两个项目链接在一起:

Suppose I have two projects that I would like to link together:

  • 使用 Visual C++ 编译为 DLL 文件的 C++ 库.
  • 使用 C++ Builder 编译的 C++ 可执行文件,使用库中的类.

我意识到没有标准的 C++ ABI,任何将这两个 C++ 项目直接链接在一起的尝试都将失败.什么是创建允许我完成此操作的兼容层的好的自动化方法?

I realize that there is no standard C++ ABI and that any attempts to directly link these two C++ projects together will fail. What is a good, automated way of creating a compatibility layer that allows me to accomplish this?

例如,可以想象 C++ 库可以通过 C 接口暴露自己.然后可执行文件将有一些 C++ 类,这些类包装了 C++ 库公开的 C 接口.由于有一个用于 C 的标准 ABI,所以它会起作用.

For example, conceivably the C++ library could expose itself via a C interface. Then the executable would have some C++ classes that wrap the C interface exposed by the C++ library. Since there is a standard ABI for C, it would work.

唯一的问题是如何自动创建 C 接口和 C++ 包装类 - 手动维护这不是一个选项.SWIG 项目看起来很有希望,但不幸的是,C++ 并不是他们网站上列出的 SWIG 的出口之一.有没有办法用 SWIG 做我想做的事?或者除了 SWIG 之外还有其他项目可以帮助我完成这项任务吗?

The only question is how to automatically create the C interface and C++ wrapper classes - manually maintaining this would not be an option. The SWIG project looks promising, but unfortunately, C++ is not one of the exits of SWIG listed on their web site. Is there a way to do what I want with SWIG? Or is there another project other than SWIG that would help me with this task?

还是我的方法不对?

核心 C++ 库旨在跨平台.显然,可执行文件是特定于 Windows 的.我不想将核心库污染到无法在其他平台上编译的程度.

The core C++ library is intended to be cross-platform. The executable, obviously, is Windows-specific. I don't want to pollute the core library to the extent that it becomes impossible to compile it on other platforms.

推荐答案

如果它只需要在 Windows 上运行,我会将这些类公开为 COM 对象.它们仍将位于 DLL 中,并且可以被任何理解 COM 的语言使用.

If it only has to run on Windows, I would expose the classes as COM objects. They'll still be in a DLL and they can be used by any language which understands COM.

这篇关于混合来自不同编译器的 C++ 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆