1 背景

1.1 场景

编译器gcc 9.4
运行系统Ubuntu 20.04.4 LTS
xmake: v2.6.7
场景:其大致场景是使用c++的future/promise功能,类似如下示意代码:

#include <iostream>
#include <future>using namespace std;int main(int argc, char** argv)
{std::promise<int> p;p.set_value(42);auto f = p.get_future();int res = f.get();std::cout << "future got value: " << res << std::endl;return 0;
}

默认xmake文件如下:

add_rules("mode.debug", "mode.release")target("explicit_stdc++")set_kind("binary")add_files("src/*.cpp")

编译运行以及执行结果如下:

gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake
checking for platform ... linux
checking for architecture ... x86_64
[ 25%]: ccache compiling.release src/main.cpp
[ 50%]: linking.release explicit_stdc++
[100%]: build ok!
gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake run
terminate called after throwing an instance of 'std::system_error'what():  Unknown error -1

编译出来的执行文件的链接库如下所示:

$ ldd build/linux/x86_64/release/explicit_stdc++ linux-vdso.so.1 (0x00007ffe29fbd000)libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f26b8abf000)libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f26b8aa4000)  libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f26b88b2000)libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f26b8763000)/lib64/ld-linux-x86-64.so.2 (0x00007f26b8cb4000)

网上关于这部分的信息几乎是没有,经过一番查找,然后才开始了如下尝试,并最终找到对应的解决方案。

2 问题及现象的原因

根本原因:从c++11开始的future/promise 等于多线程编程相关的标准接口在Linux下的gcc环境下,其底层实现是需要依赖pthread相关接口的,因此需要在xmake脚本中添加相应的库依赖。

3 尝试

3.1 直接添加pthread

既然是因为c++多线程编程相关功能的底层实现在Linux下的gcc环境是依赖的pthread相关接口, 而如前面所示,原始的xmake中并未显示地添加pthread库,那么直接在xmake添加pthread 库。其xmake脚本如下所示:

add_rules("mode.debug", "mode.release")target("explicit_stdc++")set_kind("binary")add_syslinks("pthread")add_files("src/*.cpp")

但是从编译以及ldd的结果来看,其并未为在链接库里面新增pthread库,并且运行仍然会出错。

gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ cat xmake.lua
add_rules("mode.debug", "mode.release")target("explicit_stdc++")set_kind("binary")add_syslinks("pthread")add_files("src/*.cpp")gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake
[ 25%]: ccache compiling.release src/main.cpp
[ 50%]: linking.release explicit_stdc++
[100%]: build ok!.../xmake_test/explicit_stdc++$ ldd build/linux/x86_64/release/explicit_stdc++linux-vdso.so.1 (0x00007fff7d3b0000)libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f8d9e680000)libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8d9e665000)  libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8d9e473000)libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8d9e324000)/lib64/ld-linux-x86-64.so.2 (0x00007f8d9e875000)
gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake run
terminate called after throwing an instance of 'std::system_error'what():  Unknown error -1

3.2 更改ld

对于这个问题,一开始笔者也是百思不得其解,折腾了半天,无奈之下,尝试将xmake的构建过程显示出来看看到底是什么情况:

gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake -vvv
[ 25%]: ccache compiling.release src/main.cpp
/usr/bin/gcc -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o src/main.cpp
[ 50%]: linking.release explicit_stdc++
/usr/bin/g++ -o build/linux/x86_64/release/explicit_stdc++ build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o -m64 -s -lpthread
[100%]: build ok!

如上,所示,其是使用g++作为“ld” 进行链接的,且在链接的参数中已经有显示地添加 -lpthread。

于是想到的另一个尝试就是修改ld,使用gcc作为链接器。xmake的资料相对来说还是太少了, 查找了一下,可以如下方式指定ld:

gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake f --ld=gcc
checking for platform ... linux
checking for architecture ... x86_64
gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake -vvvv
checking for gcc ... /usr/bin/gcc
checking for the c++ compiler (cxx) ... gcc
checking for /usr/bin/gcc ... ok
checking for flags (-fPIC) ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: ccache compiling.release src/main.cpp
/usr/bin/gcc -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o src/main.cpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
checking for flags (-fPIC) ... ok
[ 50%]: linking.release explicit_stdc++
gcc -o build/linux/x86_64/release/explicit_stdc++ build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o -m64 -s -lpthread
error: /usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&)::{lambda()#2}::_FUN()':
main.cpp:(.text._ZZSt9call_onceIMNSt13__future_base13_State_baseV2EFvPSt8functionIFSt10unique_ptrINS0_12_Result_baseENS4_8_DeleterEEvEEPbEJPS1_S9_SA_EEvRSt9once_flagOT_DpOT0_ENUlvE0_4_FUNEv[_ZZSt9call_onceIMNSt13__future_base13_State_baseV2EFvPSt8functionIFSt10unique_ptrINS0_12_Result_baseENS4_8_DeleterEEvEEPbEJPS1_S9_SA_EEvRSt9once_flagOT_DpOT0_ENUlvE0_4_FUNEv]+0x7): undefined reference to `std::__once_callable'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::__future_base::_State_baseV2::~_State_baseV2()':
main.cpp:(.text._ZNSt13__future_base13_State_baseV2D0Ev[_ZNSt13__future_base13_State_baseV2D5Ev]+0x2a): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::_Sp_counted_ptr_inplace<std::__future_base::_State_baseV2, std::allocator<std::__future_base::_State_baseV2>, (__gnu_cxx::_Lock_policy)2>::~_Sp_counted_ptr_inplace()':
main.cpp:(.text._ZNSt23_Sp_counted_ptr_inplaceINSt13__future_base13_State_baseV2ESaIS1_ELN9__gnu_cxx12_Lock_policyE2EED0Ev[_ZNSt23_Sp_counted_ptr_inplaceINSt13__future_base13_State_baseV2ESaIS1_ELN9__gnu_cxx12_Lock_policyE2EED5Ev]+0xa): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::__future_base::_Result<int>::~_Result()':
main.cpp:(.text._ZNSt13__future_base7_ResultIiED2Ev[_ZNSt13__future_base7_ResultIiED5Ev]+0xf): undefined reference to `std::__future_base::_Result_base::~_Result_base()'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::__future_base::_Result<int>::~_Result()':
main.cpp:(.text._ZNSt13__future_base7_ResultIiED0Ev[_ZNSt13__future_base7_ResultIiED5Ev]+0x13): undefined reference to `std::__future_base::_Result_base::~_Result_base()'
/usr/bin/ld: main.cpp:(.text._ZNSt13__future_base7_ResultIiED0Ev[_ZNSt13__future_base7_ResultIiED5Ev]+0x21): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::_Sp_counted_ptr_inplace<std::__future_base::_State_baseV2, std::allocator<std::__future_base::_State_baseV2>, (__gnu_cxx::_Lock_policy)2>::_M_destroy()':
main.cpp:(.text._ZNSt23_Sp_counted_ptr_inplaceINSt13__future_base13_State_baseV2ESaIS1_ELN9__gnu_cxx12_Lock_policyE2EE10_M_destroyEv[_ZNSt23_Sp_counted_ptr_inplaceINSt13__future_base13_State_baseV2ESaIS1_ELN9__gnu_cxx12_Lock_policyE2EE10_M_destroyEv]+0x5): undefined reference to `operator delete(void*)'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::__future_base::_State_baseV2::_M_do_set(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)':
main.cpp:(.text._ZNSt13__future_base13_State_baseV29_M_do_setEPSt8functionIFSt10unique_ptrINS_12_Result_baseENS3_8_DeleterEEvEEPb[_ZNSt13__future_base13_State_baseV29_M_do_setEPSt8functionIFSt10unique_ptrINS_12_Result_baseENS3_8_DeleterEEvEEPb]+0x63): undefined reference to `std::__throw_bad_function_call()'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::__future_base::_Result<int>::_M_destroy()':
main.cpp:(.text._ZNSt13__future_base7_ResultIiE10_M_destroyEv[_ZNSt13__future_base7_ResultIiE10_M_destroyEv]+0x26): undefined reference to `std::__future_base::_Result_base::~_Result_base()'
/usr/bin/ld: main.cpp:(.text._ZNSt13__future_base7_ResultIiE10_M_destroyEv[_ZNSt13__future_base7_ResultIiE10_M_destroyEv]+0x34): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::promise<int>::~promise()':
main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x5f): undefined reference to `std::future_category()'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x91): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0xe3): undefined reference to `std::logic_error::logic_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0xf2): undefined reference to `operator delete(void*)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x106): undefined reference to `operator delete(void*)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x10d): undefined reference to `vtable for std::future_error'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x129): undefined reference to `__cxa_allocate_exception'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x137): undefined reference to `typeinfo for std::future_error'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x142): undefined reference to `__cxa_init_primary_exception'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x14d): undefined reference to `std::logic_error::logic_error(std::logic_error const&)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x170): undefined reference to `std::__exception_ptr::exception_ptr::exception_ptr(void*)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x18d): undefined reference to `std::__exception_ptr::exception_ptr::swap(std::__exception_ptr::exception_ptr&)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x195): undefined reference to `std::__exception_ptr::exception_ptr::~exception_ptr()'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x19d): undefined reference to `std::__exception_ptr::exception_ptr::~exception_ptr()'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x1a5): undefined reference to `std::future_error::~future_error()'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x215): undefined reference to `std::__future_base::_Result_base::~_Result_base()'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x222): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x301): undefined reference to `std::__atomic_futex_unsigned_base::_M_futex_notify_all(unsigned int*)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x324): undefined reference to `operator delete(void*)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x338): undefined reference to `operator delete(void*)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiED2Ev[_ZNSt7promiseIiED5Ev]+0x33d): undefined reference to `std::terminate()'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `std::promise<int>::set_value(int&&)':
main.cpp:(.text._ZNSt7promiseIiE9set_valueEOi[_ZNSt7promiseIiE9set_valueEOi]+0xe2): undefined reference to `std::__once_callable'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiE9set_valueEOi[_ZNSt7promiseIiE9set_valueEOi]+0xf6): undefined reference to `std::__once_call'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiE9set_valueEOi[_ZNSt7promiseIiE9set_valueEOi]+0x103): undefined reference to `__once_proxy'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiE9set_valueEOi[_ZNSt7promiseIiE9set_valueEOi]+0x11d): undefined reference to `std::__throw_future_error(int)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiE9set_valueEOi[_ZNSt7promiseIiE9set_valueEOi]+0x171): undefined reference to `std::__atomic_futex_unsigned_base::_M_futex_notify_all(unsigned int*)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiE9set_valueEOi[_ZNSt7promiseIiE9set_valueEOi]+0x17f): undefined reference to `std::__throw_system_error(int)'
/usr/bin/ld: main.cpp:(.text._ZNSt7promiseIiE9set_valueEOi[_ZNSt7promiseIiE9set_valueEOi]+0x189): undefined reference to `std::__throw_future_error(int)'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `main.cold':
main.cpp:(.text.unlikely+0x4): undefined reference to `std::__exception_ptr::exception_ptr::~exception_ptr()'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `main':
main.cpp:(.text.startup+0x31): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: main.cpp:(.text.startup+0x86): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: main.cpp:(.text.startup+0x9b): undefined reference to `std::__future_base::_Result_base::_Result_base()'
/usr/bin/ld: main.cpp:(.text.startup+0x142): undefined reference to `std::__atomic_futex_unsigned_base::_M_futex_wait_until(unsigned int*, unsigned int, bool, std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> >)'/usr/bin/ld: main.cpp:(.text.startup+0x16e): undefined reference to `std::__exception_ptr::operator==(std::__exception_ptr::exception_ptr const&, std::__exception_ptr::exception_ptr const&)'
/usr/bin/ld: main.cpp:(.text.startup+0x178): undefined reference to `std::__exception_ptr::exception_ptr::~exception_ptr()'
/usr/bin/ld: main.cpp:(.text.startup+0x1b6): undefined reference to `std::cout'
/usr/bin/ld: main.cpp:(.text.startup+0x1bb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)'
/usr/bin/ld: main.cpp:(.text.startup+0x1c5): undefined reference to `std::cout'
/usr/bin/ld: main.cpp:(.text.startup+0x1ca): undefined reference to `std::ostream::operator<<(int)'
/usr/bin/ld: main.cpp:(.text.startup+0x1d2): 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> >&)'
/usr/bin/ld: main.cpp:(.text.startup+0x253): undefined reference to `std::__throw_future_error(int)'
/usr/bin/ld: main.cpp:(.text.startup+0x262): undefined reference to `std::__throw_future_error(int)'
/usr/bin/ld: main.cpp:(.text.startup+0x26d): undefined reference to `std::__exception_ptr::exception_ptr::exception_ptr(std::__exception_ptr::exception_ptr const&)'
/usr/bin/ld: main.cpp:(.text.startup+0x275): undefined reference to `std::rethrow_exception(std::__exception_ptr::exception_ptr)'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o: in function `_GLOBAL__sub_I_main':
main.cpp:(.text.startup+0x2d0): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: main.cpp:(.text.startup+0x2d7): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.ro._ZTISt11_Mutex_baseILN9__gnu_cxx12_Lock_policyE2EE[_ZTISt11_Mutex_baseILN9__gnu_cxx12_Lock_policyE2EE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.ro._ZTISt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE[_ZTISt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.ro._ZTINSt13__future_base13_State_baseV2E[_ZTINSt13__future_base13_State_baseV2E]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.ro._ZTINSt13__future_base7_ResultIiEE[_ZTINSt13__future_base7_ResultIiEE]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.ro._ZTINSt13__future_base7_ResultIiEE[_ZTINSt13__future_base7_ResultIiEE]+0x10): undefined reference to `typeinfo for std::__future_base::_Result_base'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.ro._ZTINSt13__future_base13_State_baseV27_SetterIiOiEE[_ZTINSt13__future_base13_State_baseV27_SetterIiOiEE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.ro._ZTISt23_Sp_counted_ptr_inplaceINSt13__future_base13_State_baseV2ESaIS1_ELN9__gnu_cxx12_Lock_policyE2EE[_ZTISt23_Sp_counted_ptr_inplaceINSt13__future_base13_State_baseV2ESaIS1_ELN9__gnu_cxx12_Lock_policyE2EE]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/usr/bin/ld: build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

其结果如上,其根本原因是,使用gcc作为链接器,它不会默认链接c++的标准库,因此需要显示在xmake中添加,如下所示:

add_rules("mode.debug", "mode.release")target("explicit_stdc++")set_kind("binary")add_syslinks("stdc++", "pthread")add_files("src/*.cpp")

其编译和运行结果,如下所示:

gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake -vvvv
checking for gcc ... /usr/bin/gcc
checking for dmd ... no
checking for ldc2 ... no
checking for gdc ... no
checking for zig ... no
checking for zig ... no
checking for gcc ... /usr/bin/gcc
checking for the c++ compiler (cxx) ... gcc
checking for /usr/bin/gcc ... ok
checking for flags (-fPIC) ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: ccache compiling.release src/main.cpp
/usr/bin/gcc -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o src/main.cpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
checking for flags (-fPIC) ... ok
[ 50%]: linking.release explicit_stdc++
gcc -o build/linux/x86_64/release/explicit_stdc++ build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o -m64 -s -lstdc++ -lpthread
[100%]: build ok!
gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake run
future got value: 42

如上所示,已经解决了此问题,可以程序可以正常运行了。

Note: 此种解决方案有两个问题

  1. 需要显示地更改ld,构建过程多了xmake f xxx 这样的配置过程
  2. 不同操作系统或者运行环境,使用的c++标准库可能会有不同,因此添加的“stdc++”标准可可能需要根据情况进行灵活变更

3.3 只需显示添加sdc++库

虽然根据3.2节可以解决问题,但感觉还是有些复杂。于是乎做了一番尝试,发现其实只需要显示地添加stdc++标准库,即使使用默认的g++作为链接器,也可以正常的编译和运行。如下所示:

gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake -vvvv
checking for platform ... linux
checking for architecture ... x86_64
checking for gcc ... /usr/bin/gcc
checking for dmd ... no
checking for ldc2 ... no
checking for gdc ... no
checking for zig ... no
checking for zig ... no
checking for gcc ... /usr/bin/gcc
checking for the c++ compiler (cxx) ... gcc
checking for /usr/bin/gcc ... ok
checking for flags (-fPIC) ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: ccache compiling.release src/main.cpp
/usr/bin/gcc -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o src/main.cpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
checking for g++ ... /usr/bin/g++
checking for the linker (ld) ... g++
checking for /usr/bin/g++ ... ok
checking for flags (-fPIC) ... ok
[ 50%]: linking.release explicit_stdc++
/usr/bin/g++ -o build/linux/x86_64/release/explicit_stdc++ build/.objs/explicit_stdc++/linux/x86_64/release/src/main.cpp.o -m64 -s -lstdc++ -lpthread
[100%]: build ok!
gw123@DESKTOP-KJUN5HL:~/workspace/temp/xmake_test/explicit_stdc++$ xmake run
future got value: 42

至此,问题解决,笔者最终也是选择的最后这种方案,只需显示地添加stdc++库和pthread库即可。 至于这其中的根本原因,笔者目前还不胜了解[捂脸]。 有知道其根本原因的大牛也希望能不吝赐教[抱拳]

xmake经验总结1:解决c++ future/promise抛出std::system_error的问题相关推荐

  1. Future.get()抛出ExecutionException或InterruptedException?

    ExecutionException和InterruptedException是两个非常不同的事情. ExecutionException封装了正在执行的线程抛出的任何异常,所以如果线程是做某种IO导 ...

  2. future promise java_Future与Promise

    https://code.csdn.NET/DOC_Scala/chinese_scala_offical_document/file/Futures-and-Promises-cn.md#ancho ...

  3. rust异步编程--理解并发/多线程/回调/异步/future/promise/async/await/tokio

    1. 异步编程简介 通常我们将消息通信分成同步和异步两种: 同步就是消息的发送方要等待消息返回才能继续处理其它事情 异步就是消息的发送方不需要等待消息返回就可以处理其它事情 很显然异步允许我们同时做更 ...

  4. folly教程系列之:future/promise

         attension:本文严禁转载. 一.前言 promise/future是一个非常重要的异步编程模型,它可以让我们摆脱传统的回调陷阱,从而使用更加优雅.清晰的方式进行异步编程.c++11中 ...

  5. 【转】现代c++开发利器folly教程系列之:future/promise

    一.前言 promise/future是一个非常重要的异步编程模型,它可以让我们摆脱传统的回调陷阱,从而使用更加优雅.清晰的方式进行异步编程.c++11中已经开始支持std::future/std:: ...

  6. C++异步调用利器future/promise实现原理

    前言 在异步编程中,各种回调将让人眼花缭乱,代码分散,维护起来十分困难.boost和C++11 的 future/promise 提供了一个很好的解决方案,使得代码更加漂亮.易维护. 在工作中,我也用 ...

  7. future promise shared_future简单使用

    #include #include #include /furture 和promi******************/ int display(const int& value) { re ...

  8. C++11 并发指南四(future 详解一 std::promise 介绍)

    前面两讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread 和 std::m ...

  9. 解决Uncaught (in promise) reason的问题

    解决Uncaught (in promise) reason的问题 报错如下 错误代码 new Promise((resolve, reject) => {//resolve(123);reje ...

  10. SpringCloud(10)—— 国寿i动项目经验之(解决前后端跨域请求技术)

    国寿i动项目经验之(解决前后端跨域请求技术): 由于网段原因,导致前端js请求后端服务接口出现跨域,没法实现正常的请求,所以需要对请求进行跨域处理 引入jar: <!-- 解决前后端接口交互跨域 ...

最新文章

  1. 单一窗口关区备案_【干货】上海国际贸易单一窗口货物申报对接版(信天翁)“两段准入” 操作手册...
  2. 学号 20175223 《Java程序设计》第4周学习总结
  3. Spring JDBC-自增键和行集RowSet
  4. [数组] 连续子数组的最大和 --- LeetCode53
  5. 把svg图标制作成字体图标_让我们用SVG符号和CSS变量制作彩色图标
  6. php 同时导出两个文件_使用orangehill/iseed自动反向生成数据填充文件
  7. Update与JOIN使用
  8. 编辑距离问题(Edit Distance Problem)
  9. Migrations有两个文件迁移数据的方法
  10. u盘重装惠普服务器系统,教你hp惠普u盘重装win10系统
  11. 笔记本安装黑苹果MacOS BigSur记录
  12. 思维导图不会画,没关系,实用思维导图模板分享
  13. android horizontalscrollview 动画,Android horizontalscrollview使用教程
  14. 分类学 · 狡兔为何偏要有三窟???
  15. js 小写金额转大写金额
  16. 用计算机处理表格信息,用计算机处理数据
  17. IPSec IKEv1IKEv2
  18. 彻底卸载secureCRT
  19. String.format()详解与遇到的问题(UnknownFormatConversionException)
  20. android微信支付开发过程

热门文章

  1. 超自动化如何提升保险业?
  2. HUAWEI 机试题:VLAN资源池
  3. U盘格式化了怎样恢复还原(必学技能)
  4. pop3邮箱怎么设置收发服务器端口,pop3设置(如何设置邮箱服务器?IMAP、POP3有何区别?)...
  5. Navicat连接Oracle
  6. iOS越狱系统绕过frida检测
  7. 浅谈Es6中import * as xxx from几种基本用法
  8. 2016年北京邮电大学计算机考研机试试题及答案
  9. WordPress网站配置腾讯云cdn缓存!
  10. git简介以及windows下的git安装配置gitgub推送