dynamic link libraries can be useful. they let you gather functions, variables or classes in libraries you can load and unload at runtime. this keeps your code nicely organized, and easier to manage. however i was unable to find examples how to build and use them effectively, so here is a short intro how to do it.
before we continue, refresh your knowledge of c++ including functions, classes, member methods, function pointers, dynamic and static loading. make shure you know how to complile and link projects with gnu gcc tools, and make shure you know what preprocessor directives represent.
in this intro we will use dynamically loading of dlls.
so let's start. we want an ordinary c++ program to call single function (non-member function) from dll, and return the value. we will use main.cpp as our main program that will call function and print out the result. this should be straightforward.
compile it with
c++ main.cpp -o main.exe
Now we also need the dll:
compile it with
c++ -c dll.cpp
c++ -shared dll.cpp -o dll.dll
try running main, and you will get output 5, just as we wanted. if you remove dll.dll, main program will say: couldn't find library. this is just what we wanted.
tomorrow comes the tricky part. how to export classes to dll and use their objects in your program.
Posted by turbulence at 22.05.06 11:04 | TrackBack