Template linking problem (multiple definition)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georg Teichtmeister

    Template linking problem (multiple definition)

    Hello!

    We are developing a math - library for realtime applications and want to
    use some given mathlibraries as base(ipp, MTL, .. ). Our library is a
    wrapper for those and you should be able to select the underlying
    library by template parameter. Therefore we split up our library in two
    namespaces:

    The first one defines namespace-global functions which call the
    baselibrary functions. These are templatefunctio ns which some
    spezialitions for double and float.

    The second one defines classes, where the functions of the first
    namespace will be called.

    Now comes the problem: If I include our mathlibrary into two
    compilationunit s the linker gives a error which says:

    A.lo:in function Namespace1::ns_ globalfunction:
    A.lo:multiple definition of ns_globalfuncti on
    B.lo:first defined here

    And this comes for every namespaceglobal template function of the first
    namespace for every spezialition.

    regards,
    Georg

  • Georg Teichtmeister

    #2
    Re: Template linking problem (multiple definition)



    Georg Teichtmeister wrote:[color=blue]
    > Hello!
    >
    > We are developing a math - library for realtime applications and want to
    > use some given mathlibraries as base(ipp, MTL, .. ). Our library is a
    > wrapper for those and you should be able to select the underlying
    > library by template parameter. Therefore we split up our library in two
    > namespaces:
    >
    > The first one defines namespace-global functions which call the
    > baselibrary functions. These are templatefunctio ns which some
    > spezialitions for double and float.
    >
    > The second one defines classes, where the functions of the first
    > namespace will be called.
    >
    > Now comes the problem: If I include our mathlibrary into two
    > compilationunit s the linker gives a error which says:
    >
    > A.lo:in function Namespace1::ns_ globalfunction:
    > A.lo:multiple definition of ns_globalfuncti on
    > B.lo:first defined here
    >
    > And this comes for every namespaceglobal template function of the first
    > namespace for every spezialition.
    >[/color]

    sorry, forgot to mention that I use gcc-2.95.3 and gcc-3.2


    Comment

    • John Harrison

      #3
      Re: Template linking problem (multiple definition)


      "Georg Teichtmeister" <teichtmeister@ emt.tugraz.at> wrote in message
      news:3F5836FF.4 080207@emt.tugr az.at...[color=blue]
      > Hello!
      >
      > We are developing a math - library for realtime applications and want to
      > use some given mathlibraries as base(ipp, MTL, .. ). Our library is a
      > wrapper for those and you should be able to select the underlying
      > library by template parameter. Therefore we split up our library in two
      > namespaces:
      >
      > The first one defines namespace-global functions which call the
      > baselibrary functions. These are templatefunctio ns which some
      > spezialitions for double and float.
      >
      > The second one defines classes, where the functions of the first
      > namespace will be called.
      >
      > Now comes the problem: If I include our mathlibrary into two
      > compilationunit s the linker gives a error which says:
      >
      > A.lo:in function Namespace1::ns_ globalfunction:
      > A.lo:multiple definition of ns_globalfuncti on
      > B.lo:first defined here
      >
      > And this comes for every namespaceglobal template function of the first
      > namespace for every spezialition.
      >
      > regards,
      > Georg
      >[/color]

      Did you inline the fully specialised functions? I.e.

      template <typename T>
      void f(T x)
      {
      ...
      }

      template <>
      inline void f<double>(doubl e x)
      {
      ...
      }

      john


      Comment

      • Georg Teichtmeister

        #4
        Re: Template linking problem (multiple definition)

        [color=blue]
        >
        > Did you inline the fully specialised functions? I.e.
        >
        > template <typename T>
        > void f(T x)
        > {
        > ...
        > }
        >
        > template <>
        > inline void f<double>(doubl e x)
        > {
        > ...
        > }[/color]

        no, i write it like that:


        template <typename T>
        void f(T x)
        {
        ...
        }

        template <>
        void f(double x)
        {
        ...
        }

        Comment

        • Agent Mulder

          #5
          Re: Template linking problem (multiple definition)


          <Georg Teichtmeister>
          Now comes the problem: If I include our mathlibrary into two
          compilationunit s the linker gives a error which says:

          A.lo:in function Namespace1::ns_ globalfunction:
          A.lo:multiple definition of ns_globalfuncti on
          B.lo:first defined here

          And this comes for every namespaceglobal template function of the first
          namespace for every spezialition.
          <Georg Teichtmeister>

          You used a 'using namespace' somewhere and now
          there is ambiguity between the global names and the
          (same) names used in the namespace. You can help
          it by prepending :: if you want the global version.

          Namespace1::ns_ globalfunction( ); //namespaced version used
          ::ns_globalfunc tion(); //global version called

          -X


          Comment

          • Georg Teichtmeister

            #6
            Re: Template linking problem (multiple definition)



            Agent Mulder wrote:[color=blue]
            > <Georg Teichtmeister>
            > Now comes the problem: If I include our mathlibrary into two
            > compilationunit s the linker gives a error which says:
            >
            > A.lo:in function Namespace1::ns_ globalfunction:
            > A.lo:multiple definition of ns_globalfuncti on
            > B.lo:first defined here
            >
            > And this comes for every namespaceglobal template function of the first
            > namespace for every spezialition.
            > <Georg Teichtmeister>
            >
            > You used a 'using namespace' somewhere and now
            > there is ambiguity between the global names and the
            > (same) names used in the namespace. You can help
            > it by prepending :: if you want the global version.
            >
            > Namespace1::ns_ globalfunction( ); //namespaced version used
            > ::ns_globalfunc tion(); //global version called
            >
            > -X[/color]

            sorry, i made a mistake, actually the error output is as the following:

            A.lo:in function Namespace1::ns_ globalfunction:
            A.lo:multiple definition of Namespace1::ns_ globalfunction
            ^^^^^^^^^^^^
            B.lo:first defined here


            Comment

            • Agent Mulder

              #7
              Re: Template linking problem (multiple definition)


              "Georg Teichtmeister" <teichtmeister@ emt.tugraz.at> wrote in message
              news:3F583F68.5 070609@emt.tugr az.at...[color=blue]
              >
              >
              > Agent Mulder wrote:[color=green]
              > > <Georg Teichtmeister>
              > > Now comes the problem: If I include our mathlibrary into two
              > > compilationunit s the linker gives a error which says:
              > >
              > > A.lo:in function Namespace1::ns_ globalfunction:
              > > A.lo:multiple definition of ns_globalfuncti on
              > > B.lo:first defined here
              > >
              > > And this comes for every namespaceglobal template function of the first
              > > namespace for every spezialition.
              > > <Georg Teichtmeister>
              > >
              > > You used a 'using namespace' somewhere and now
              > > there is ambiguity between the global names and the
              > > (same) names used in the namespace. You can help
              > > it by prepending :: if you want the global version.
              > >
              > > Namespace1::ns_ globalfunction( ); //namespaced version used
              > > ::ns_globalfunc tion(); //global version called
              > >
              > > -X[/color]
              >
              > sorry, i made a mistake, actually the error output is as the following:
              >
              > A.lo:in function Namespace1::ns_ globalfunction:
              > A.lo:multiple definition of Namespace1::ns_ globalfunction
              > ^^^^^^^^^^^^
              > B.lo:first defined here
              >
              >[/color]


              I misread your post. Sorry :-(


              Comment

              • John Harrison

                #8
                Re: Template linking problem (multiple definition)


                "Georg Teichtmeister" <teichtmeister@ emt.tugraz.at> wrote in message
                news:3F583C5F.4 050102@emt.tugr az.at...[color=blue]
                >[color=green]
                > >
                > > Did you inline the fully specialised functions? I.e.
                > >
                > > template <typename T>
                > > void f(T x)
                > > {
                > > ...
                > > }
                > >
                > > template <>
                > > inline void f<double>(doubl e x)
                > > {
                > > ...
                > > }[/color]
                >
                > no, i write it like that:
                >
                >
                > template <typename T>
                > void f(T x)
                > {
                > ...
                > }
                >
                > template <>
                > void f(double x)
                > {
                > ...
                > }
                >[/color]

                Well that's why you get multiply defined errors. You need to inline the
                fully specialised versions of your template functions.

                john


                Comment

                • Georg Teichtmeister

                  #9
                  Re: Template linking problem (multiple definition)



                  Georg Teichtmeister wrote:[color=blue]
                  > Hello!
                  >
                  > We are developing a math - library for realtime applications and want to
                  > use some given mathlibraries as base(ipp, MTL, .. ). Our library is a
                  > wrapper for those and you should be able to select the underlying
                  > library by template parameter. Therefore we split up our library in two
                  > namespaces:
                  >
                  > The first one defines namespace-global functions which call the
                  > baselibrary functions. These are templatefunctio ns which some
                  > spezialitions for double and float.
                  >
                  > The second one defines classes, where the functions of the first
                  > namespace will be called.
                  >
                  > Now comes the problem: If I include our mathlibrary into two
                  > compilationunit s the linker gives a error which says:
                  >
                  > A.lo:in function Namespace1::ns_ globalfunction:
                  > A.lo:multiple definition of ns_globalfuncti on
                  > B.lo:first defined here
                  >
                  > And this comes for every namespaceglobal template function of the first
                  > namespace for every spezialition.
                  >
                  > regards,
                  > Georg
                  >[/color]

                  now I have a coding example which produces the error I have:

                  FILE: output.h
                  #include <iostream>

                  #ifndef TEST_H
                  #define TEST_H

                  namespace Test
                  {
                  template<typena me T>
                  void test(T output)
                  {
                  std::cout << output << std::endl;
                  }

                  template<> void test<int>(int output)
                  {
                  std::cout << "Int: " << output << std::endl;
                  }

                  }


                  #endif


                  FILE: class1.h

                  #include "output.h"

                  class class1
                  {
                  public:
                  class1();
                  };


                  FILE: class1.cpp

                  #include "class1.h"

                  class1::class1( )
                  {
                  Test::test<int> (13);
                  }

                  FILE: class2.h

                  #include "class1.h"

                  class class2 : public class1
                  {
                  public:
                  class2();
                  };


                  FILE: class2.cpp

                  #include "class2.h"

                  class2::class2( )
                  {
                  Test::test<unsi gned int>(34);
                  }

                  FILE: testTemplate.cp p

                  #include "class2.h"

                  int main(int argc, char** argv)
                  {
                  class1 c1;
                  class2 c2;


                  }

                  This is the error output from the linker:

                  class2.o: In function `void Test::test<int> (int)':
                  class2.o(.text+ 0x0): multiple definition of `void Test::test<int> (int)'
                  class1.o(.text+ 0x0): first defined here
                  testTemplate.o: In function `void Test::test<int> (int)':
                  testTemplate.o( .text+0x0): multiple definition of `void
                  Test::test<int> (int)'
                  class1.o(.text+ 0x0): first defined here
                  collect2: ld returned 1 exit status

                  Compiler: gcc-2.95.3

                  regards,
                  Georg

                  Comment

                  • Georg Teichtmeister

                    #10
                    Re: Template linking problem (multiple definition)

                    [color=blue]
                    >
                    >
                    > Well that's why you get multiply defined errors. You need to inline the
                    > fully specialised versions of your template functions.
                    >
                    > john
                    >[/color]
                    it works with my sample program, lets try it for the lib :)

                    thank you


                    Comment

                    • John Harrison

                      #11
                      Re: Template linking problem (multiple definition)

                      >[color=blue]
                      > now I have a coding example which produces the error I have:
                      >
                      > FILE: output.h
                      > #include <iostream>
                      >
                      > #ifndef TEST_H
                      > #define TEST_H
                      >
                      > namespace Test
                      > {
                      > template<typena me T>
                      > void test(T output)
                      > {
                      > std::cout << output << std::endl;
                      > }
                      >
                      > template<> void test<int>(int output)[/color]

                      Try this

                      template<> inline void test<int>(int output)
                      [color=blue]
                      > {
                      > std::cout << "Int: " << output << std::endl;
                      > }
                      >
                      > }[/color]

                      john


                      Comment

                      • Geoff Macartney

                        #12
                        Re: Template linking problem (multiple definition)


                        John

                        why is that? what difference logically does the inline make?
                        Is this just a feature of gcc? or is it to do with the inline making the
                        definition appear only in the compilation unit it's included in?
                        Is this just a feature for specialised functions or does it apply to
                        all template functions?

                        Geoff



                        John Harrison wrote:[color=blue]
                        > "Georg Teichtmeister" <teichtmeister@ emt.tugraz.at> wrote in message
                        > news:3F583C5F.4 050102@emt.tugr az.at...
                        >[color=green][color=darkred]
                        >>>Did you inline the fully specialised functions? I.e.
                        >>>
                        >>>template <typename T>
                        >>>void f(T x)
                        >>>{
                        >>> ...
                        >>>}
                        >>>
                        >>>template <>
                        >>>inline void f<double>(doubl e x)
                        >>>{
                        >>> ...
                        >>>}[/color]
                        >>[/color][/color]

                        Comment

                        Working...