Advertisement

External linking of a class

Started by April 04, 2001 03:33 AM
3 comments, last by cow_in_the_well 23 years, 10 months ago
Okay, for my engine I want to break it up into parts. One part (compiled as AppCore.lib) will have WinMain in it. When you make a game with this engine i want to make it so u just include the AppCore.lib so no winmain setup is required. The winmain would look something like this:

int WinMain (blah blah blah)
{
  CApp *App = new CApp;


  App->DoYourThang();


  delete App;

 return 1;
}
 
How would you link the CApp created in AppCore.lib to its class implementation in the program which links to AppCore.Lib? dunno if this makes any sence at ALL, but hey.... ----------------------------- -cow_in_the_well ''When in doubt, empty your magazine.'' - Murphy''s Combat Law

- Thomas Cowellwebsite | journal | engine video

well, even though i''m not fully understanding your question, i can say, it''s the same as linking the direct x libraries.

for your class, you have to include it in your main function source, like, where you have winmain, put your #include "appcore.h" or whatever headers are required for appcore.lib. and of course, type in appcore.lib on the text line in Project/Settings/Link if you''re using vc6

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Advertisement
1. If you are simply calling new and delete, you can simply instantiate it using automatic memory:


CApp App
App.DoYourThing();



Polymorphism will still work.


2. If it were C++, I'd advise you to make WinMain a function template:


template
int WINAPI WinMain(...)
{
T t;
t.DoYourThing();

return 0;
}



Unfortunately, you can't make WinMain a function template due to name-mangling. The easiest solution is to place this code in your library:


// Pointer used by WinMain and other parts of the lib
class CApp* p;

// Base for all user apps, this will work fine:
class CApp
{
public:
CApp() { if( p ) throw exception("only one instance of CApp is allowed!"); p = this; }
~CApp() { p = 0; }
};

int WINAPI WinMain(...)
{
if( not p ) throw exception("you must have a global instance of CApp!");
p->DoYourThing();

return 0;
}



And this is what the user will do:


class CUsersApp : public CApp {};

CUsersApp theApp; // sets lib's p, allows WinMain to use it



Now the user doesn't have to define his own WinMain. Just be careful what you allow the user to do inside his constructor, as the standard run-time library may not be initialized yet (until execution hits WinMain).

Edited by - null_pointer on April 5, 2001 10:11:17 PM
I don''t know if this helps, but this is how Prophercy3D does it:


// portable main() macro
#ifdef PR_MAIN
extern int PrMain(int argc, char** argv);
int APIENTRY WinMain(HINSTANCE cinstance, HINSTANCE pinstance, LPSTR cline, int cshow)
{
char* argv[] = { "hoaxarg" };
int argc = 1;
return PrMain( argc, argv );
}
#endif


Simply define PR_MAIN before you include the file, and you automatically get a WinMain!

Simon Wilson,
XEOS Digital Development
XEOS Digital Development - Supporting the independant and OpenSource game developers!
well ,

i used a Dll , and put in DllMain

this was the user of the engine can still use winmain , and i could still initialise my stuff..

This topic is closed to new replies.

Advertisement