Dynamic and Static Libraries in C++

后端 未结 5 1849
甜味超标
甜味超标 2021-01-30 14:45

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.

Howeve

5条回答
  •  迷失自我
    2021-01-30 15:16

    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.

提交回复
热议问题