c++ - How Does WinAPI ensure compilation fail when WinMain is not implemented -


when create win32 project in visual studio , dont define winmain function compilation error:

error 1 error lnk2019: unresolved external symbol _winmain@16 referenced in function ___tmaincrtstartup

how api setup designed/implemented ensure user can never create win32 application without implementing winmain? creating api , want put in compilation errors when user hasn't used api or hasn't implemented essential features.

an example of want use similar is: user must implement app class (base or inherited). if can somehow detect @ compilation time app class has not been implemented can throw compilation error.

is winapi's implementation simple forward declaration function hasn't been implemented (which ensures compilation failure)? ie,

// forward declaration with, intentionally, no function implementation int apientry _twinmain(_in_ hinstance  hinstance, _in_opt_ hinstance  hprevinstance, _in_ lptstr  lpcmdline, _in_ int  ncmdshow); 

when starting program, os loads module , calls initialization code. symbol carries initialization code (entrypoint) can changed, typically winmaincrtstartup or maincrtstartup (if remember correctly). now, function supplied compiler vendor , linked program. first calls initialization code, e.g. constructors of globals , delegates various forms of main() or winmain(). here lies reason why compilation fails: since initialization code included (via externally visible entrypoint) , calls according main function, missing main function cause linker error.

note simple declaration not enough (you have tried that), must ensure function declares used.

concerning approach generate errors, don't think it's feasible, @ least not @ compile-time. if possible, try avoid such rigid constraints using library. why shouldn't there multiple such classes or multiple uses library inside application? if constraint makes sense, consider using technique many windowing libraries provide global function registers user-written instance. can add according checks code assert() instance registered.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -