I'd like to know what is the difference and also what is better, create a static library with the common code I need or just import files to the project.
What is the difference between create a static library and just import the code?
227 views Asked by Leandro Lima AtThere are 3 answers
You mean manually move the code over? I think the difference is during linking, compiler optimization is slightly different. Static library is essentially "copy over the exposed code" which happens during compile time.
It is more a form of code reuse to me. Dynamic library on the other hand, is more complicated and flexible as the functions are called at runtime only. As a result, programs linked with dynamic library has less information in binary comparing to those compiled with static library using exact same source. I did comparison myself a while ago.
And yes, Scott is absolutely right regarding code distribution.
One other difference in embedded projects is that you may want to compile the library with different project settings, so that would be one advantage. However, the optimizer may be able to do more with using registers to pass variables, etc if all the source is in the project.
Embedded compilers can be very terrible, and its the case that sometimes you have to hand feed them. (I remember one 8051 c compiler that wouldn't even do constant expression folding, uggh.)
With a library, you can distribute the code w/o the source. If you have the source, then your compiler has the opportunity to optimize that code.
"Better" depends on what your criteria are.