如何让你的头脑围绕C ++链接/依赖? [英] How to get your head around C++ linking/dependencies?

查看:207
本文介绍了如何让你的头脑围绕C ++链接/依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Java开发人员,我从来不必担心包含文件或混淆BUILD文件。

I'm a Java developer and I never have to worry about including files or messing with BUILD files.

每当我需要编写C ++代码时,事情变得更复杂。我可以想到在Java中创建* .h文件作为接口,但是弄清楚如何编写构建文件以及应该包括什么顺序类,这让我很头疼。

Whenever I need to write C++ code, things get more complicated. I can think of creating *.h files as interfaces in Java, but figuring out how to write the build file and what order classes should be included in gives me a headache.

有没有一个简单的方法来想想这个?你怎么知道什么时候包括东西或如何正确地分离东西。例如,通常一个很好的方法来处理一个项目与几十个相互依赖的源文件。

Is there a simple way to think of this? How do you know when to include something or how to separate things out properly. For example, what is generally a good way to deal with a project with dozens of sources files that are interdependent on each other.

有一些框架来创建BUILD文件

Is there some framework to make creating BUILD files or managing all this boilerplate compilation stuff more bearable?

推荐答案

CMake 是我迄今为止能找到的最好的构建系统。你给它一个源文件的列表,它会自动扫描依赖关系,并只重新编译更改的文件。虽然它的语法有点滑稽,文档不是很方便,CMake在可用性和简单性击败GNU自动工具,它可以在所有主要平台上工作。

CMake is the best build system I've been able to find so far. You give it a list of your source files, and it will automatically scan dependencies and recompile only changed files. Although its syntax is a bit funny, and documentation is not very accessible, CMake beats GNU autotools in usability and simplicity, and it works on all major platforms.

心理模型,这里有一些要点。

As to your "mental model" of what's going on, here are some points to keep in mind.


  • A .cpp 文件完全独立于其他 .cpp 文件进行编译。

  • A .cpp file is compiled completely independently of other .cpp files.

编译器从上到下读取 .cpp 文件,只有一次。因此,事情需要按照正确的顺序。

The .cpp file is read by the compiler from top to bottom, only once. Hence, things need to be in the proper order.

A #include 复制/粘贴标题到 .cpp 文件中。

A #include directive is the same as copy/pasting the header into the .cpp file.

,需要该函数的声明,但不一定是定义

At the point where a function is used, a declaration of that function is needed, but not necessarily a definition.

在访问类成员时,需要类的定义。从类派生也需要它的定义。获取指针或引用不需要定义,但需要一个声明。在头部使用这个你的优势:而不是包括 Foo.hpp ,看看你是否可以逃避只有一个声明 class Foo; code>。

At the point where a class member is accessed, a definition of the class is needed. Deriving from a class also requires its definition. Taking pointers or references does not require a definition, but does require a declaration. Use this to your advantage in headers: instead of including Foo.hpp, see if you can get away with just a declaration of class Foo;.

当编译 .cpp 生成包含在 .cpp 中的那些函数定义的实现的.o 文件。

When compiling a .cpp file, a .o file is generated that contains the implementation of exactly those functions defined in the .cpp. References to functions not defined therein are left for the linker to resolve.

链接器将所有这些定义放在一个可执行文件中,但每个函数定义必须是出现一次。 (模板和内联函数是例外。)

The linker puts all these definitions together into an executable, but each function definition has to be present exactly once. (Templates and inline functions are an exception.)

这篇关于如何让你的头脑围绕C ++链接/依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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