简体   繁体   English

C ++包含和Makefiles

[英]C++ includes and Makefiles

I have a two fold question about working with C++ headers and makefiles. 关于使用C ++标头和makefile,我有两个问题。 I know that I have all the parts, I am just not sure how to put them together. 我知道我拥有所有零件,只是不确定如何将它们组合在一起。

I have the following files: 我有以下文件:

main.cpp
Point.cpp
Point.h

Point.h declares my simple Point object, the definition is inside the Point.cpp file. Point.h声明了我的简单Point对象,该定义位于Point.cpp文件中。 Then inside of main() I want to create and use the Point object. 然后在main()内部,我想创建和使用Point对象。

Do I need to include the Point.h file inside of the Point.cpp file or is that something that the Makefile takes care off? 我是否需要在Point.cpp文件中包含Point.h文件,还是Makefile需要处理的事情? Also, the main.cpp file that is using the Point object: do I need an include for Point.cpp or is that something that is done/can be done in the Makefile? 另外,正在使用Point对象的main.cpp文件:我是否需要Point.cpp的包含,还是在Makefile中已经完成/可以完成的事情?

What should each file here include and what should my Makefile look like? 这里的每个文件应该包括什么,我的Makefile应该是什么样?

//Working from Linux platform exclusive// //从Linux平台开始工作//

Makefiles are nothing more than a way to run certain commands when certain files are out of date. Makefile只是在某些文件过期时运行某些命令的一种方式。 The make program knows next to nothing about C++. make程序几乎不了解C ++。

You should #include "Point.h" in both main.cpp and Point.cpp. 您应该在main.cpp和Point.cpp中都#include "Point.h" If you want to make sure that make rebuilds your program correctly when you edit files, you should explicitly declare main.o and Point.o as dependencies of Point.h. 如果要确保在编辑文件时make正确重建程序,则应显式声明main.o和Point.o作为Point.h的依赖项。

Here's a fairly simple example: 这是一个非常简单的示例:

all : point_app

point_app : Point.o main.o
    $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@

Point.o : Point.h

main.o : Point.h

gcc/g++ and make have tricks that enable you to automatically generate these header file dependencies, but it's been far too long since I've used them for me to offer proper advise on their use. gcc / g ++和make具有使您能够自动生成这些头文件依赖项的技巧,但是自从我使用它们为我提供有关它们使用的正确建议以来,已经太久了。 You could look up the documentation for gcc's -M... family of options as a starting point. 您可以从gcc的-M...系列选项中查找文档。

Makefiles does nothing with your headers. Makefile对标题不执行任何操作。 You need to include in your main manually in order to use point. 您需要手动将其包含在主体中才能使用点。

The header file provides the class definition, therefore wherever you want to use objects of a class, you need to provide it's definition (by including appropriate header file). 头文件提供了类定义,因此,无论您想在何处使用类的对象,都需要提供它的定义(包括适当的头文件)。

The source (cpp) file doesn't have to be included, unless you need the class implementation. 除非您需要类实现,否则不必包括源(cpp)文件。 That needs to be done for a template class, unless the class is explicitly instantiated. 除非显式实例化该类,否则需要为模板类完成此操作。

Makefiles define rules to build libraries or programs, and doesn't include header files. Makefile定义用于构建库或程序的规则,并且不包括头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM