2012-10-20 22 views
24

Tôi đang làm việc qua một số LLVM Tutorial, nhưng tôi đang gặp sự cố khi biên soạn. Tôi đã viết một ví dụ rất nhỏ mà tái tạo vấn đề:Tại sao tôi nhận được "tham chiếu không xác định đối với` dladdr '"ngay cả với -ldl cho chương trình đơn giản này?

#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; 
} 

Khi tôi cố gắng biên soạn, tôi nhận được một loạt các 'tài liệu tham khảo undefined' 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 

Nếu tôi xem manpage cho dladdr , nó nói rằng tôi cần phải liên kết với -ldl. Nhưng tôi đã làm điều đó với 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 

Bên cạnh đó, -ldl xuất hiện theo đúng thứ tự (ví dụ, sau khi tập tin .o mà đòi hỏi những biểu tượng).

Tôi đang thua lỗ trong bước tiếp theo để gỡ lỗi vấn đề này. ai đó có thể chỉ cho tôi phương hướng đúng không? Tôi đang chạy LVVM 2.9-7 trên Ubuntu 12.04.

Trả lời

31

Thư viện yêu cầu các biểu tượng được bao gồm bởi -lLLVMSupport, vì vậy -ldl phải đến sau -lLLVMSupport. Tôi đã thay đổi này:

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

Để này:

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

Và mối liên kết đã thành công.

+6

Hy vọng rằng, nó không kết thúc như http://xkcd.com/979/ Cảm ơn bạn. – deadalnix

11

Tôi đã gặp vấn đề tương tự khi thực hiện hướng dẫn LLVM cho llvm-3.4, và không may, câu trả lời từ Matthew không giúp ích gì. Để tham khảo trong tương lai, tôi đăng một câu trả lời mới với các vấn đề mà tôi đã gặp phải và cách tôi khắc phục nó.

Tôi đã cài đặt LLVM trong ~/dev/llvm/install, vì vậy tôi đã sử dụng các lệnh được đưa ra trong hướng dẫn này, nhưng thay llvm-config với đường dẫn để cài đặt llvm tôi:

clang++ -g -O3 toy.cpp `../llvm/install/bin/llvm-config --cppflags --ldflags --libs core` -o toy 

tôi nhận được một tấn lỗi, người đầu tiên là:

[email protected]:~/dev/kaleidoscope$ clang++ -g -O3 toy.cpp `../llvm/install/bin/llvm-config --cppflags --ldflags --libs core` -o toy 
In file included from toy.cpp:1: 
In file included from /home/filipe/dev/llvm/install/include/llvm/IR/Verifier.h:24: 
/home/filipe/dev/llvm/install/include/llvm/ADT/StringRef.h:342:14: error: no template named 'enable_if' in namespace 'std'; did you mean 
     '__gnu_cxx::__enable_if'? 
    typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type 
      ^~~~~~~~~~~~~~ 
      __gnu_cxx::__enable_if 
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ext/type_traits.h:43:12: note: '__gnu_cxx::__enable_if' declared here 
    struct __enable_if 
     ^

Điều này là do tại thời điểm này LLVM yêu cầu trình biên dịch C++ 11; thêm các tùy chọn -std=c++11 đến clang++ giải quyết được. Nhưng sau đó tôi nhận:

