什么问题我可以期待编译C code与C ++编译器? [英] What issues can I expect compiling C code with a C++ compiler?

查看:168
本文介绍了什么问题我可以期待编译C code与C ++编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你把现有的C code碱基,并用C ++编译器编译它,你还能指望什么样的问题冒出来?例如,我认为分配与枚举类型的值的整数,将在C ++中失败了,而这是合法的(如果有点讨厌)的温度。

If you take an existing C code base and compile it with a C++ compiler, what sort of issues can you expect to crop up? For example, I think that assigning an integer to an value with an enumerated type will fail in C++, whereas it's legal (if a bit nasty) in C.

如果我不裹在外部C {...}我所有的C文件,我是不是会得到名称压延,我最不期望它?有一些原因,我真的不应该这样做吗?

If I don't wrap all my C files in extern C { ... }, am I going to get name-mangling where I least expect it? Is there some reason why I really shouldn't do this?

有关的背景下,我们有一个非常大的code碱基C写的。因为我们一直在跳火圈做的事情,会通过C自然而然++几年(homebrewe继承,例如)。我们想开始朝着C ++移动,但在逐步时尚;让我们的CORBA一样的框架来支持它,和重构模块,因为我们走采取更自然的方法C ++将提供优势。

For background, we have a very large code-base written in C. For a few years we've been jumping through hoops to do things that would come naturally via C++ ( homebrewe inheritance, for example). We'd like to start moving towards C++, but in a gradual fashion; getting our CORBA-like framework to support it, and refactoring modules as we go along to take advantage of the more natural approach C++ would provide.

推荐答案

我做了这样的事情一次。问题的主要来源是,C ++将更加严格类型的,因为你嫌。你必须添加的演员在那里的void *与其他类型的指针混合。像分配内存:

I've done something like this once. The main source of problems was that C++ is more strict about types, as you suspected. You'll have to add casts where void* are mixed with pointers of other types. Like allocating memory:

Foo *foo;
foo = malloc(sizeof(*foo));

以上是典型的C code,但它会需要在C ++中铸造:

The above is typical C code, but it'll need a cast in C++:

Foo *foo;
foo = (Foo*)malloc(sizeof(*foo));

有C ++中新的保留字,如阶级,和,布尔,抓,删除,明确的,可变,空间,新,操作员,或,私人,保护,朋友等,这些不能被用作变量名,例如

There are new reserved words in C++, such as "class", "and", "bool", "catch", "delete", "explicit", "mutable", "namespace", "new", "operator", "or", "private", "protected", "friend", etc. These cannot be used as variable names, for example.

以上是可能是最常见的问题,当您编译旧的C code与C ++编译器。对于不兼容的完整列表,请参阅ISO C和ISO C ++ 。

The above are probably the most common problems when you compile old C code with a C++ compiler. For a complete list of incompatibilities, see Incompatibilities Between ISO C and ISO C++.

您还问名字改编。在缺席的externC包装,C ++编译器的将会的裂伤的符号。只要你使用的只有的C ++编译器,不依赖于对dlsym()或类似的东西,从库中拉符号这不是一个问题。

You also ask about name mangling. In absence of extern "C" wrappers, the C++ compiler will mangle the symbols. It's not a problem as long as you use only a C++ compiler, and don't rely on dlsym() or something like that to pull symbols from libraries.

这篇关于什么问题我可以期待编译C code与C ++编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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