You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义g++9.4.0工具链中shared_ptr_base.h因alignas报错:__tag未声明

升级g++至9.4.0后std::shared_ptr交叉编译错误问题

问题背景

将工具链从g6.4.0替换为g9.4.0后,Linux内核可正常编译,但现有项目交叉编译时,使用C++ <memory> 头文件中的std::shared_ptr出现编译错误。

涉及头文件代码

#ifndef TEMPLATE_SHARED_PTR_H
#define TEMPLATE_SHARED_PTR_H

#include <memory>

class CSignalingEvent;
class CBlockableCommsConn;
class CVirtualConn;
class CTcpConn;
class CSourceMachine;
class CSrcMachineProtocolIF;
class CExternalMachineIF;
class CAMPProtocol;

namespace gv
{
typedef std::shared_ptr<CSignalingEvent> shared_ptr_CSignalingEvent_t;
typedef std::shared_ptr<CBlockableCommsConn> shared_ptr_CBlockableCommsConn_t;
typedef std::shared_ptr<CVirtualConn> shared_ptr_CVirtualConn_t;
typedef std::shared_ptr<CTcpConn> shared_ptr_CTcpConn_t;
typedef std::shared_ptr<CSrcMachineProtocolIF> shared_ptr_CSrcMachineProtocolIF_t;
typedef std::shared_ptr<CSourceMachine> shared_ptr_CSourceMachine_t;
typedef std::shared_ptr<CExternalMachineIF> shared_ptr_CExternalMachineIF_t;
typedef std::shared_ptr<CAMPProtocol> shared_ptr_CAMPProtocol_t;

typedef std::weak_ptr<CSrcMachineProtocolIF> weak_ptr_CSrcMachineProtocolIF_t;
typedef std::weak_ptr<CSourceMachine> weak_ptr_CSourceMachine_t;
typedef std::weak_ptr<CExternalMachineIF> weak_ptr_CExternalMachineIF_t;
typedef std::weak_ptr<CAMPProtocol> weak_ptr_CAMPProtocol_t;

template<class T> class CNullDeleter { public: void operator()(T* ptr) const { /* do nothing */ } };

} // namespace gv

#endif // TEMPLATE_SHARED_PTR_H

编译错误信息

.../c++/9.4.0/bits/shared_ptr_base.h: In static member function 'static const std::type_info& std::_Sp_make_shared_tag::_S_ti()':
.../c++/9.4.0/bits/shared_ptr_base.h:511:49: error: '__tag' was not declared in this scope

已尝试的无效解决方案

  • 针对pstdalign.h相关的方案:当前工具链仅存在stdalign.h,该方案无效
  • 针对重定义alignas关键字的方案:项目中未重定义该关键字,方案无效

g++版本结构体对比

g++9.4.0的_Sp_make_shared_tag结构体

struct _Sp_make_shared_tag
{
private:
  template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
    friend class _Sp_counted_ptr_inplace;

  static const type_info&
  _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
  {
    alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { };
    return reinterpret_cast<const type_info&>(__tag);
  }

  static bool _S_eq(const type_info&) noexcept;
};

g++6.4.0的_Sp_make_shared_tag结构体

struct _Sp_make_shared_tag {}

需求

注释掉alignas(type_info)后编译可正常通过,但不确定问题根源,寻求无需修改源码的干净解决方案,例如添加编译标志或自定义头文件。

内容的提问来源于stack exchange,提问作者RAZ_Muh_Taz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 07:57:34