如何让你的头周围的C ++联/依赖呢? [英] How to get your head around C++ linking/dependencies?

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

问题描述

我是一个Java开发人员,我从来不必担心,包括文件或构建文件搞乱。

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

每当我需要编写C ++ code,事情变得更加复杂。我能想到的创建* .h文件作为Java接口,但搞清楚如何编写构建文件,并应包含什么样的顺序班让我头疼的。

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.

有一些框架,使创建构建文件或管理所有这些样板编译东西更惬意?

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 ,看你能不能只用类Foo的声明中脱身;

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 文件,将生成一个的.o 文件,其中包含的实施正是这些功能的的.cpp 定义的。引用没有定义其中的功能留给链接器来解决。

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.

链接器将所有这些定义组合成一个可执行文件,但每个函数的定义必须是present一次。 (模板和内联函数是一个例外。)

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天全站免登陆