[cfe-dev] Why a few std::string function definitions are not in the object code?

Xiaolong Tang xiaolong.snake at gmail.com
Fri Feb 18 14:42:24 PST 2011


Thanks Doug!
> 
> On Feb 18, 2011, at 1:18 PM, Xiaolong Tang wrote:
> 
> > Hi all, 
> > 
> > I am working on whole-program analysis, and expect the final bitcode
> > to have all function definitions. When I examined the final bitcode of
> > a simple program using the standard string, I found that some function
> > defintions are missing. Particularly I am interested in the following
> > function definitions which are missing in the final bitcode. 
> > 
> >  // string constructor
> >  declare void @_ZNSsC1Ev(%"struct.std::string")
> >  // After name demangling 
> >  declare void @std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()(%"struct.std::string")
> > 
> >  // allocator constructor
> >  declare void @_ZNSaIcEC1Ev(%"struct.std::allocator<char>") nounwind
> >  // After name demangling 
> >  declare void @std::allocator<char>::allocator()(%"struct.std::allocator<char>") nounwind
> > 
> >  // string constructor from a C string
> >  declare void @_ZNSsC1EPKcRKSaIcE(%"struct.std::string", i8, %"struct.std::allocator<char>")
> >  // After name demangling 
> >  declare void @std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)(%"struct.std::string", i8, %"struct.std::allocator<char>")
> > 
> >  // string assignment
> >  declare %"struct.std::string" @_ZNSsaSERKSs(%"struct.std::string", %"struct.std::string")
> >  // After name demangling 
> >  declare %"struct.std::string" @std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)(%"struct.std::string", %"struct.std::string")
> > 
> > So, I am wondering why it is so? Specifically, I have a few questions. 
> > 
> > 1. Is the above compilation result related to the compilation mode and
> > instantiation mechanism of C++ templates?
> 
> Yes. Your standard library is using extern templates to suppress the generation of these definitions in each translation unit. If you look in your standard library headers, you'll see something like:
> 
> 	extern template class basic_string<char>;

I thought that the keyword "export" serves the purpose that "extern"
does here. Now I have the clue. Thanks.

> 
> > 2. Does clang (or linker) provide any way to force these function
> >   definitions to be present in the final bitcode? 
> 
> If you can comment out or #define away those extern templates, you'll see all of the function definitions. I don't know of any easier way.
> 
> 	- Doug

Best, 
Xiaolong



More information about the cfe-dev mailing list