Created attachment 25377 [details] sample source files to reproduce the problem gcc 4.5.1: When compiling/linking my sample library, it seems that the optimization level -O3 is not finding symbols where -O2 & -O1 works just fine. make g++ -g -fPIC -Wall -O3 -c -o bug_Option.o bug_Option.cpp g++ -g -fPIC -Wall -O3 -c -o UseOption.o UseOption.cpp g++ -g -shared -Wl,-z,defs -g -shared -o libOption.so bug_Option.o UseOption.o UseOption.o: In function `foo()': /tmp/bug/UseOption.cpp:4: undefined reference to `Option<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Option()' collect2: ld returned 1 exit status make: *** [libOption.so] Error 1 I've attached my code. change the GNUmakefile CXXFLAGS to -O2 to see it work.
definitions of templates need to be visible in every file which instantiates them
your hack to allow "separately compiled template components" isn't valid C++, but you can make the code valid by putting an explicit instantiation declaration in the header: extern template class Option<std::string>; and an explicit instantiation definition in bug_Option.cpp (after all the members have been defined) template class Option<std::string>;
Thanks for the quick response. Your solution works.