Separate compilation units versus Unified Compilation Units for faster compilation, linking and optimized code?

There are several questions that talk about why we should have separate compilation units in order to improve compilation time (for example, not including the code in the hpp file, but only in the cpp files).

But then I found this question:

# include all .cpp files in a single compilation unit?

If we can ignore the issue of maintainability, if we can just look at the compilation time / links, and also optimize the code, what would be the advantages and disadvantages of having only one hpp and cpp file?

Please note that the message is associated with reporting a single cpp file (while there are many header files). I ask what happens if we only have one hpp file and one cpp file .....

EDIT: if we can ignore the fact that changing one line will recompile the entire code, will it be even faster than if 1000 separate files were recompiled from scratch ...

EDIT: I'm not interested in discussing maintainability. I am trying to figure out what speeds up compilation. This question has nothing to do with what is practical, but more related to understanding a simple question:

Will one large hpp and cpp file compile faster than if the code were split into many hpp and cpp files using one core.

: , , . , - , - , , .

EDIT: , , , , , . SO - , , , , .

+4
2

, .

++ 11 ( ++ 14) : GCC Clang/LLVM -flto ( l t ime o ptimiation...). , ( ) . make :

make 'CXX=g++ -flto -O2' 

, :

g++ -flto -O2 -Wall -I/usr/local/include -c src1.cc
g++ -flto -O2 -Wall -I/usr/local/include -c src2.cc
g++ -flto -O2 -Wall src1.o src2.o -L/usr/local/lib -lsome -o binprog

-flto -O2 !

, src1.cc src2.cc . , ( ) src1.cc src2.cc

-flto ( GCC, Clang) , ( Gimple/SSA) , "link-time" ( ) . , .

, -flto ( 2 ) ( ). .

, .

, . GCC5 GCC6, g++ -O2 ( IIRC clang++ -O2) (, / AST, , ), . . , ++ 11 ++ 14 - (, #include <vector> ). BTW, g++ -O0 , g++ -O1 , g++ -O2. (, g++ -g2) . , g++ -O1 -g2 , g++ -O0 ( ).

( !). , : 20 *.cc , 200 *.cc 200 ( , ). *.cc, , class ( ). , , 4KLOC, , .

, ++ "" ( ++ , Ocaml ). , vector<map<string,long>> "" ( ) , <vector> <map <string> vector<map<string,long>>... - - AST s. , vector<map<string,set<long>>> - - , "" vector<map<string,set<double>>>

, , . make -j

, GCC , -ftime-report g++, . this. GCC, -fdump-tree-all .

( Linux- GCC, ):

  • (, make -j g++ , , }, , *.cc). Makefile -s.

  • ( , ); ( ), , . ( #include ).

  • , , . 20 2000 , 200 200 ( , ), . ( , YMMV )

  • , g++ -O0 g++ -O1 g++ -O2 -flto. ( ) g++ -O3 -with -flto...- ( , , g++ -O2). YMMV. -O3. pragmas attributes, , *.cc.

  • , g++ -O1 g++ -01 -g2; (, g++ -g3) gdb, YMMV.

  • , . , , -Wall g++ , , -Wextra , .

  • , , . std::set<std::vector<std::map<std::string,long>>>; PIMPL. (, ) *.cc ( , YMMV).

. clang++ g++. ( ). undefined .

, ++ Java: . YMMV.

PS. . ( ) gcc-melt.org GCC.

+3

Source: https://habr.com/ru/post/1654305/


All Articles