[英]When do I need to #include .cpp files?
One of my homeworks involves three files: LineType.h, LineType.cpp, and Driver.cpp. 我的一项家庭作业涉及三个文件:LineType.h,LineType.cpp和Driver.cpp。 Driver.cpp contains the main() method which uses a class defined by LineType.h and LineType.cpp.
Driver.cpp包含main()方法,该方法使用LineType.h和LineType.cpp定义的类。
On my system, Driver.cpp starts with: 在我的系统上,Driver.cpp开头为:
#include "LineType.h"
#include "LineType.cpp"
#include <iostream>
And the program compiles and runs perfectly when I run g++ Driver.cpp
from within the project directory via the command line. 当我通过命令行从项目目录中运行
g++ Driver.cpp
时,该程序可以编译并完美运行。 However, when my instructor attempts to compile the program (I believe she uses Eclipse), it fails to compile. 但是,当我的讲师尝试编译程序时(我相信她使用Eclipse),则编译失败。 After some back-and-forth, she was able to fix the problem on her end by commenting out one of the #includes from Driver.cpp:
经过反复反复,她能够通过注释掉Driver.cpp中的#includes之一来解决此问题:
#include "LineType.h"
//#include "LineType.cpp"
#include <iostream>
When I attempt to run g++ Driver.cpp
on this edited file, my compiler complains about "Undefined symbols for architecture", which I understand to mean that it cannot find definitions for the class/methods being called. 当我尝试在此编辑的文件上运行
g++ Driver.cpp
时,我的编译器抱怨“体系结构的未定义符号”,我理解这意味着它无法找到所调用的类/方法的定义。
What are my instructor and I doing differently to cause this difference in behavior? 我的老师和我做些什么不同以引起这种行为上的差异? Why does a line required by my compiler cause her compiler to fail?
为什么我的编译器要求的一行导致她的编译器失败?
You should never include source files directly. 您绝对不应直接包含源文件。
Instead, you should list all your source files in the g++
command when you compile: 相反,在编译时应在
g++
命令中列出所有源文件:
g++ Driver.cpp LineType.cpp MyOtherFile.cpp # etc...
Using #include somefilename
means that content of somefilename is put in place of the include. 使用
#include somefilename
意味着somefilename的内容到位的包括。
By putting #include "LineType.cpp"
in your Driver.cpp file you efectively put everythig in one file and then compiling using g++ Driver.cpp
works fine for you. 通过将
#include "LineType.cpp"
放入Driver.cpp文件中,可以将每个文件有效地放入一个文件中,然后使用g++ Driver.cpp
进行编译g++ Driver.cpp
。
When your instructor used IDE for compiling it went on separate compile and linking. 当您的讲师使用IDE进行编译时,它将进行单独的编译和链接。 So it compiled Driver.cpp and LineType.cpp Both files contain definitions from LineType.cpp due to that include.
因此,它编译了Driver.cpp和LineType.cpp这两个文件都包含LineType.cpp中的定义(由于包含)。 So when it came to linking, she had everything definded in LineType.cpp twice and linker didn't know what to do.
因此,在进行链接时,她两次在LineType.cpp中定义了所有内容,链接程序不知道该怎么做。 You can compile and link multiple files at once by using
您可以使用一次编译和链接多个文件
g++ Driver.cpp LineType.cpp
Or using separate compile and linking commands 或使用单独的编译和链接命令
g++ -c Driver.cpp
g++ -c LineType.cpp
Which will generate files Driver.o
and LineType.o
. 它将生成文件
Driver.o
和LineType.o
。 Then you can combine them together by running 然后您可以通过运行将它们组合在一起
g++ Driver.o LineType.o
Personally I strongly do not recommend to include
source files. 我个人强烈建议不要
include
源文件。 But author of this article claims that including source files can reduce large project compilation time in orders. 但是本文的作者声称,包括源文件可以按顺序减少大型项目的编译时间。 He calls this 'unity builds' and claims the approach is widely used in games industry.
他称此为“统一体”,并声称该方法已广泛用于游戏行业。 The main idea of unity build is to reduce the number of modules in a compilation.
统一构建的主要思想是减少编译中的模块数量。 Like this:
像这样:
my_unity_build_1.cpp:
#include "renderer.cpp"
#include "ui_elements.cpp"
#include "gameplay_code.cpp"
#include "character_AI.cpp"
my_unity_build_2.cpp:
#include "file_io.cpp"
#include "cat_dynamics.cpp"
#include "wobbly_bits.cpp"
#include "death_ray.cpp"
Fewer modules mean less duplication common functions and less code generation. 更少的模块意味着更少的重复通用功能和更少的代码生成。 Modules allow to reduce compilation time dramatically but still are not in standard.
模块可以极大地减少编译时间,但是仍然不是标准的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.