home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!sunic!ugle.unit.no!sigyn.idt.unit.no!bjornmu
- From: bjornmu@idt.unit.no (Bj|rn P. Munch)
- Subject: Re: Problem with templates...
- Message-ID: <1992Sep4.154825.12584@ugle.unit.no>
- Sender: news@ugle.unit.no (NetNews Administrator)
- Organization: Div. of CS & Telematics, Norwegian Institute of Technology
- References: <Bu0xvC.6np@usenet.ucs.indiana.edu>
- Date: Fri, 4 Sep 92 15:48:25 GMT
- Lines: 108
-
- In article <Bu0xvC.6np@usenet.ucs.indiana.edu>, nsundare@silver.ucs.indiana.edu (Neelakantan Sundaresan) writes:
- |>
- |> I am trying to separate compile template definitions and use.
- |> For instance,
- |> file1.c has
- |> -----
- |>
- |> #include <stdio.h>
- |> #include <stdarg.h>
- |> #include "file1.h"
- |>
- |> template <class T> XT<T>::Xtest(T t)
- |> { printf("HEE\n");
- |> return 0;
- |> }
- |> template <class T> XT<T>::XT(){}
- |>
- |> file2.c has
- |> ------
- |>
- |> #include <stdio.h>
- |> #include <stdarg.h>
- |> #include "file1.h"
- |>
- |> main()
- |> {
- |> typedef XT<int> Y;
- |> Y x;
- |> x.Xtest(4);
- |> }
- |>
- |> file1.h has
- |> -------
- |>
- |> template <class T> class XT {
- |> public:
- |> XT();
- |> Xtest(T);
- |> };
- |>
- |>
- |> ------------ On compiling with g++ and linking I get the error
- |> undefined symbol:
- |> ___t2XT1Zi
- |> _Xtest__t2XT1Zii
- |>
- |> But if I have file1.c and file2.c in the same file, everything seems
- |> to be okay.
- |>
- |>
- |> Any ideas as to what is going on?
- |> thanks
- |> Neel.
-
- Yes, I've been struggling with a similar problem. I assume you want
- to have the member functions compiled in file.c only.
-
- Well, that does not happen. You "instantiate" the template in
- file2.c, but there the compiler doesn't have the definitions (bodies)
- of the member functions available. In file.c it's the opposite; you
- have the body templates, but don't create any instances of them.
-
- What you need to do, is to #include file1.c also into file2.c; then it
- should work.
-
- You'll have to include file.c also into all other files that use
- instances of XT. If you want to avoid this, and have the functions
- for XT<int> compiled only in one file (but use it in other files), try
- this:
-
- In file1.h, say:
-
- > #ifdef __GNUG__
- > #pragma interface
- > #endif
-
- I'm not 100% sure that is required, but what _is_ required, is to
- include this in the .c file where you want to create the functions:
-
- > #ifdef __GNUG__
- > #pragma implementation "file1.h"
- > #endif
- > #include "file1.c"
-
- This will make g++ spit out global functions, not local (static) ones.
- You can use this file to instantiate all the XT's you need, e.g. by:
-
- > typedef XT<int> intXT;
- > typedef XT<float> floatXT;
- > typedef XT<char *> strXT;
-
- , and you will have all the functions made for you. In other files,
- you can use these by including file1.h, but you don't have to include
- file1.c too.
-
- Hope this was comprehensible? My earlier post about the same topic
- was probably too cryptic, sorry. :-|
-
- Disclaimer: I haven't read this in docs, but found it out "the hard
- way". Works for me with g++ v 2.2.2 on a Sun4 running SunOS 4.1.2.
-
- - BugWriter
-
- ---
- Bj|rn P. Munch | Dept. of Comp. Science & Telematics,
- Bjoern.P.Munch@idt.unit.no | Norwegian Institute of Technology (NTH),
- PhD Student | N-7034 Trondheim, Norway
- Univeristy of Trondheim | Fingerable addr: bjornmu@multe.idt.unit.no
-