从Java / C#透视图了解C ++编译器 [英] Understanding C++ compilers from a Java / C# perspective

查看:103
本文介绍了从Java / C#透视图了解C ++编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个中等经验的Java / C#程序员,我最近开始学习C ++。问题是,我无法理解如何构建各种标头和代码文件。这似乎主要是因为我不知道如何编译器链接的一切。我试过读一些教科书,但我的先入为主的着色我的Java和C#的知识。例如,我很难了解方法等可以在命名空间中定义,而不仅仅是在类定义中。

I'm a moderately experienced Java / C# programmer, and I've recently started learning C++. The problem is, I'm having trouble understanding how to structure the various header and code files. This seems mostly due to my lack of understanding as to how the compiler links everything together. I've tried reading some textbooks, but my preconceptions are heavily colored by my Java and C# knowledge. For example, I'm having a hard time coming to grips with the fact that methods and the like can be defined in a namespace rather than only in a class definition.

我已经找到了大量的C ++ - > Java / C#指南,但实际上没有什么可以去另一种方式。有没有任何好的资源,以缓解Java / C# - > C ++的过渡,特别是在理解编译过程?

I've found plenty of C++ -> Java/C# guides, but practically nothing to go the other way. Are there any good resources out there for easing the Java/C# -> C++ transition, particularly with respect to understanding the compiling process?

推荐答案

C ++常见问题是关于C ++所有特性的优秀资源,但是它可能比你想要的更先进 - 大多数问题(不只是答案)是奥秘,甚至对相当经验的C ++开发人员。

The C++ FAQ is an excellent resource about all the idiosyncrasies of C++, but it's probably a little more advanced than you're looking for -- most of the questions (not just the answers) are mysteries even to fairly experienced C++ developers.

我想如果你google的C ++教程,你会能够找到的东西。你可能还想尝试学习汇编语言(或至少快速介绍一下微处理器中实际发生的事情),因为C和C ++都非常接近硬件的方式。这是他们的速度和力量来自哪里,但它是以一些更好的抽象的价格提供Java提供。

I think if you google for C++ tutorials, you'll be able to find something. You may also want to try learning assembly language (or at least getting a quick introduction as to how things actually happen in a microprocessor), as both C and C++ are quite close to the hardware in the way they do things. This is where their speed and power comes from, but it comes at the price of some of the nicer abstractions Java offers.

我可以尝试回答你的具体问题,但我不知道我会做得多好。

I can try to answer your specific questions asked above, but I don't know how well I'll do.

理解头文件和cpp文件之间的关系的一个关键是理解翻译单元。 Java类文件可以被认为是翻译单元,因为它是编译成二进制形式的基本单元。在C ++中,几乎每个cpp文件都是一个翻译单元(如果你在做奇怪的事情,则有例外)。

One of the keys to understanding the relationship between header files and cpp files is understanding the idea of a "translation unit". A Java class file can be considered a translation unit as it is the basic unit that is compiled into a binary form. In C++, pretty much every cpp file is a translation unit (there are exceptions if you're doing weird stuff).

头文件可能包含在多个翻译单元(并且必须包含在使用标题中定义的任何地方)。 #include指令只是做一个文本替换 - 包含的文件的内容被逐字插入#include指令是。您通常希望在头文件中定义类接口,并在cpp文件中实现。这是因为您不想将实施详细信息暴露给可能包含标题的其他翻译单元。在C ++中,包括类在内的所有内容都不是真正丰富的对象,而只是内存块,编译器通过将相同的头信息编译到每个翻译单元中来分配含义。编译器保证所有翻译单元都具有同样理解什么是内存块。由于编译后缺少丰富的数据,反射等事情是不可能的。

A header file may be included in multiple translation units (and must be included everywhere that uses whatever is defined in the header). The #include directive literally just does a text substitution -- the contents of the included file are inserted verbatim where the #include directive is. You normally want your class interface to be defined in the header file, and the implementation in the cpp file. This is because you don't want to be exposing your implementation details to other translation units that may include the header. In C++, everything, including classes, aren't really rich objects, but just chunks of memory that the compiler assigns meaning to... by compiling the same header information into each translation unit, the compiler guarantees that all the translation units have the same understanding of what a chunk of memory represents. Because of the lack of rich data after compile time, things like reflection are impossible.

C ++构建过程的第二步是链接,编译的翻译单元并查找在翻译单元中使用但在那里未定义的符号(通常是函数调用,但也是变量)。然后,它寻找定义该符号并将它们链接在一起的另一翻译单元,使得对特定函数的所有调用都被定向到定义该符号的翻译单元。

The second step in the C++ build process is linking, which is where the linker takes all the compiled translation units and looks for symbols (usually function calls, but also variables) used in a translation unit but not defined there. It then looks for another translation unit that defines that symbol and "links" them together, so that all calls to a particular function are directed to the translation unit that defines it.

在类方法的情况下,它们必须通过类实例来调用,这在后台只是指向一块内存的指针。当编译器看到这些类型的方法调用时,它输出调用函数的代码,将称为 this 指针的指针隐式传递给函数作为第一个参数。你可以有不属于类的函数(不是方法,正如你所说,因为一个方法是一个类的成员函数,因此不能没有类),因为链接器没有类的概念。它将会看到一个翻译单元定义一个函数,另一个翻译单元调用一个函数并将它们绑定在一起。

In the case of class methods, they must be called through a class instance, which is behind the scenes just a pointer to a piece of memory. When the compiler sees these types of method calls, it outputs code that calls a function, implicitly passing the pointer, known as the this pointer, to the function as the first argument. You can have functions that do not belong to classes (not methods, as you said, because a method is properly a member function of a class and thus cannot exist without a class) because the linker has no concept of a class. It will see a translation unit that defines a function and another that calls a function and tie them together.

这最终会比我预期的更长,是一个过度简化,但它是精确到我的知识和细节提供的水平...希望它帮助一些。至少它应该给你一些googling的起点。

That ended up being a lot longer than I expected, and of course is an oversimplification, but it is accurate to the best of my knowledge and the level of detail provided... hope it helps some. At least it should give you a starting point for some googling.

这篇关于从Java / C#透视图了解C ++编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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