build system performance impact of gcc linking to unused libraries

坚强是说给别人听的谎言 提交于 2019-12-12 04:53:22

问题


I have a code-base similar to the following:

sources:

src/a/b/c.cpp

and unit-tests (these are actually boost unit-test executables):

test/a/b/c_test.cpp

The src tree is used in a single executable target. However c.cpp only uses a sub-set of the library dependencies of that target, e.g. -lx, of -lx -ly -lz.

Similarly, c_test.cpp compiles to a test executable that links to c.cpp's -lx,c.o and a few more additional libraries for testing.

For setting up the build-system, in this case I have two options:

  1. Pair each such executable in the build-system with its own library dependency list. (Painful, but perhaps could be automated by mapping header dependency -> library.)
  2. Or, just use the same library list for all test executables and the main executable. (The easy, painless way).

What is the build-system performance impact of #2 "in the large"? Does it really matter?

Misc: This is with g++ (Debian 4.9.1-16) 4.9.1


回答1:


The answer to this REALLY depends on what those libraries are, but in general, I'd say "probably no big impact, unless the libraries are huge". It is libraries that are ACTUALLY USED that is the problem, particularly if they are large, because all the code from the library has to be copied in.

I just did a small experiment, and compiled a "Hello World" program (using clang++, but it uses the same linker at the backend whether you use clang++ or g++, with the default libraries as per the compiler provides. It took 0.232s as the best time (the first time, because the compiler had to be read from disk took about 2 seconds).

I then added the llvm-config --libs (so the libraries you need when you use the llvm compiler framework). Which turns into this:

-lLLVMLTO -lLLVMObjCARCOpts -lLLVMLinker -lLLVMipo -lLLVMVectorize -lLLVMBitWriter
-lLLVMIRReader -lLLVMAsmParser -lLLVMTableGen -lLLVMDebugInfo -lLLVMOption 
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG 
-lLLVMAsmPrinter -lLLVMX86Desc -lLLVMMCDisassembler -lLLVMX86Info 
-lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMMCJIT -lLLVMRuntimeDyld -lLLVMLineEditor 
-lLLVMInstrumentation -lLLVMInterpreter -lLLVMExecutionEngine -lLLVMCodeGen 
-lLLVMScalarOpts -lLLVMProfileData -lLLVMObject -lLLVMMCParser -lLLVMBitReader 
-lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget 
-lLLVMMC -lLLVMCore -lLLVMSupport

That, when linked in, makes my compiler project into a good 100MB of executable file.

The difference in compile time with the "hello world" program was around 0.04s.

So, I would just keep it simple and maintain one single list of libraries.

I would add however that it does depend on where the files are located. If you are linking against a bunch of files on a very slow file-server, it may take a bit longer to read the "what does this library contain".



来源:https://stackoverflow.com/questions/26747187/build-system-performance-impact-of-gcc-linking-to-unused-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!