ccurlcompiler-errorslibcurlnss

undefined reference to symbol 'CERT_GetDefaultCertDB@@NSS_3.2'


I recently added libcurl dependency to my c++ library. I statically compiled libcurl with-nss for https support. I use Debian 7 for compilation.

I create two builds for my library - static and shared

Shared version is linking fine with binaries built on any Linux distro, but the static build only links with binaries when compiled on Debian 7.

I tried static linking on Ubuntu 16.04, Debian Stretch but all are reporting following error during compilation:

g++ -Wall -o Sample Sample.cpp -Wl,-Bstatic -L. -lMyLibrary -Wl,-Bdynamic  -lssl3
/usr/bin/ld: ./libMyLibrary.a(libcurl_la-nss.o): undefined reference to symbol 'CERT_GetDefaultCertDB@@NSS_3.2'
//usr/lib/x86_64-linux-gnu/libnss3.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Makefile:22: recipe for target 'Sample' failed
make: *** [Sample] Error 1

Static compilation now only works on Debian 7 which is a big problem.


Solution

  • Static libraries are just archives of object files. As one consequences, they don't carry any dependency information. So, if you link a static library which depends on some other libraries, you have to add these explicitly in your linking command.

    In your case, this means: