I'm working with Visual Studio 2005.
I want to compile a simple program that will work with any Windows 32bit version independent of what c++ runtime library version installed.
This program will call to GetModuleHandle
and GetProcAddress
functions without any other function calls, and then exit, when the exit code is the function address.
How to compile a C++ program with only dependency on kernel32.dll and user32.dll, without any c++ runtime library?
You'll need to define your own entry point instead of using main
or WinMain
. Your entry point is a void function taking no arguments. You have to specify its name to the linker with /entry:funcName
(where funcName
gets replaced by whatever name you gave the function you want to use as the entry point).
When you do this you'll also have to specify the subsystem to the linker, as in /subsystem:console
. It normally deduces the subsystem based on the name of function it finds (i.e., main
-> console, WinMain
-> Windows), but when you use your own entry point, you have to specify it explicitly. Although you probably don't want to very often, you can specify the subsystem explicitly even when you don't specify your own entry point, so (for example) you can use main
as the entry point to a windows subsystem program, or WinMain
as the entry point to a console program.
Set /NODEFAULTLIB
under your project options. In newer versions of Visual C++, you'll also have to turn off stack overrun checks, because those cause the compiler to automatically insert calls to library functions.
EDIT: If you really mean "run on ANY 32-bit Windows version", you're also going to have to use editbin
to change the subsystem version field in the PE header. Otherwise you're restricted to (IIRC) Windows 2000 and later when building with the VC++ 2005 linker, and newer versions of VC++ are even worse (requiring XP by default). Windows 2000 is 5.0, you'd want to specify 3.5 or thereabouts to allow all versions of NT in addition to Win9x.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With