I was looking at nino's latest tiny executables, and trying to see how to link them without linking in freebasic's startup code and default libraries. These are really large, so it's important to be able to get rid of them for tiny exes. The problem is that FB's linker and compiler hard code a few things and that means the startup code always gets linked.
So here's my solution to the problem.
First, write your program as usual, obviously NOT using an built-in FB functions like print, graphics, etc.
Here's my test program (tiny.bas)
#include "windows.bi"
declare function WinMainCRTStartup alias "WinMainCRTStartup"
function WinMainCRTStartup() as integer
messagebox(0,"Very Small FB EXE Sample","Hello",MB_OK)
return 0
end function
compile this using
fbc -c tiny.basThe -c tells the FB compiler just to make a .o file and not an .exe. .o files are the building blocks used by linkers to make executable files.
Next, we need to stub the functions that FB has hardcoded.
I used assembler for this (stub.asm)
.386p
.model flat
.code
public _fb_RtInit@0
_fb_RtInit@0:
end
I assembled it with MASM, but it can be built with lots of other assemblers.
Command line
ml /c /coff stub.asmThis produces stub.obj.
Finally, you need to link these all together. FB is no use for this, you need a proper linker. I've used crinkler, which has the added advantage of compressing while it links. You can download it from
www.crinkler.netThe command line I used to do the link is
crinkler /SUBSYSTEM:WINDOWS /OUT:tiny.exe /LIBPATH:"C:\program files\microsoft platform sdk for windows server 2003 r2\lib" kernel32.lib user32.lib tiny.o stub.obj
You will need the libs from Microsoft Platform SDK to perform this step. You will need to change LIBPATH to point to wherever you have them installed. You can change the name of the output file by changing OUT.
And that produces an exe which is just 605 bytes in size!
Included in the archive are:
All the source from this article, stub.obj for those who don't have MASM, and the final exe.
Crinkler is here
www.crinkler.netHave fun!
Jim