undefined reference to symbol 'CERT_GetDefaultCertDB##NSS_3.2' - c

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.

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:
find whoever defines CERT_GetDefaultCertDB##NSS_3.2 -- some answers to this FAQ could help here
add this library to your linker command (with -l, after your static library)

Related

How to fix "cannot find -lz"

I am working on code have Zlib.h header, This header is found in my code folder, I compile this code by using
gcc -o x xx.c -lz
but I get on this
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
This happen just with Linux that I installed in a VBox.
How to fix that.
Try installing 'zlib1g-dev'. On Ubuntu this following command will install the library.
sudo apt install zlib1g-dev
When you type gcc foo.c, you ask gcc to compile and link the given file.
1. Compilation
Compilation consist of transforming the source file into an object file.
This step need the included files, like zlib.h to be found by gcc.
This step seems to be correct on system.
NB: You can ask gcc to only do this step typing gcc -c foo.c, or better gcc -Wall -c foo.c
2. Link
Once the object files have be created, then need to be linked to create an executable file.
It's that step that failed for you: your linked can't find the already compiled functions needed by your code.
When linking with option -lz, you tell your linker "search for libz.so file to find the missing functions"
On current linux distribution, you can install package like libz-dev to install the .so file in well known places. (/lib, /usr/lib, /usr/local/lib...)
If you don't have the libz.so file installed on the library search path, you can specify where is the library to your linker.
For instance, if libz.so is if /bar/baz directory, you can type gcc foo.c /bar/baz/libz.so. The same for libz.a.
In any case, you'll need the libz.so file or at least the libz.a file
See also What's the difference between .so, .la and .a library files?

Not able to link gcc against libbluetooth library

I am using BlueZ library for developing bluetooth based application for linux using C. I am trying to link libbluetooth-dev to my C file but it is not working.
/usr/bin/ld: cannot find -libbluetooth-dev
collect2: error: ld returned 1 exit status
My project directory is bluez-5.45, i am trying to build the project from this folder and added my .C file in this main folder
I installed libbluetooth-dev library and tryed that linking but not able to do that.
Command for linking:
gcc -o output myfile.c -libbluetooth
please let me know the mistake done by me.
The "lib" is implied, so link against "bluetooth" instead of "libbluetooth":
$ gcc -o output myfile.c -lbluetooth

gcc: Not able to create .so from object files

I am trying to create .so dynamic library from *.o files and facing below issue.
LOG:
[nptemp-static]$ gcc -shared *.o -o libexample.so
/usr/bin/ld: bindings_hubbub_parser.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
bindings_hubbub_parser.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
Any idea? Do I need to recompile my whole source code with the option specified?
Actually, I am not aware of the source code which I compiled because all the source code is open source which I downloaded and compiled by following instructions in README.
I am trying to create .so dynamic library from *.o files and facing below issue.
This is not that simple. In practice, you should compile specifically when making a shared library, at least on Linux.
(Perhaps you might need to edit your Makefile or configure somehow your build automation if it was not designed for building a shared library; if building some free software library, you might ask help from its authors or community)
Shared libraries want to have position independent code. So you need to compile their source code with the -fPIC flag passed to g++ or gcc (see this). You could also want to explicit the rpath.
Read Drepper's paper: How To Write Shared Libraries.

Link static libs to dynamic libs: -mimpure-text in G++ / Ubuntu

I'm trying to link some 3rd party static libraries into a dynamic library. However I get the error -
relocation R_X86_64_32 against `TMC_END' can not be used when
making a shared object; recompile with -fPIC
On Solaris, you can tell GCC/ G++ to allow linking with code that wasn't compiled with -fPIC, through the option -mimpure-text. (Found via great answer here) However this option isn't recognized on Ubuntu (16.04, g++ version 5.4.0):
g++: error: unrecognized command line option ‘-mimpure-text’
Is there any equivalent option, or any other solution except for compiling the 3rd parties static libraries with -fPIC enabled?
Thanks!
P.S Excellent explanation on relocations here

Generate LLVM IR for httpd

I am trying to compile apache2 with LLVM, to generate final binaries in LLVM IR.
I used flto option in compiling and linking and passed "also-emit-llvm" to the linker through clang.
It seems to work for most steps, however I had two concerns
1) Earlier I used LLVM 3.6, whose gold plugin doesn't accept also-emit-llvm, but has emit-llvm only, basically it will emit only elf or llvm. Which the Autotools build system doesn't like. The configure and make script want binaries, while I want llvm. Any solutions to this?
2) So I shifted to LLVM 3.5.2, the build process goes well, I can generate both httpd.bc and httpd elf binaries, but the linker doesn't want to link for modules (it was able to link for binaries)
Specifically, I get this error
`
/usr/share/apr-1.0/build/libtool --silent --mode=link /home/rbhatia/Desktop/llvm-newbuild/bin/clang -pthread -flto -o mod_authn_file.la -rpath /home/rbhatia/Desktop/httpd-2.4.12/llvm/modules -module -avoid-version mod_authn_file.lo
/usr/bin/ld: error: .libs/mod_authn_file.o:1:3: invalid chaenter code hereracter
/usr/bin/ld: error: .libs/mod_authn_file.o:1:3: syntax error, unexpected $end
/usr/bin/ld: error: .libs/mod_authn_file.o: not an object or archive
collect2: error: ld returned 1 exit status
`
I can see that mod_authn_file.o is a valid LLVM IR file which I can disassemble with llvm-dis.
Also, just before this step, the linker is able to link httpd and httpd.bc
Any help on what the error is?
Take a look at
https://github.com/SRI-CSL/whole-program-llvm
we use this tool to build quite large projects into bitcode. Our
biggest so far was FreeBSD 10.0, so size is not usually an issue.
Our travis build check is actually
apache, as is our tutorial. Hope that helps.

Resources