I created a library fuction having two fuction inside 1) Loop1() 2) Loop2() when i will call the only one fucnction still the copiler will add the code for both, below i am writing the content of the library file
#pragma SAVE #pragma REGPARMS extern void Loop1(void); extern void Loop2(void); #pragma RESTORE
void Loop1() { unsigned int i=0; while(i) { i--; }
}
void Loop2() { unsigned char i=0; while(i) { i--; } how can i avoid this problem
void main() { Loop1(); Loop2(); while(1);
doesn't that defeat the entire object of a Library
Not in my mind.
It shouldn't be necessary at all, if the tools could manage to avoid pulling unneeded code out of the library in the first place. But, the Keil tools insist on taking entire segments (.c files) as a whole, even if you only need one routine. So, you can follow up with REMOVEUNUSED to trim the code back closer to what it should have been.
The only way to get the Keil tools to behave properly is to put each function in its own .c file (whether you're going to put the .objs in a library or not). This is undesirable for a number of reasons: it's more work, it breaks up the modularity of your program for no good reason, and it inevitably exposes much more to the global name space than if you can keep related code together.
The purpose of a library is to bundle a bunch of OBJs together so that they can be handled as a unit. Many programs might link to the same library, and use various overlapping fractions of it. REMOVEUNUSED doesn't change this purpose, so I wouldn't say it "defeats the purposes" of a library. It just enhances the ability of the linker to trim down the object code without requiring a lot of manual intervention from the programmer.
Not just Keil
It is common behaivior for the Linker to pull objects from the library. Anything above and beyond means the linker is not just resolving addresses. It needs alot more smarts. i.e is that unused, or called via pointer.