Bug 50560 - g++ optimization -O3 is removing symbols from templates
Summary: g++ optimization -O3 is removing symbols from templates
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.5.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-28 18:24 UTC by Mark
Modified: 2011-09-28 19:38 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
sample source files to reproduce the problem (794 bytes, application/x-tar)
2011-09-28 18:24 UTC, Mark
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mark 2011-09-28 18:24:19 UTC
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.
Comment 1 Jonathan Wakely 2011-09-28 18:31:23 UTC
definitions of templates need to be visible in every file which instantiates them
Comment 2 Jonathan Wakely 2011-09-28 18:37:37 UTC
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>;
Comment 3 Mark 2011-09-28 19:38:05 UTC
Thanks for the quick response.  Your solution works.