[email protected]:~/dev/kaleidoscope$ clang++ -std=c++11 -g -O3 toy.cpp `../llvm/install/bin/llvm-config --cppflags --ldflags --libs core` -o toy 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(Process.o): In function `terminalHasColors(int)': 
/home/filipe/dev/llvm/llvm/lib/Support/Unix/Process.inc:273: undefined reference to `setupterm' 
/home/filipe/dev/llvm/llvm/lib/Support/Unix/Process.inc:291: undefined reference to `tigetnum' 
/home/filipe/dev/llvm/llvm/lib/Support/Unix/Process.inc:295: undefined reference to `set_curterm' 
/home/filipe/dev/llvm/llvm/lib/Support/Unix/Process.inc:296: undefined reference to `del_curterm' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(Signals.o): In function `llvm::sys::PrintStackTrace(_IO_FILE*)': 
/home/filipe/dev/llvm/llvm/lib/Support/Unix/Signals.inc:278: undefined reference to `dladdr' 
/home/filipe/dev/llvm/llvm/lib/Support/Unix/Signals.inc:290: undefined reference to `dladdr' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(Mutex.o): In function `MutexImpl': 
/home/filipe/dev/llvm/llvm/lib/Support/Mutex.cpp:53: undefined reference to `pthread_mutexattr_init' 
/home/filipe/dev/llvm/llvm/lib/Support/Mutex.cpp:59: undefined reference to `pthread_mutexattr_settype' 
/home/filipe/dev/llvm/llvm/lib/Support/Mutex.cpp:67: undefined reference to `pthread_mutexattr_destroy' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(Mutex.o): In function `llvm::sys::MutexImpl::tryacquire()': 
/home/filipe/dev/llvm/llvm/lib/Support/Mutex.cpp:109: undefined reference to `pthread_mutex_trylock' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(RWMutex.o): In function `RWMutexImpl': 
/home/filipe/dev/llvm/llvm/lib/Support/RWMutex.cpp:59: undefined reference to `pthread_rwlock_init' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(RWMutex.o): In function `~RWMutexImpl': 
/home/filipe/dev/llvm/llvm/lib/Support/RWMutex.cpp:72: undefined reference to `pthread_rwlock_destroy' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::reader_acquire()': 
/home/filipe/dev/llvm/llvm/lib/Support/RWMutex.cpp:82: undefined reference to `pthread_rwlock_rdlock' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::reader_release()': 
/home/filipe/dev/llvm/llvm/lib/Support/RWMutex.cpp:92: undefined reference to `pthread_rwlock_unlock' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::writer_acquire()': 
/home/filipe/dev/llvm/llvm/lib/Support/RWMutex.cpp:102: undefined reference to `pthread_rwlock_wrlock' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(RWMutex.o): In function `llvm::sys::RWMutexImpl::writer_release()': 
/home/filipe/dev/llvm/llvm/lib/Support/RWMutex.cpp:112: undefined reference to `pthread_rwlock_unlock' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(ThreadLocal.o): In function `ThreadLocalImpl': 
/home/filipe/dev/llvm/llvm/lib/Support/ThreadLocal.cpp:56: undefined reference to `pthread_key_create' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(ThreadLocal.o): In function `~ThreadLocalImpl': 
/home/filipe/dev/llvm/llvm/lib/Support/ThreadLocal.cpp:63: undefined reference to `pthread_key_delete' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(ThreadLocal.o): In function `llvm::sys::ThreadLocalImpl::setInstance(void const*)': 
/home/filipe/dev/llvm/llvm/lib/Support/ThreadLocal.cpp:70: undefined reference to `pthread_setspecific' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(ThreadLocal.o): In function `llvm::sys::ThreadLocalImpl::getInstance()': 
/home/filipe/dev/llvm/llvm/lib/Support/ThreadLocal.cpp:77: undefined reference to `pthread_getspecific' 
/home/filipe/dev/llvm/install/lib/libLLVMSupport.a(Threading.o): In function `llvm::llvm_execute_on_thread(void (*)(void*), void*, unsigned int)': 
/home/filipe/dev/llvm/llvm/lib/Support/Threading.cpp:91: undefined reference to `pthread_attr_setstacksize' 
/home/filipe/dev/llvm/llvm/lib/Support/Threading.cpp:96: undefined reference to `pthread_create' 
/home/filipe/dev/llvm/llvm/lib/Support/Threading.cpp:100: undefined reference to `pthread_join' 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

Trái với những gì đã xảy ra với tác giả của câu hỏi ban đầu, tôi nhận thấy llvm-config đã thậm chí không liên kết chống lại bất kỳ thư viện hệ thống. Sau đó, tôi đã tìm rằng tôi cần phải sử dụng --system-libs để bao gồm các thư viện hệ thống mong muốn vào trình liên kết:

clang++ -std=c++11 -g -O3 toy.cpp `../llvm/install/bin/llvm-config --cppflags --libs core --ldflags --system-libs` -o toy 

Điều quan trọng là luôn đặt --system-libs cuối cùng, do đó tất cả phụ thuộc mất tích được đưa vào bởi các linker.

Được thử nghiệm với LLVM 3.4 và Kubuntu 14.04

+4

+1. với clang 3.5, '--system-libs' đã giúp tôi. –

+4

Tôi cũng cần --system-libs để biên dịch thành công với clang 3.5. Họ tạo ra một tập hợp các thư viện kiểu mô-đun tuyệt vời và sau đó nó không thể liên kết được với chúng. – korrok

Các vấn đề liên quan