Posts: 60
Threads: 11
Joined: Apr 2022
Reputation:
7
I was looking this weekend on how to better manage my libraries. One thought was to include most of my common routines in a single large file and simply load that. What I found was that this caused the .EXE file size to grow considerably. I created a .BAS file that did nothing but a print statement and included this large library, of which zero routines were actually used. The file size was huge compared to using a very small library, of which zero routines were used.
Is there a switch or setting that I can use that will cause unused SUBs and Functions to be excluded at compile time if they are not used?
Posts: 207
Threads: 13
Joined: Apr 2022
Reputation:
52
Usually the linker is smart enough to only link those object files, which contain used references. In C/C++ you would just create one translation unit per function, hence getting a single .o file for every function. You may also collect those single object in a .a file, but even therein it remains a collection of single objects, they will not be concatenated on a binary level, but just saved in one file. However, ALL user code in a .bas program, i.e. all your main code and all SUBs/FUNCs go into one translation unit (doesn't matter if it's in the main file or $INCLUDE'd). The linker is not able to tear that unit to avoid unused code. If just one of the funtions in the unit is referenced, then the unit is linked in whole, and as your main code IS usually referenced it WILL be linked.
Saying that, the only way to achive what you want, is to split your libraries into one SUB/FUNC per include file (or maybe small collections of SUBs/FUNCs which are definitly used together) and $INCLUDE only those which you need in your program. That way you only have the really needed code in the big user code unit. Placing an $INCLUDEONCE in each include file will help to avoid double definition errors, if you nest $INCLUDEs to pull in other required SUBs/FUNCs.
Posts: 128
Threads: 12
Joined: Apr 2022
Reputation:
14
For working with lots of include files it would be awesome if we dropped the requirement that no (main)code is allowed between subs/functions.
Now I often need 2 includes: one with global declarations at the top of main code and one with subs/functions at the end...
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Posts: 60
Threads: 11
Joined: Apr 2022
Reputation:
7
Update:
I used UPX to compress my .EXE and the size went from 5,986,415 down to 2,025,583. A HUGE difference. Thanks for the suggestion!