hi,

what is the difference between static and shared library how do we create them and what is the extension used for both of

Dani AI

Generated

The short answers from , and cover the basics. Missing from the thread are practical build steps, runtime/versioning notes, common pitfalls and quick checks you can use right away. Below are concise, practical examples and troubleshooting tips that fill those gaps.

Common build steps (examples for GNU toolchain):

gcc -c foo.c -o foo.o
ar rcs libfoo.a foo.o
gcc -fPIC -c foo.c -o foo.o
gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0.0 foo.o
ln -s libfoo.so.1.0.0 libfoo.so.1
ln -s libfoo.so.1 libfoo.so

Link an executable against the shared lib and embed an rpath so it finds the library next to the binary:

gcc main.c -L. -lfoo -Wl,-rpath,'$ORIGIN' -o app

Quick runtime checks and troubleshooting

  • Use ldd app to list dynamic dependencies (do not run on untrusted binaries; prefer readelf -d or objdump -p there).
  • Use nm -g, objdump -T or readelf -s to inspect exported symbols and diagnose unresolved references.
  • If a shared lib isn't found at runtime, set LD_LIBRARY_PATH temporarily or add the directory to the system cache with ldconfig, or use an embedded rpath (-Wl,-rpath).
  • On Windows toolchains: MSVC cl /LD foo.c builds a DLL and import LIB; MinGW can produce a DLL and an import lib with -Wl,--out-implib,libfoo.a.

Caveats and best practices

  • Build shared libraries with position-independent code (-fPIC) on platforms that require it.
  • Use SONAME/versioned filenames for ABI stability so upgrades don't break existing binaries.
  • For C++ APIs, avoid exposing STL types across the ABI; prefer extern "C" or a stable C wrapper.
  • Consider licensing (for example, LGPL) and update/security implications before statically linking system C runtimes.

These steps should make it straightforward to create, test and deploy libraries while avoiding the common pitfalls not covered earlier in the thread.

Recommended Answers

All 3 Replies

Every different program that uses a static library has its own copy of the library built in. A shared library only has one copy and every program that uses it just references that copy. How you create them depends on the tool you're using, and how they're named depends on the platform. For example, on Windows it might be *.lib versus *.dll. On Linux it might be *.a versus *.so.

And it gets a little more complicated because there are *nix compilers that have been ported to MS-Windows os, and consequently still use *.a library extensions. Dev-C++ is one such compiler. So for libraries the extension depends on the compiler.

Not that it matters too much. I think that .lib files are the same format as .a files these days.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.