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:
main()
function?.dylib
and .a
files 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)
Is writing them any different than a normal C++ program, minus the main() function?
No.
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'?
Pass the -dynamiclib
flag when you're compiling. (The name of the result is still by default a.out
. On Mac OS X you should name your dynamic libraries as lib***.dylib
, and on Linux, lib***.so
(shared objects))
Once I get it to its format, how do I include it in another program?
First, make a header file so the the other program can #include
to 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 -lblah
flag to gcc.
Is there a standard place to put them, so that whatever compilers/linkers need them can find them easily?
/usr/lib
or /usr/local/lib
.
What is the difference (technically and practically) between a dynamic and static library?
Basically, for a static lib, the whole library is embedded into the file it "links" to.
How would I use third party libraries in my code (I'm staring at .dylib and .a files for the MySql C++ Connector)
See the 3rd answer.