2016-01-15 59 views
2

CppCon 2015: Detlef Vollmann “Executors for C++ - A Long Story ..."开始了与该实施例中:的std ::异步([](){的std :: COUT << “你好”;})建立错误

std::async([](){ std::cout << "Hello "; }); 
std::async([](){ std::cout << "World!\n"; }); 

C++ reference显示std::async<future>std::cout是在<iostream>。构建工作缺少什么?

$ cat >hw.cpp <<EOF 
> #include <iostream> 
> int main(){ 
>  std::cout << "Hello World!\n"; 
> } 
> EOF 
$ clang++ -std=c++14 hw.cpp 
$ ./a.out 
Hello World! 
$ cat >cppcon15.cpp <<EOF 
> #include <future> 
> #include <iostream> 
> int main(){ 
>  std::async([](){ std::cout << "Hello "; }); 
>  std::async([](){ std::cout << "World!\n"; }); 
> } 
> EOF 
$ clang++ -std=c++14 cppcon15.cpp 
/tmp/cppcon15-4f0a58.o: In function `std::thread::thread<std::__future_base::_Async_state_impl<std::_Bind_simple<main::$_1()>, void>::_Async_state_impl(std::_Bind_simple<main::$_1()>&&)::{lambda()#1}>(std::__future_base::_Async_state_impl<std::_Bind_simple<main::$_1()>, void>::_Async_state_impl(std::_Bind_simple<main::$_1()>&&)::{lambda()#1}&&)': 
cppcon15.cpp:(.text+0x2cf6): undefined reference to `pthread_create' 
/tmp/cppcon15-4f0a58.o: In function `std::thread::thread<std::__future_base::_Async_state_impl<std::_Bind_simple<main::$_0()>, void>::_Async_state_impl(std::_Bind_simple<main::$_0()>&&)::{lambda()#1}>(std::__future_base::_Async_state_impl<std::_Bind_simple<main::$_0()>, void>::_Async_state_impl(std::_Bind_simple<main::$_0()>&&)::{lambda()#1}&&)': 
cppcon15.cpp:(.text+0x6bb6): undefined reference to `pthread_create' 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
+0

的可能的复制[什么是未定义参考/解析的外部符号错误,以及如何解决呢?(http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved- external-symbol-error-and-how-do-i-fix) –

回答

9

您需要-pthread编译,以便链接可以让你利用异步/未来/线程功能。

+0

你想试试-lpthread吗? – Joshua

+0

@Joshua在-lpthread'以有利于-pthread'的''弃用GCC/clang''。 – Galik

+0

@pacevedo,在那里会是这样记载,W/O型人能找到它第一次知道答案,从未定义的参考'在pthread_create“? –

0

对于许多图书馆,你必须包括在库的引用而连接。 为<future>,我相信这是--pthread

因此,尝试,clang++ --std=c++14 cppcon15.cpp --pthread

相关问题