1

我使用 Crosstool-ng 构建了一个 gcc 工具链,但生成的 g++ 不会编译和/或链接 C++11 异常。

如果我尝试编译:

#include <exception>

int main()
{
    std::current_exception();
}

通过:

arm-none-linux-gnueabihf-g++ -std=c++11 foo.cpp

我得到:

foo.cpp: In function 'int main()':
foo.cpp:5:9: error: 'current_exception' is not a member of 'std'
         std::current_exception();
         ^~~

但是,如果我另外提供 cortex-a9 作为 cpu,则行为会发生变化:

arm-none-linux-gnueabihf-g++ -mcpu=cortex-a9 -std=c++11 foo.cpp

结果是:

/tmp/cc13AEjE.o: In function `main':
foo.cpp:(.text+0x14): undefined reference to `std::current_exception()'
foo.cpp:(.text+0x20): undefined reference to 
`std::__exception_ptr::exception_ptr::~exception_ptr()'
collect2: error: ld returned 1 exit status

我在工具链中寻找 current_exception ,这对我来说似乎很好:

grep -r -e current_exception arm-none-linux-gnueabihf/


Binary file arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/sysroot/lib/libstdc++.so.6.0.22 matches
Binary file arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/sysroot/lib/libstdc++.a matches
Binary file arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/sysroot/lib/libsupc++.a matches
arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/include/c++/6.3.0/cxxabi.h:  __cxa_current_exception_type() _GLIBCXX_NOTHROW __attribute__ ((__pure__));
arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/include/c++/6.3.0/future:     (*_M_result)->_M_error = current_exception();
arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/include/c++/6.3.0/future:     (*_M_result)->_M_error = current_exception();
arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/include/c++/6.3.0/bits/exception_ptr.h:  exception_ptr current_exception() _GLIBCXX_USE_NOEXCEPT;
arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/include/c++/6.3.0/bits/exception_ptr.h:      friend exception_ptr std::current_exception() _GLIBCXX_USE_NOEXCEPT;
arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/include/c++/6.3.0/bits/exception_ptr.h:     return current_exception();
arm-none-linux-gnueabihf/arm-none-linux-    gnueabihf/include/c++/6.3.0/bits/nested_exception.h:    nested_exception()     noexcept : _M_ptr(current_exception()) { }

我用于 Crosstool-ng 的设置是:

arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-g++ -v

Using built-in specs.
COLLECT_GCC=x-tools/arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-g++
COLLECT_LTO_WRAPPER=/home/builduser/x-tools/arm-none-linux-gnueabihf/libexec/gcc/arm-none-linux-gnueabihf/6.3.0/lto-wrapper
Target: arm-none-linux-gnueabihf
Configured with: /home/builduser/build/.build/src/gcc-6.3.0/configure 
--build=x86_64-build_pc-linux-gnu 
--host=x86_64-build_pc-linux-gnu 
--target=arm-none-linux-gnueabihf 
--prefix=/home/builduser/x-tools/arm-none-linux-gnueabihf 
--with-sysroot=/home/builduser/x-tools/arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/sysroot 
--enable-languages=c,c++ 
--with-float=hard 
--with-pkgversion='crosstool-NG crosstool-ng-1.23.0' 
--disable-sjlj-exceptions 
--enable-__cxa_atexit 
--disable-libmudflap 
--disable-libgomp 
--disable-libssp 
--disable-libquadmath 
--disable-libquadmath-support 
--disable-libsanitizer 
--disable-libmpx 
--with-gmp=/home/builduser/build/.build/arm-none-linux-gnueabihf/buildtools 
--with-mpfr=/home/builduser/build/.build/arm-none-linux-gnueabihf/buildtools 
--with-mpc=/home/builduser/build/.build/arm-none-linux-gnueabihf/buildtools 
--with-isl=/home/builduser/build/.build/arm-none-linux-gnueabihf/buildtools 
--disable-lto 
--with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++ -lm' 
--enable-threads=posix 
--enable-target-optspace 
--disable-plugin 
--disable-nls 
--disable-multilib 
--with-local-prefix=/home/builduser/x-tools/arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/sysroot 
--enable-long-long
Thread model: posix
gcc version 6.3.0 (crosstool-NG crosstool-ng-1.23.0) 

构建 std::array 示例工作正常。

编辑:

如果我在CT_ARCH_CPU="cortex-a9"我的 crostool-ng 配置中包含该选项(这导致--with-cpu=cortex-a9在 g++ 中),则 std::current_exception 示例可以正常工作。

所以问题是:

为什么编译器应该为其生成代码的 cpu 变体以及 cpu 变体被告知工具链的方式(-mcpu在运行时与 inbuild 时--with-cpu)正在影响编译/链接 std::current_exception 的能力?

4

0 回答 0