引用你没有的类#include'd(C ++)

时间:2021-10-10 15:08:17

I'm playing around with Box2D for fun right now, and after getting the hang of some of the concepts I decided to make my own test for the test bed (Box2D comes with a set of examples and has a simple extendable Test class for making your own tests). I started by grabbing one of the other tests, ripping out everything but the function signatures, and inserting some of my own code.

我正在玩Box2D以获得乐趣,在获得一些概念后,我决定自己测试一下这个测试床(Box2D带有一组示例,并且有一个简单的可扩展测试类用于制作你自己的测试)。我开始抓住其他测试之一,除了函数签名之外的所有内容,并插入一些我自己的代码。

However, there's no #includes to any of Box2D's libraries so it doesn't compile (but only my file errors, remove my test file and it compiles fine). I figured I must have accidentally deleted them when I was moving stuff around, but upon checking the other test files there's no #includes anywhere to be seen. Each one of the files uses datastructures and functions that are declared in various Box2D header files. How does this compile at all?

但是,任何Box2D的库都没有#includes,所以它不能编译(但只有我的文件错误,删除我的测试文件,它编译得很好)。我想我在搬东西时一定是不小心删掉了它们,但是在查看其他测试文件时,没有任何#includes可以看到。每个文件都使用在各种Box2D头文件中声明的数据结构和函数。这怎么编译?

For example, this is one of the prepackaged tests stripped of the constructor body and some comments at the top:

例如,这是剥离构造函数体的预打包测试之一,顶部有一些注释:

#ifndef CHAIN_H
#define CHAIN_H

class Chain : public Test
{
public:
    Chain()
    {
        // Since b2BodyDef isn't defined in this file, and no 
        // other files are included how does this even compile?
        b2BodyDef bd; 


        // rest of constructor...
    }

    static Test* Create()
    {
        return new Chain;
    }
};
#endif

3 个解决方案

#1


Each cpp file gets compiled. Before it is compiled though, the preprocessor runs. The preprocesser deals with all of the keywords starting with #, like #include. The preprocessor takes the text of any #include'd files and replaces the #include statement with all the text in the file it includes. If the #include'd file includes other files, their text is fetched too.

每个cpp文件都被编译。在编译之前,预处理器会运行。 preprocesser处理所有以#开头的关键字,如#include。预处理器获取任何#include'd文件的文本,并将#include语句替换为它包含的文件中的所有文本。如果#include'd文件包含其他文件,则也会获取其文本。

After the preprocessor has run, you end up with a great big text file called a translation unit. This is what gets compiled.

在预处理器运行之后,您最终会得到一个称为翻译单元的大文本文件。这是编译的内容。

So.. probably as other people have said. The cpp file somewhere includes the Box2D stuff before it includes chain.h, so everything works. There is often an option on the compiler or the project settings that will get the preprocessor to create a file with all of the text in the translation unit so you can see it. This is sometimes useful to track down errors with #includes or macros.

所以..可能正如其他人所说的那样。某个地方的cpp文件包含了包含chain.h之前的Box2D内容,所以一切正常。编译器或项目设置上经常有一个选项可以让预处理器创建一个包含翻译单元中所有文本的文件,以便您可以看到它。这有时对于使用#includes或宏来追踪错误很有用。

#2


Perhaps the the header that does define b2BodyDef is #included in the .cpp before this header? There are obviously other headers involved, or you would not be able to refer to class Test.

也许定义b2BodyDef的标题在此标题之前的.cpp中是#included?显然还有其他标题,或者您无法引用类Test。

#3


The #ifndef CHAIN_H at the beginning is a common pattern that indicates to me that this is from a file called chain.h.

开头的#ifndef CHAIN_H是一个常见的模式,它向我表明这是来自一个名为chain.h的文件。

Header files like the chain.h you're quoting are not intended to be stand-alone compilable. Most likely you need to create a simple C++-file which includes this and the necessary other files before it:

像你引用的chain.h这样的头文件并不是独立的可编译文件。您很可能需要创建一个简单的C ++文件 - 其中包含此文件和必要的其他文件:

test.cpp:

#include "box2d.h"
#include "chain.h"

// more code here.

#1


Each cpp file gets compiled. Before it is compiled though, the preprocessor runs. The preprocesser deals with all of the keywords starting with #, like #include. The preprocessor takes the text of any #include'd files and replaces the #include statement with all the text in the file it includes. If the #include'd file includes other files, their text is fetched too.

每个cpp文件都被编译。在编译之前,预处理器会运行。 preprocesser处理所有以#开头的关键字,如#include。预处理器获取任何#include'd文件的文本,并将#include语句替换为它包含的文件中的所有文本。如果#include'd文件包含其他文件,则也会获取其文本。

After the preprocessor has run, you end up with a great big text file called a translation unit. This is what gets compiled.

在预处理器运行之后,您最终会得到一个称为翻译单元的大文本文件。这是编译的内容。

So.. probably as other people have said. The cpp file somewhere includes the Box2D stuff before it includes chain.h, so everything works. There is often an option on the compiler or the project settings that will get the preprocessor to create a file with all of the text in the translation unit so you can see it. This is sometimes useful to track down errors with #includes or macros.

所以..可能正如其他人所说的那样。某个地方的cpp文件包含了包含chain.h之前的Box2D内容,所以一切正常。编译器或项目设置上经常有一个选项可以让预处理器创建一个包含翻译单元中所有文本的文件,以便您可以看到它。这有时对于使用#includes或宏来追踪错误很有用。

#2


Perhaps the the header that does define b2BodyDef is #included in the .cpp before this header? There are obviously other headers involved, or you would not be able to refer to class Test.

也许定义b2BodyDef的标题在此标题之前的.cpp中是#included?显然还有其他标题,或者您无法引用类Test。

#3


The #ifndef CHAIN_H at the beginning is a common pattern that indicates to me that this is from a file called chain.h.

开头的#ifndef CHAIN_H是一个常见的模式,它向我表明这是来自一个名为chain.h的文件。

Header files like the chain.h you're quoting are not intended to be stand-alone compilable. Most likely you need to create a simple C++-file which includes this and the necessary other files before it:

像你引用的chain.h这样的头文件并不是独立的可编译文件。您很可能需要创建一个简单的C ++文件 - 其中包含此文件和必要的其他文件:

test.cpp:

#include "box2d.h"
#include "chain.h"

// more code here.