Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "undefined reference to `dladdr'" even with -ldl for this simple program?

I'm working through an LLVM Tutorial, but I'm having trouble compiling. I've written a minimal example that reproduces the issue:

#include "llvm/Module.h" #include "llvm/LLVMContext.h"  int main(int argc, char **argv) {     llvm::Module *module = new llvm::Module("test", llvm::getGlobalContext());     return 0; } 

When I try to compile, I get a bunch of 'undefined reference' erros:

clang++ `llvm-config --cxxflags`   -c -o test.o test.cpp clang++  test.o  `llvm-config --ldflags --libs core` -o test /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Signals.o): In function `PrintStackTrace(void*)': (.text+0x6c): undefined reference to `dladdr' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Signals.o): In function `PrintStackTrace(void*)': (.text+0x1f6): undefined reference to `dladdr' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o): In function `llvm::sys::MutexImpl::MutexImpl(bool)': (.text+0x53): undefined reference to `pthread_mutexattr_init' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o): In function `llvm::sys::MutexImpl::MutexImpl(bool)': (.text+0x64): undefined reference to `pthread_mutexattr_settype' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o): In function `llvm::sys::MutexImpl::MutexImpl(bool)': (.text+0x74): undefined reference to `pthread_mutexattr_setpshared' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o): In function `llvm::sys::MutexImpl::MutexImpl(bool)': (.text+0x88): undefined reference to `pthread_mutexattr_destroy' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o): In function `llvm::sys::MutexImpl::tryacquire()': (.text+0x179): undefined reference to `pthread_mutex_trylock' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::RWMutexImpl()': (.text+0x3e): undefined reference to `pthread_rwlock_init' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::~RWMutexImpl()': (.text+0x80): undefined reference to `pthread_rwlock_destroy' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::reader_acquire()': (.text+0xb9): undefined reference to `pthread_rwlock_rdlock' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::reader_release()': (.text+0xe9): undefined reference to `pthread_rwlock_unlock' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::writer_acquire()': (.text+0x119): undefined reference to `pthread_rwlock_wrlock' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::writer_release()': (.text+0x149): undefined reference to `pthread_rwlock_unlock' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Threading.o): In function `llvm::llvm_execute_on_thread(void (*)(void*), void*, unsigned int)': (.text+0x1cc): undefined reference to `pthread_create' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Threading.o): In function `llvm::llvm_execute_on_thread(void (*)(void*), void*, unsigned int)': (.text+0x208): undefined reference to `pthread_attr_setstacksize' /usr/lib/llvm-2.9/lib/libLLVMSupport.a(Threading.o): In function `llvm::llvm_execute_on_thread(void (*)(void*), void*, unsigned int)': (.text+0x228): undefined reference to `pthread_join' clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [test] Error 1 

If I view the manpage for dladdr, it says that I need to link with -ldl. But I'm already doing that with llvm-config:

$ llvm-config --ldflags --libs core -L/usr/lib/llvm-2.9/lib  -lpthread -lffi -ldl -lm  -lLLVMCore -lLLVMSupport -L/usr/lib/llvm-2.9/lib 

Additionally, -ldl appears in the correct order (i.e., after the .o file that requires the symbols).

I'm at a loss for the next step in debugging this issue. Can anyone point me in the right direction? I'm running LVVM 2.9-7 on Ubuntu 12.04.

like image 667
Matthew Avatar asked Oct 20 '12 23:10

Matthew


People also ask

How do you fix a undefined reference?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do you fix undefined references in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

What does undefined reference to main mean in C++?

Undefined reference to main() means that your program lacks a main() function, which is mandatory for all C++ programs.


1 Answers

The library requiring the symbols is included by -lLLVMSupport, so -ldl must come after -lLLVMSupport. I changed this:

`llvm-config --ldflags --libs core` 

To this:

`llvm-config --libs core` `llvm-config --ldflags` 

And the linker was successful.

like image 54
Matthew Avatar answered Oct 11 '22 08:10

Matthew