@@ -68,11 +68,21 @@ template class LLVM_EXPORT_TEMPLATE basic_parser<float>;
68
68
template class LLVM_EXPORT_TEMPLATE basic_parser<std::string>;
69
69
template class LLVM_EXPORT_TEMPLATE basic_parser<char >;
70
70
71
- template class opt <unsigned >;
72
- template class opt <int >;
73
- template class opt <std::string>;
74
- template class opt <char >;
75
- template class opt <bool >;
71
+ #if !defined(LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS) || \
72
+ !(defined(_MSC_VER) && !defined(__clang__))
73
+ // Only instantiate opt<std::string> when not building a Windows DLL with MSVC.
74
+ // When exporting opt<std::string>, MSVC cl implicitly exports symbols for
75
+ // std::basic_string through transitive inheritance via std::string. These
76
+ // symbols may appear in other TUs with different linkage, leading to duplicate
77
+ // symbol conflicts.
78
+ template class LLVM_EXPORT_TEMPLATE opt<std::string>;
79
+ #endif
80
+
81
+ template class LLVM_EXPORT_TEMPLATE opt<bool >;
82
+ template class LLVM_EXPORT_TEMPLATE opt<char >;
83
+ template class LLVM_EXPORT_TEMPLATE opt<int >;
84
+ template class LLVM_EXPORT_TEMPLATE opt<unsigned >;
85
+
76
86
} // namespace cl
77
87
} // namespace llvm
78
88
@@ -95,6 +105,15 @@ void parser<float>::anchor() {}
95
105
void parser<std::string>::anchor() {}
96
106
void parser<char >::anchor() {}
97
107
108
+ // These anchor functions instantiate opt<T> and reference its virtual
109
+ // destructor to ensure MSVC exports the corresponding vtable and typeinfo when
110
+ // building a Windows DLL. Without an explicit reference, MSVC may omit the
111
+ // instantiation at link time even if it is marked DLL-export.
112
+ void opt_bool_anchor () { opt<bool > anchor{" " }; }
113
+ void opt_char_anchor () { opt<char > anchor{" " }; }
114
+ void opt_int_anchor () { opt<int > anchor{" " }; }
115
+ void opt_unsigned_anchor () { opt<unsigned > anchor{" " }; }
116
+
98
117
// ===----------------------------------------------------------------------===//
99
118
100
119
const static size_t DefaultPad = 2 ;
0 commit comments