为什么我得到“未定义的引用`dladdr'”即使用这个简单的程序的-ldl? [英] Why am I getting "undefined reference to `dladdr'" even with -ldl for this simple program?

查看:4512
本文介绍了为什么我得到“未定义的引用`dladdr'”即使用这个简单的程序的-ldl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成 LLVM教程,但我编译时遇到问题。我写了一个最小的例子,再现的问题:

  #includellvm / Module.h
# includellvm / LLVMContext.h

int main(int argc,char ** argv){
llvm :: Module * module = new llvm :: Module(test,llvm :: getGlobalContext());
return 0;
}



当我尝试编译时,我得到一堆'未定义的引用' :

  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):在函数`PrintStackTrace(void *)' :
(.text + 0x6c):未定义引用`dladdr'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(Signals.o):在函数`PrintStackTrace(void *) ':
(.text + 0x1f6):未定义的引用`dladdr'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o):在函数`llvm :: sys :: MutexImpl :: MutexImpl(bool)':
(.text + 0x53):未定义的引用`pthread_mutexattr_init'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o ):在函数`llvm :: sys :: MutexImpl :: MutexImpl(bool)':
(.text + 0x64):未定义引用`pthread_mutexattr_settype'
/usr/lib/llvm-2.9/ lib / libLLVMSupport.a(Mutex.o):在函数`llvm :: sys :: MutexImpl :: MutexImpl(bool)':
(.text + 0x74):未定义引用`pthread_mutexattr_setpshared'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o):在函数`llvm :: sys :: MutexImpl :: MutexImpl(bool)'中:
(.text + 0x88):undefined参考`pthread_mutexattr_destroy'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(Mutex.o):在函数`llvm :: sys :: MutexImpl :: tryacquire()':
(.text + 0x179):未定义的引用`pthread_mutex_trylock'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o):在函数`llvm :: sys :: RWMutexImpl :: RWMutexImpl ()':
(.text + 0x3e):未定义引用`pthread_rwlock_init'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o):在函数`llvm: :sys :: RWMutexImpl ::〜RWMutexImpl()':
(.text + 0x80):未定义引用`pthread_rwlock_destroy'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex .o):在函数`llvm :: sys :: RWMutexImpl :: reader_acquire()'中:
(.text + 0xb9):未定义引用`pthread_rwlock_rdlock'
/usr/lib/llvm-2.9 /lib/libLLVMSupport.a(RWMutex.o):在函数`llvm :: sys :: RWMutexImpl :: reader_release()':
(.text + 0xe9):未定义引用`pthread_rwlock_unlock'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o):在函数`llvm :: sys :: RWMutexImpl :: writer_acquire()':
(.text + 0x119):undefined reference到`pthread_rwlock_wrlock'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(RWMutex.o):在函数`llvm :: sys :: RWMutexImpl :: writer_release()':
$ text / $ lib / lib / lib / llvm-2.9 / lib / lib / lib / libs / lib / lib / lib / lib *),void *,unsigned int)':
(.text + 0x1cc):未定义引用`pthread_create'
/usr/lib/llvm-2.9/lib/libLLVMSupport.a(Threading.o ):在函数`llvm :: llvm_execute_on_thread(void(*)(void *),void *,unsigned int)':
(.text + 0x208):未定义引用`pthread_attr_setstacksize'
/ usr /lib/llvm-2.9/lib/libLLVMSupport.a(Threading.o):在函数`llvm :: llvm_execute_on_thread(void(*)(void *),void *,unsigned int)':
+ 0x228):未定义引用`pthread_join'
clang:error:linker命令失败,退出代码1(使用-v查看调用)
make:*** [test]错误1

如果我查看dladdr的联机帮助页,它说我需要链接到 -ldl 。但我已经这样做与 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

此外, -ldl 以正确的顺序出现(即在需要符号的 .o 文件之后)。



在调试这个问题的下一步的损失。任何人都可以指向正确的方向?我在Ubuntu 12.04上运行LVVM 2.9-7。

解决方案

需要符号的库包含在 -lLLVMSupport ,因此 -ldl 必须在 -lLLVMSupport 之后。我改变了这一点:

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

至此:

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

链接器已成功。 p>

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.

解决方案

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.

这篇关于为什么我得到“未定义的引用`dladdr'”即使用这个简单的程序的-ldl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