In my quest to learn C++, I have come across dynamic and static libraries.
I generally get the gist of them: compiled code to include into other programs.
However, I would like to know a few things about them:
- Is writing them any different than a normal C++ program, minus the
main()function? - How does the compiled program get to be a library? It’s obviously not an executable, so how do I turn, say ‘test.cpp’ into ‘test.dll’?
- Once I get it to its format, how do I include it in another program?
- Is there a standard place to put them, so that whatever compilers/linkers need them can find them easily?
- What is the difference (technically and practically) between a dynamic and static library?
- How would I use third party libraries in my code (I’m staring at
.dyliband.afiles for the MySql C++ Connector)
Everything I have found relating to libraries seems to be targeting those who already know how to use them. I, however, don’t. (But would like to!)
Thanks!
(I should also note I’m using Mac OS X, and although would prefer to remain IDE-neutral or command-line oriented, I use QtCreator/Netbeans)
No.
Pass the
-dynamiclibflag when you’re compiling. (The name of the result is still by defaulta.out. On Mac OS X you should name your dynamic libraries aslib***.dylib, and on Linux,lib***.so(shared objects))First, make a header file so the the other program can
#includeto know what functions can be used in your dylib.Second, link to your dylib. If your dylib is named as
libblah.dylib, you pass the-lblahflag to gcc./usr/libor/usr/local/lib.Basically, for a static lib, the whole library is embedded into the file it “links” to.
See the 3rd answer.