I am trying to compile with address sanitizer.
gcc12 -fsanitize=address -fsanitize=leak -static-libasan my_copy.c -o asan -lpthread
Getting this error:
/usr/local/bin/ld: my_copy.o: in function `pthread_mutex_lock':
my_copy.c:(.text+0x22): multiple definition of `pthread_mutex_lock'; /usr/local/lib/gcc12/gcc/x86_64-portbld-freebsd11.4/12.0.0/../../../libasan.a(asan_interceptors.o):(.text+0x3a70): first defined here
/usr/local/bin/ld: my_copy.o: in function `pthread_mutex_unlock':
my_copy.c:(.text+0x31): multiple definition of `pthread_mutex_unlock'; /usr/local/lib/gcc12/gcc/x86_64-portbld-freebsd11.4/12.0.0/../../../libasan.a(asan_interceptors.o):(.text+0x3ab0): first defined here
collect2: error: ld returned 1 exit status
But when I remove -static-libasan
, it compiles fine.
I want to link libasan
as static.
my_copy.c is just a snippent used to recreate this issue. And this is part of legacy library.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) {
(void)mutex;
(void)attr;
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex) {
(void)mutex;
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *mutex) {
(void)mutex;
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
(void)mutex;
return 0;
}
int main()
{
return EXIT_SUCCESS;
}
I understand why the error is coming, but don't know how get around it.