gpt4 book ai didi

c++ - 从多个编译单元引用模板化静态变量时,Clang 链接到不同的位置

转载 作者:太空狗 更新时间:2023-10-29 23:17:13 25 4
gpt4 key购买 nike

在尝试使用 Clang 编译现有(GCC 开发的)代码库时,我们遇到了这个有趣的问题。结果,Clang 编译的可执行文件创建了一些单例的多个实例。不确定我们的使用和理解是否符合标准,或者Linux上的GCC和/或Clang或C++标准库和工具链是否真的有问题。

  • 我们使用工厂来创建单例实例
  • 实际创建委托(delegate)给策略模板
  • 在某些地方,我们使用了单例工厂的变体,其中单例的实际类型可以在定义站点配置,而无需向客户端访问单例。客户端只知道接口(interface)类型
  • 当通过从不同编译单元使用的内联函数引用“相同”静态变量时出现问题

以下是摘录,省略了任何锁定、生命周期问题、初始化和清理


文件-1:clang-static-init.hpp

#include <iostream>
using std::cout;

namespace test {

/* === Layer-1: a singleton factory based on a templated static variable === */

template<typename I ///< Interface of the product type
,template <class> class Fac ///< Policy: actual factory to create the instance
>
struct Holder
{
static I* instance;

I&
get()
{
if (!instance)
{
cout << "Singleton Factory: invoke Fabrication ---> address of static instance variable: "<<&instance<<"...\n";

instance = Fac<I>::create();
}
return *instance;
}
};

/**
* allocate storage for the per-type shared
* (static) variable to hold the singleton instance
*/
template<typename I
,template <class> class F
>
I* Holder<I,F>::instance;




template<typename C>
struct Factory
{
static C*
create()
{
return new C();
}
};





/* === Layer-2: configurable product type === */

template<typename I>
struct Adapter
{
typedef I* FactoryFunction (void);

static FactoryFunction* factoryFunction;


template<typename C>
static I*
concreteFactoryFunction()
{
return static_cast<I*> (Factory<C>::create());
}


template<typename X>
struct AdaptedConfigurableFactory
{
static X*
create()
{
return (*factoryFunction)();
}
};
};

/** storage for the per-type shared function pointer to the concrete factory */
template<typename I>
typename Adapter<I>::FactoryFunction* Adapter<I>::factoryFunction;



template<typename C>
struct TypeInfo { };



/**
* Singleton factory with the ability to configure the actual product type C
* only at the \em definition site. Users get to see only the interface type T
*/
template<typename T>
struct ConfigurableHolder
: Holder<T, Adapter<T>::template AdaptedConfigurableFactory>
{
/** define the actual product type */
template<typename C>
ConfigurableHolder (TypeInfo<C>)
{
Adapter<T>::factoryFunction = &Adapter<T>::template concreteFactoryFunction<C>;
}
};





/* === Actual usage: Test case fabricating Subject instances === */

struct Subject
{
static int creationCount;

Subject();

};

typedef ConfigurableHolder<Subject> AccessPoint;

/** singleton factory instance */
extern AccessPoint fab;


Subject& fabricate();

} // namespace test

文件 2:clang-static-init-1.cpp

#include "clang-static-init.hpp"


test::Subject&
localFunction()
{
return test::fab.get();
}


int
main (int, char**)
{
cout << "\nStart Testcase: invoking two instances of the configurable singleton factory...\n\n";

test::Subject& ref1 = test::fab.get();
test::Subject& sub2 = test::fabricate(); ///NOTE: invoking get() from within another compilation unit reveales the problem
test::Subject& sub3 = localFunction();

cout << "sub1=" << &ref1
<< "\nsub2="<< &sub2
<< "\nsub3="<< &sub3
<< "\n";


return 0;
}

文件 3:clang-static-init-2.cpp

#include "clang-static-init.hpp"



namespace test {


int Subject::creationCount = 0;

Subject::Subject()
{
++creationCount;
std::cout << "Subject("<<creationCount<<")\n";
}



namespace {
TypeInfo<Subject> shall_build_a_Subject_instance;
}

/**
* instance of the singleton factory
* @note especially for this example we're using just \em one
* shared instance of the factory.
* Yet still, two (inlined) calls to the get() function might
* access different addresses for the embedded singleton instance
*/
AccessPoint fab(shall_build_a_Subject_instance);


Subject&
fabricate()
{
return fab.get();
}


} // namespace test

