[LLVMbugs] [Bug 20593] New: link success with static template member depends on optimisation level

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Aug 8 06:29:34 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20593

            Bug ID: 20593
           Summary: link success with static template member depends on
                    optimisation level
           Product: clang
           Version: 3.4
          Hardware: PC
                OS: FreeBSD
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: kamikaze at bsdforen.de
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

First of all I do not know whether static template members are permitted. I
figure there are some initialisation issues for the linker to sort out if a
template is used in multiple object files.

In any way I think the behaviour should be the same for all optimisation
levels.


EXAMPLE CODE

--- foo.hpp ---
#ifndef _FOO_HPP_
#define _FOO_HPP_

template <class T>
class foo {
        private:
        static T static_member;

        public:
        foo() {
        }

        static T const & static_method() {
                return foo<T>::static_member;
        }

        T const & dynamic_method() const {
                return this->static_member;
        }
};

#endif /* _FOO_HPP_ */

--- main.cpp ---
#include "foo.hpp"

int main() {
        foo<int> bar;
        bar.dynamic_method();
        foo<int>::static_method();
        return 0;
}

CLANG VERSION

> c++ -v
FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512
Target: x86_64-unknown-freebsd10.0
Thread model: posix
Selected GCC installation: 

OS

> uname -a
FreeBSD AprilRyan.norad 10.0-STABLE FreeBSD 10.0-STABLE #0 r268659: Tue Jul 15
11:25:22 CEST 2014    
root at AprilRyan.norad:/usr/obj/S403/amd64/usr/src/sys/S403  amd64

DIRECT COMPILE

> c++ -O0 main.cpp
/tmp/main-b8dc96.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
/tmp/main-b8dc96.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)


> c++ -O1 main.cpp
/tmp/main-17cbe6.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
/tmp/main-17cbe6.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)


> c++ -O2 main.cpp
> ll
total 16
-rwxr-xr-x  1 kamikaze  kamikaze  7371  8 Aug 15:30 a.out*
-rw-r--r--  1 kamikaze  kamikaze   295  8 Aug 15:04 foo.hpp
-rw-r--r--  1 kamikaze  kamikaze   113  8 Aug 15:08 main.cpp


COMPILE FIRST LINK LATER

> c++ -c -O0 main.cpp
> c++ -O0 main.o
main.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
main.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)


> c++ -c -O1 main.cpp
> c++ -O1 main.o
main.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
main.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)


> c++ -c -O2 main.cpp
> c++ -O2 main.o
> ll
total 20
-rwxr-xr-x  1 kamikaze  kamikaze  7371  8 Aug 15:35 a.out*
-rw-r--r--  1 kamikaze  kamikaze   295  8 Aug 15:04 foo.hpp
-rw-r--r--  1 kamikaze  kamikaze   113  8 Aug 15:08 main.cpp
-rw-r--r--  1 kamikaze  kamikaze  1256  8 Aug 15:35 main.o

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140808/bff57b25/attachment.html>


More information about the llvm-bugs mailing list