std::async([](){ std::cout<< "Hello " ; }) build error
std::async([](){ std::cout<< "Hello "; }) build error
CppCon 2015: Detlef Vollmann "Executors for C++ - A Long Story ..."从这个例子开始:
std::async([](){ std::cout << "Hello "; });
std::async([](){ std::cout << "World!n"; });
C++参考资料显示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)
您需要使用
-pthread
进行编译,以便链接器允许您使用异步/未来/线程功能。
对于许多库,您必须在链接时包含对库的引用。对于<future>
,我相信这是--pthread
。
所以试试,clang++ --std=c++14 cppcon15.cpp --pthread
.
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- std::vector::reserve(未知m),我知道m<<;N(通常)并且知道N
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- C++运算符<<调用::ostream而不是std::osttream
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- C++重载<<具有typedef'd std::vector
- 以x的倍数填充前导零,使用std::cout<<std::十六进制
- 错误:没有匹配'运算符<<"在'std::cout
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- std::ostream&operator< & lt; (std:: ostream&压力,压力& &;val)
- 将std::endl传递给std::operator<<
- std::映射<>或std::vector<>在处理大型标志集时
- 重载& lt; & lt;使用命名空间std