Choosing which lib automatically at runtime

Started by
13 comments, last by Jx 22 years, 2 months ago
Yes, it would fail if you tried to load 2 DLL''s with the same information at compile time. This is why they are dynamically loaded at run-time.. aka, after you''ve figured out which one you want to use on the system it''s running on. This is also the reason they can have the EXACT same names for classes/variables/etc.. because only ONE gets used at runtime, and neither is refrenced at compile time.

Billy - BillyB@mrsnj.com
Advertisement
But with the method i suggest, you can do either - link with the libs, or load dynamically at runtime.

Hmm... need a second opinion...

Thanks for all the input though tagentz - it''s helpfull to have someone to bounce your ideas off even if we''re getting our wires crossed



Magmai Kai Holmlor:

quote:
You need to abstract at a higher level in the code, and construct two (or more) different functions, each using inlined MMx/SSE/3DNow code.


So Magmai, you don''t thinks it''s worthwhile optimizing just the matrix and vector libraries?

quote:
So instead of deciding which matrix multiplication you use, you decide which physics engine you''re going to use.

You can then either use macros and conditional compilation, or templates to produce the various flavors of the physics engine.


Any examples?
One other thing - with the method tangentz suggested, you HAVE to link with the lib otherwise the linker will complain. So if I can''t link with both libs, but I have to link with one then how will it work?

if you go the dll route (MUCH better route imho). here is how you can do things (and load multiple dlls at the same time.

the trick is not to use an abstract class, but an empty base class which defines the funtions for the most genric method possible.

you create a simple template class. this class MUST contain all functions that all the classes will require. simply have all functions/operators "return 0" (if needed) so thay have a function body. then you base ALL your cpu specifc version on that class. you DO NOT link to the lib of the dll in exe. instead use LoadLibrary() and get the functions using GetFunctionByName() (i think thats the call). now in your exe make a class (if you wish) that does the following:

1. checks the cpu for which dll to use.
2. loads the correct dll, or the generic if none apply.
3. you make all requests for new matrices by using a create function instead of the new operator. this create function is exported by the dll and handles the new for you. you will also need to have a delete function in the dll which will handle the freeing (since things created by dlls should be freed by the dll).

this is how polymorrohism and dlls work. now you can add as many different versions of dlls WITHOUT recompiling the original exe. i use this method in my plugin system for loading images. i create a "house" which holds all the codecs at load time. and the house based on the codecs response to the filename passed will use the approiate class from the correct dll to load the file into a genric image buffer class (width, height, pointer to data, color dpeth) for me to manipulate. this makes everything abbstracted very nicly.

again you NEVER link with the dll libs. you MUST do things dynamicly and load the libraries at runtime yourself (and free them using FreeLibrary). check msdn, its ctaully quite trivial once you wrap it all in a class.
That''s the way i was thinking of doing it - or at least something similar. But tangentz method was different. I was just wondering which was better.

This topic is closed to new replies.

Advertisement