'undefined reference to '_asan_init_v4' when compiling

When linking my code compiled with the AddressSanitizer tool, I'm getting many errors of the kind undefined reference to '_asan_init_v4'.

clang -fPIC -g -fno-omit-frame-pointer -DNDEBUG -Wl,-z,defs \
  -shared -Wl,-soname,libqpid-dispatch.so -o libqpid-dispatch.so \
  CMakeFiles/qpid-dispatch.dir/alloc_pool.c.o \
  CMakeFiles/qpid-dispatch.dir/amqp.c.o \
  [...]
  -lpthread -lrt -ldl -lpython3.7m -lwebsockets -fsanitize=address

Some examples of the errors

/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: CMakeFiles/qpid-dispatch.dir/http-libwebsockets.c.o: in function `qd_http_server_free':
/home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:824: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__asan_report_load8'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__asan_report_load8'

Note: I am using NixOS 19.09 and Clang 10.



Solution 1:[1]

You should be using the compile flag -fsanitize=address: https://github.com/google/sanitizers/wiki/AddressSanitizer

Note that -fsanitize=address = -lasan + some add'l options. And using -lasan has been discouraged by ASan developers.

Solution 2:[2]

I'm not sure that this was the problem, but for me, adding -lasan to the linker options was enough to get my program to build and run.

Solution 3:[3]

(I see @user7610 basically said this in their comment)

You should not use -z defs, since it is incompatible with asan.

Source: https://clang.llvm.org/docs/AddressSanitizer.html

"When linking shared libraries, the AddressSanitizer run-time is not linked, so -Wl,-z,defs may cause link errors (don’t use it with AddressSanitizer)."

Solution 4:[4]

Adding -static-libasan to linker flags will solves this problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BoltzmannBrain
Solution 2
Solution 3
Solution 4 rashok