带有Clang的C ++ 11线程 [英] C++ 11 threads with clang

查看:249
本文介绍了带有Clang的C ++ 11线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习使用C ++ 11线程来加快语言的编译速度(是的,我正在构建编译器:x).我尝试的第一个示例使用clang(3.3 SVN)引发了多个错误.在GCC(4.6.3)下编译良好.

I wanted to learn use of C++11 threads to speed up compilation of my language (yes I'm building a compiler :x). The first sample I tried threw several errors with clang (3.3 SVN). It compiled fine under GCC (4.6.3).

我从llvm.org的SVN下载了clang和libc ++. clang用GCC(4.6.3)编译,而libc ++用clang编译.这两个makefile都是使用CMake生成的.

I downloaded clang and libc++ from llvm.org's SVN. clang was compiled with GCC (4.6.3) and libc++ was compiled with clang. Both makefiles were generated with CMake.

对于c,我遵循此指南: http://llvm.org/docs/GettingStarted.html #checkout

For clang I followed this guide: http://llvm.org/docs/GettingStarted.html#checkout

对于libc ++,我遵循了该指南: http://libcxx.llvm.org/

For libc++ I followed this guide: http://libcxx.llvm.org/

我要编译的代码段(foobar.cpp):

The piece of code I want to compile (foobar.cpp):

#include <iostream>
#include <thread>

using namespace std;

int main(void) {
    thread t1([](void)->void {
        cout << "foobar" << endl;
    });
}

使用clang --std=c++0x -stdlib=libc++ -lpthread foobar.cpp可以正常编译,但是出现很多链接器错误:

It compiles fine with clang --std=c++0x -stdlib=libc++ -lpthread foobar.cpp but I get a lot of linker errors:

/tmp/foobar-59W5DR.o: In function `main':
foobar.cpp:(.text+0x21): undefined reference to `std::__1::thread::~thread()'
/tmp/foobar-59W5DR.o: In function `_ZNSt3__16threadC2IZ4mainE3$_0JEvEEOT_DpOT0_':
foobar.cpp:(.text+0x5c): undefined reference to `operator new(unsigned long)'
foobar.cpp:(.text+0x186): undefined reference to `operator delete(void*)'
foobar.cpp:(.text+0x19b): undefined reference to `std::__1::__throw_system_error(int, char const*)'
/tmp/foobar-59W5DR.o: In function `_ZNSt3__114__thread_proxyINS_5tupleIJZ4mainE3$_0EEEEEPvS4_':
foobar.cpp:(.text+0x200): undefined reference to `std::__1::__thread_local_data()'
foobar.cpp:(.text+0x211): undefined reference to `operator new(unsigned long)'
foobar.cpp:(.text+0x23b): undefined reference to `std::__1::__thread_struct::__thread_struct()'
foobar.cpp:(.text+0x31b): undefined reference to `operator delete(void*)'
/tmp/foobar-59W5DR.o: In function `_ZNSt3__110unique_ptrINS_5tupleIJZ4mainE3$_0EEENS_14default_deleteIS3_EEED2Ev':
foobar.cpp:(.text+0x3dd): undefined reference to `operator delete(void*)'
/tmp/foobar-59W5DR.o: In function `main::$_0::operator()() const':
foobar.cpp:(.text+0x3fc): undefined reference to `std::__1::cout'
/tmp/foobar-59W5DR.o: In function `_ZNSt3__110unique_ptrINS_5tupleIJZ4mainE3$_0EEENS_14default_deleteIS3_EEEC2EPS3_':
foobar.cpp:(.text+0x4e9): undefined reference to `std::terminate()'
/tmp/foobar-59W5DR.o: In function `std::__1::__thread_specific_ptr<std::__1::__thread_struct>::reset(std::__1::__thread_struct*)':
foobar.cpp:(.text._ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_[_ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_]+0x57): undefined reference to `std::__1::__thread_struct::~__thread_struct()'
foobar.cpp:(.text._ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_[_ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_]+0x60): undefined reference to `operator delete(void*)'
/tmp/foobar-59W5DR.o: In function `std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*)':
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x28): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x1ec): undefined reference to `std::__1::ios_base::getloc() const'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x1fe): undefined reference to `std::__1::ctype<char>::id'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x206): undefined reference to `std::__1::locale::use_facet(std::__1::locale::id&) const'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x272): undefined reference to `std::__1::locale::~locale()'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x294): undefined reference to `std::__1::locale::~locale()'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x378): undefined reference to `std::__1::ios_base::clear(unsigned int)'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x3d0): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x3dc): undefined reference to `__cxa_begin_catch'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x3f9): undefined reference to `std::__1::ios_base::__set_badbit_and_consider_rethrow()'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x403): undefined reference to `__cxa_end_catch'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x424): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x43d): undefined reference to `__cxa_end_catch'
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x466): undefined reference to `std::terminate()'
/tmp/foobar-59W5DR.o: In function `std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)':
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0x38): undefined reference to `std::__1::ios_base::getloc() const'
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0x49): undefined reference to `std::__1::ctype<char>::id'
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0x55): undefined reference to `std::__1::locale::use_facet(std::__1::locale::id&) const'
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xac): undefined reference to `std::__1::locale::~locale()'
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xbe): undefined reference to `std::__1::locale::~locale()'
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xcd): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)'
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xdd): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()'
/tmp/foobar-59W5DR.o: In function `std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char)':
foobar.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x215): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)'
foobar.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x389): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()'
foobar.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x3a4): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()'
/tmp/foobar-59W5DR.o:(.eh_frame+0x47): undefined reference to `__gxx_personality_v0'
clang-3: error: linker command failed with exit code 1 (use -v to see invocation)

这是两个标准库下的标准搜索目录. 由于'clang -Wp,-v -x c ++--fsyntax-only',我得到了这个:

Here are the standard search directories under the two standard libraries. As result of 'clang -Wp,-v -x c++ - -fsyntax-only' I got this:

我尝试了clang --std=c++11 -stdlib=libc++ -llibc++ -lpthread -o foobar foobar.cpp,但是链接器再次失败,它找不到libc ++.我是否无法链接到libc ++还是更困难?

I tried clang --std=c++11 -stdlib=libc++ -llibc++ -lpthread -o foobar foobar.cpp but the linker failed again, it does not find libc++. Do I fail on linking to libc++ or is it something more difficult?

推荐答案

您正在使用Clang而不是C ++进行clang编译.

You are compiling with clang for C not for C++.

  • 对于C语言,请使用 clang
  • 对于C ++,请使用 clang ++
  • For C use clang
  • For C++ use clang++

clang不会警告您的原因是因为您明确定义了标准.

这篇关于带有Clang的C ++ 11线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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