要点

  • 我们只使用一个 AccessPoint 实例
  • 不过,不同的编译单元使用(内联)函数Holder<T,F>::get() , 将看到静态变量的不同位置 instance
  • 而实际的 ctor 调用 ConfigurableHolder以要创建的单例的具体类型为模板,此特定类型信息被删除;它与 Adapter 的类型无关或 ConfigurableHolder
  • 如果这种理解是正确的,get() 的所有用法应该看到相同类型的 Holder因此嵌入在 Holder 中的静态变量的位置相同
  • 但实际上 Clang 编译的可执行文件再次为 sub2 调用工厂,这是从另一个编译单元调用的,而sub1sub3按预期共享相同的单例实例

有趣的是,使用 Clang-3.0 构建的可执行文件的符号表显示此静态变量已链接两次(使用 Clang-3.2 时行为相同)

10: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS research/clang-static-init-1.cpp
11: 0000000000400cd0 11 FUNC LOCAL DEFAULT 14 global constructors keyed to a
12: 0000000000400b70 114 FUNC LOCAL DEFAULT 14 test::Holder<test::Subject, test::Adapter<test::Subject>::AdaptedConfigurableFactory>::get()
13: 00000000004027e0 8 OBJECT LOCAL DEFAULT 28 test::Holder<test::Subject, test::Adapter<test::Subject>::AdaptedConfigurableFactory>::instance
14: 00000000004027d8 1 OBJECT LOCAL DEFAULT 28 std::__ioinit
15: 0000000000400b10 62 FUNC LOCAL DEFAULT 14 __cxx_global_var_init
16: 0000000000000000 0 FILE LOCAL DEFAULT ABS research/clang-static-init-2.cpp
17: 00000000004010e8 0 NOTYPE LOCAL DEFAULT 17 GCC_except_table9
18: 0000000000400e60 16 FUNC LOCAL DEFAULT 14 global constructors keyed to a
19: 00000000004027f9 1 OBJECT LOCAL DEFAULT 28 test::(anonymous namespace)::shall_build_a_Subject_instance
20: 0000000000400de0 114 FUNC LOCAL DEFAULT 14 test::Holder<test::Subject, test::Adapter<test::Subject>::AdaptedConfigurableFactory>::get()
21: 0000000000402800 8 OBJECT LOCAL DEFAULT 28 test::Holder<test::Subject, test::Adapter<test::Subject>::AdaptedConfigurableFactory>::instance

...而 GCC-4.7.2 编译的可执行文件的相关部分按预期读取

44: 0000000000400b8c    16 FUNC    GLOBAL DEFAULT   14 localFunction()
45: 00000000004026dc 1 OBJECT GLOBAL DEFAULT 28 test::fab
46: 0000000000400c96 86 FUNC WEAK DEFAULT 14 test::Holder<test::Subject, test::Adapter<test::Subject>::AdaptedConfigurableFactory>::get()
47: 00000000004026e0 272 OBJECT GLOBAL DEFAULT 28 std::cout
48: 0000000000000000 0 FUNC GLOBAL DEFAULT UND std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(st
49: 0000000000400d4b 16 FUNC GLOBAL DEFAULT 14 test::fabricate()
50: 0000000000000000 0 FUNC GLOBAL DEFAULT UND std::basic_ostream<char, std::char_traits<char> >::operator<<(void const*)
51: 00000000004026d0 8 OBJECT UNIQUE DEFAULT 28 test::Holder<test::Subject, test::Adapter<test::Subject>::AdaptedConfigurableFactory>::instance
52: 0000000000400cec 15 FUNC WEAK DEFAULT 14 test::Adapter<test::Subject>::AdaptedConfigurableFactory<test::Subject>::create()
53: 00000000004026c8 8 OBJECT UNIQUE DEFAULT 28 test::Adapter<test::Subject>::factoryFunction

我们使用 Debian/stable 64bit(GCC-4.7 和 Clang-3.0)和 Debian/testing 32bit(Clang-3.2)来构建

最佳答案

修复方法是在外部声明您的单例模板类,并在单个编译单元中显式实例化单例。

如果您的编译单元位于单独的(共享)库中,那么 Clang 之所以这样做,只是因为它可以。

编译代码时,编译器会在每次完全指定时实例化单例模板。在链接时,除了一个实例之外的所有实例都被丢弃。但是,如果您的项目中有共享库,并且有多个链接时间,会发生什么情况?每个共享对象都将有一个模板实例。 GCC 确保在最终的可执行文件中只有一个幸存的模板实例化(可能使用 vague linkage ?),但显然 Clang 没有。

关于c++ - 从多个编译单元引用模板化静态变量时,Clang 链接到不同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com