Dynamic and Static Libraries in C++

后端 未结 5 1850
甜味超标
甜味超标 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:22

    Is writing them any different than a normal C++ program, minus the main() function?

    Except for the obvious difference that a library provides services for other programs to use, usually (*) there isn't a difference.

    * in gcc classes/functions are exported by default - this isn't the case in VC++, there you have to explicitly export using __declspec(export).

    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'?

    This depends on your compiler. In Visual Studio you specify this in your project configuration. In gcc to create a static library you compile your code normally and then package it in an archive using ar. To create a shared you compile first (with the -fpic flag to enable position independent code generation, a requirement for shared libraries), then use the -shared flag on the object files. More info can be found in the man pages.

    Once I get it to its format, how do I include it in another program?

    Again this is a little compiler-dependant. In VS, if it's a shared library, when including the class/function you wish to use it should be marked with a __declspec(import) (this is usually done with ifdefs) and you have to specify the .lib file of the shared library for linkage. For a static library you only have to specify the .lib file (no export/import needed since the code will end up in your executable).

    In gcc you only need to specify the library which you link against using -llibrary_name.

    In both cases you will need to provide your client some header files with the functions/classes that are intended for public use.

    Is there a standard place to put them, so that whatever compilers/linkers need them can find them easily?

    If it's your own library then it's up to you. Usually you can specify the linker additional folders to look in. We have a lib folder in our source tree where all .lib (or .a/.so) files end up and we add that folder to the additional folder to look in.

    If you're shipping a library on UNIX the common place is usually /usr/lib (or /usr/local/lib), this is also where gcc searches in by default.

    What is the difference (technically and practically) between a dynamic and static library?

    When you link a program to static libraries the code of the libraries ends up in your executable. Practically this makes your executable larger and makes it harder to update/fix a static library for obvious reasons (requires a new version of your executable).

    Shared libraries are separate from your executable and are referenced by your program and (usually) loaded at runtime when needed.

    It's also possible to load shared libraries without linking to them. It requires more work since you have to manually load the shared library and any symbol you wish to use. On Windows this is done using LoadLibrary/GetProcAddress and on POSIX systems using dlsym/dlopen.

    How would I use third party libraries in my code?

    This is usually accomplished by including the necessary header files and linking with the appropriate library.

    A simple example to link with a static library foo would look like this: gcc main.cpp -o main.o -L/folder/where/foo.a/is/at -lfoo.

    Most open source projects have a readme that gives more detailed instructions, I'd suggest to take a look at it if there is one.

提交回复
热议问题