Je travaille sur un Tutoriel LLVM , mais j'ai du mal à compiler. J'ai écrit un exemple minimal qui reproduit le problème:
#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;
}
Lorsque j'essaie de compiler, j'obtiens un tas d'erros de "référence non définie":
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
Si je consulte la page de manuel de dladdr, elle indique que je dois créer un lien avec -ldl
. Mais je fais déjà ça avec 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
Aditionellement, -ldl
apparaît dans le bon ordre (c'est-à-dire après le .o
fichier qui nécessite les symboles).
Je suis perdu pour la prochaine étape de débogage de ce problème. Est-ce que quelqu'un peut-il me montrer la bonne direction? J'utilise LVVM 2.9-7 sur Ubuntu 12.04.
La bibliothèque nécessitant les symboles est incluse par -lLLVMSupport
, alors -ldl
doit venir après -lLLVMSupport
. J'ai changé ça:
`llvm-config --ldflags --libs core`
Pour ça:
`llvm-config --libs core` `llvm-config --ldflags`
Et l'éditeur de liens a réussi.
J'avais le même problème lors de la lecture du didacticiel LLVM pour llvm-3.4, et malheureusement, la réponse de Matthew n'a pas aidé. Pour référence future, je poste une nouvelle réponse avec les problèmes que j'ai rencontrés et comment je les ai résolus.
J'ai installé LLVM dans ~/dev/llvm/install
, donc j'utilisais la commande donnée dans le tutoriel, mais j'ai remplacé llvm-config
avec le chemin de mon installation llvm:
clang++ -g -O3 toy.cpp `../llvm/install/bin/llvm-config --cppflags --ldflags --libs core` -o toy
J'ai eu une tonne d'erreurs, la première était:
filipe@filipe-Kubuntu:~/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
^
En effet, à ce stade, LLVM nécessite un compilateur C++ 11; ajouter -std=c++11
à clang++
les options l'ont résolu. Mais j'ai eu:
filipe@filipe-Kubuntu:~/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)
Contrairement à ce qui s'est passé avec l'auteur de la question d'origine, j'ai remarqué llvm-config
n'était même pas lié à une bibliothèque système. Ensuite, j'ai pensé que je devais utiliser --system-libs
afin d'inclure les bibliothèques système souhaitées dans le processus de liaison:
clang++ -std=c++11 -g -O3 toy.cpp `../llvm/install/bin/llvm-config --cppflags --libs core --ldflags --system-libs` -o toy
Il est important de toujours mettre --system-libs
à la fin, afin que toutes les dépendances manquantes soient apportées par l'éditeur de liens.
Testé avec LLVM 3.4 et Kubuntu 14.04