简体   繁体   English

在c ++中快速编译

[英]fast compilation in c++

I have a c++ program which has many many functions and I have different .cpp files for each of the function. 我有一个c ++程序,它有许多功能,我为每个函数都有不同的.cpp文件。 From the main program, I only supply a few parameters and just call the functions. 从主程序,我只提供一些参数,只需调用函数。 However, the compilation of the full thing takes a lot of time. 然而,整篇文章的编译需要花费很多时间。 For each compilation I only change a few parameters in the main program and leave all the functions as it is. 对于每个编译,我只更改主程序中的一些参数,并保留所有功能。 Is there anyway to speed up the compilation.? 无论如何都要加快编译速度。

You are recompiling unnecessary code. 您正在重新编译不必要的代码。 Usually IDEs handle this automatically. 通常IDE会自动处理此问题。 Otherwise, it depends on how you compile your code. 否则,这取决于您编译代码的方式。 For example lines like this: 例如这样的行:

g++ *.cpp

or 要么

g++ -o program a.cpp b.cpp c.cpp

are terribly slow, because on every compilation, you recompile everything. 非常慢,因为在每次编译时,你都会重新编译一切。

If you are writing Makefiles, you should carefully write it to avoid recompilation. 如果您正在编写Makefile,则应仔细编写它以避免重新编译。 For example: 例如:

.PHONY: all
all: program

program: a.o b.o c.o
    g++ -o $@ $^ $(LDFLAGS)
%.o: %.cpp
    g++ $(CXXFLAGS) -o $@ $<
# other dependencies:
a.o: a.h
b.o: b.h a.h
c.o: c.h

In the above example, changing c.cpp causes compilation of c.cpp and linking of the program. 在上面的例子中,改变c.cpp引起的汇编c.cpp程序的和链接。 Changing ah causes compilation of ao and bo and linking of the program. 改变ah导致编译aobo以及程序的链接。 That is, on each build, you compile the minimum number of files possible to make the program up-to-date. 也就是说,在每个构建中,您可以编译可能的最小文件数以使程序保持最新。

Side note: be careful when writing Makefiles. 旁注:编写Makefile时要小心。 If you miss a dependency, you will may not compile enough files and you may end up getting hard-to-spot segmentation faults (at best). 如果您错过了一个依赖项,您可能无法编译足够的文件,并且最终可能会遇到难以分析的分段错误(充其量)。 Take a look also at the manual of gcc for -M* options where you can use gcc itself to generate the dependencies and then include the generated output in the Makefile . 另请参阅gcc for -M*选项手册,您可以使用gcc本身生成依赖项,然后在Makefile include生成的输出。

  • Try to minimize the code impacted by your parameter changes, ideally only change one source file no one depens on (main.cpp). 尝试最小化受参数更改影响的代码,理想情况下只更改一个没有人依赖的源文件(main.cpp)。
  • Check your includes: do you really need it all? 检查你的包括:你真的需要它吗? Use forward declaration where possible (eg #include instead of ), for your own classes, forward declare what you can. 尽可能使用前向声明(例如#include而不是),对于您自己的类,转发声明您可以做什么。
  • Try using the clang ( llvm.org ) compiler. 尝试使用clang( llvm.org )编译器。 It sometimes compiles faster than gcc (assuming you're on linux/unix) and gives more readable errors. 它有时比gcc编译得更快(假设你在linux / unix上)并且提供更多可读错误。

Edit: I was assuming you were only recompiling what's needed. 编辑:我假设你只是重新编译需要的东西。 As others suggested, use a buildsystem (Makefile, IDE, CMake...) to run a minimal number of compiles. 正如其他人所建议的那样,使用构建系统(Makefile,IDE,CMake ...)来运行最少数量的编译。

Maybe this will or won't help much, but I run code through ssh and I know that it takes forever to run/compile. 也许这会有用或者没有用,但我通过ssh运行代码,我知道运行/编译需要永远。 If you are reading from data files, instead of running entire sets of data, run only over a file or two to see your intended result. 如果您正在读取数据文件,而不是运行整个数据集,则只运行一两个文件以查看预期结果。 This will be a sample of your final result, but should still be accurate (just with less statistics). 这将是您最终结果的样本,但仍应准确(只需较少的统计数据)。 Once you've tweaked your code to work to your satisfaction, then run everything. 一旦你调整了你的代码,让你满意,然后运行一切。 usually you will have no problems that way, and your compile time is much quicker comparatively speaking. 通常你会遇到这样的问题,而且相对而言你的编译时间要快得多。

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

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