Решено: Clang - куча странных ошибок компиляции

Модератор: Модераторы разделов

frp
Сообщения: 1445
ОС: Debian Squeeze

Решено: Clang - куча странных ошибок компиляции

Сообщение frp »

Захотел попробовать Clang, Поставил из репозитария Debian Squeeze. Попробовал скомпилировать Hello World. Вот код:

Код: Выделить всё

#include <iostream>
using namespace std;

int main()
{
    cout<<"Hello, World!"<<endl;
    return 0;
}

А вот ошибки:

Код:

In file included from hello.cpp:1: In file included from /usr/include/c++/4.1.3/iostream:43: In file included from /usr/include/c++/4.1.3/ostream:43: In file included from /usr/include/c++/4.1.3/ios:42: In file included from /usr/include/c++/4.1.3/iosfwd:45: In file included from /usr/include/c++/4.1.3/i486-linux-gnu/bits/c++io.h:38: In file included from /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr.h:114: /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:92:1: error: weakref declaration of '__gthrw_pthread_once' must be static __gthrw(pthread_once) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:93:1: error: weakref declaration of '__gthrw_pthread_getspecific' must be static __gthrw(pthread_getspecific) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:94:1: error: weakref declaration of '__gthrw_pthread_setspecific' must be static __gthrw(pthread_setspecific) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:95:1: error: weakref declaration of '__gthrw_pthread_create' must be static __gthrw(pthread_create) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:96:1: error: weakref declaration of '__gthrw_pthread_cancel' must be static __gthrw(pthread_cancel) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:97:1: error: weakref declaration of '__gthrw_pthread_mutex_lock' must be static __gthrw(pthread_mutex_lock) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:98:1: error: weakref declaration of '__gthrw_pthread_mutex_trylock' must be static __gthrw(pthread_mutex_trylock) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:99:1: error: weakref declaration of '__gthrw_pthread_mutex_unlock' must be static __gthrw(pthread_mutex_unlock) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:100:1: error: weakref declaration of '__gthrw_pthread_mutex_init' must be static __gthrw(pthread_mutex_init) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:103:1: error: weakref declaration of '__gthrw_pthread_key_create' must be static __gthrw(pthread_key_create) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:104:1: error: weakref declaration of '__gthrw_pthread_key_delete' must be static __gthrw(pthread_key_delete) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:105:1: error: weakref declaration of '__gthrw_pthread_mutexattr_init' must be static __gthrw(pthread_mutexattr_init) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:106:1: error: weakref declaration of '__gthrw_pthread_mutexattr_settype' must be static __gthrw(pthread_mutexattr_settype) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:107:1: error: weakref declaration of '__gthrw_pthread_mutexattr_destroy' must be static __gthrw(pthread_mutexattr_destroy) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:75:23: note: instantiated from: #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name) ^ /usr/include/c++/4.1.3/i486-linux-gnu/bits/gthr-default.h:66:46: note: instantiated from: extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \ ^ 14 diagnostics generated.

Что значат эти ошибк? почему они возникают? как с ними бороться?
Спасибо сказали:
Аватара пользователя
DaemonTux
Сообщения: 1480
Статус: Юный падаван
ОС: Gentoo

Re: Решено: Clang - куча странных ошибок компиляции

Сообщение DaemonTux »

Как собирали?
По всей видимости clang берет хедеры от gcc.
Vladivostok Linux User Group
Спасибо сказали:
frp
Сообщения: 1445
ОС: Debian Squeeze

Re: Решено: Clang - куча странных ошибок компиляции

Сообщение frp »

DaemonTux писал(а):
02.09.2010 07:27
Как собирали?

Clang - aptitude install clang :)
Hello, World - clang hello.cpp

UPD
С обычным C проблем нет. С C++, который не юзает стандартные хидеры C++, тоже проблем нет.

UPD 2
Если программа на C++ юзает C++-версии стандартных хидеров (cstdlib вместо stdlib.h), то проблем тоже нет.
Спасибо сказали:
frp
Сообщения: 1445
ОС: Debian Squeeze

Re: Решено: Clang - куча странных ошибок компиляции

Сообщение frp »

UPD 3
В гугле нашел workaround - собирать так: clang hello.cpp -D_GLIBCXX__PTHREADS
Вывод:

Код:

/tmp/cc-b6HFB0.o: In function `__cxx_global_var_init': hello.cpp:(.text+0x13): undefined reference to `std::ios_base::Init::Init()' hello.cpp:(.text+0x19): undefined reference to `std::ios_base::Init::~Init()' /tmp/cc-b6HFB0.o: In function `main': hello.cpp:(.text+0x4f): undefined reference to `std::cout' hello.cpp:(.text+0x61): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' hello.cpp:(.text+0x67): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' hello.cpp:(.text+0x73): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))' /tmp/cc-b6HFB0.o: In function `virtual thunk to std::basic_ostream<char, std::char_traits<char> >::~basic_ostream()': hello.cpp:(.gnu.linkonce.t._ZTv0_n12_NSoD1Ev+0x1c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::~basic_ostream()' /tmp/cc-b6HFB0.o: In function `virtual thunk to std::basic_ios<char, std::char_traits<char> >::~basic_ios()': hello.cpp:(.gnu.linkonce.t._ZTv0_n12_NSt9basic_iosIcSt11char_traitsIcEED0Ev+0x1c ): undefined reference to `std::basic_ios<char, std::char_traits<char> >::~basic_ios()' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTISt9basic_iosIcSt11char_traitsIcEE+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTISt9basic_iosIcSt11char_traitsIcEE+0x8): undefined reference to `typeinfo for std::ios_base' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTISo+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTVSt13basic_ostreamIcSt11char_traitsIcEE+0xc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::~basic_ostream()' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTVSt13basic_ostreamIcSt11char_traitsIcEE+0x10): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::~basic_ostream()' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTVSt13basic_ostreamIcSt11char_traitsIcEE+0x24): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::~basic_ostream()' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTVSt9basic_iosIcSt11char_traitsIcEE+0x8): undefined reference to `std::basic_ios<char, std::char_traits<char> >::~basic_ios()' /tmp/cc-b6HFB0.o:(.gnu.linkonce.r._ZTVSt9basic_iosIcSt11char_traitsIcEE+0xc): undefined reference to `std::basic_ios<char, std::char_traits<char> >::~basic_ios()' collect2: ld returned 1 exit status clang: error: linker command failed with exit code 1 (use -v to see invocation)

Собралось только после явного указания линковать с libstdc++: clang hello.cpp -D_GLIBCXX__PTHREADS -lstdc++

Что это за ерунда?
Спасибо сказали:
frp
Сообщения: 1445
ОС: Debian Squeeze

Re: Решено: Clang - куча странных ошибок компиляции

Сообщение frp »

Как оказалось, в clang должен быть симлинк clang++ (его отсутствие в squeeze-версии это баг debian). Если создать такой симлинк, то -lstdc++ указывать не нужно.
Спасибо сказали: