gcc and function templates

gcc and function templates

Post by Erik Max Franci » Fri, 15 Sep 2000 04:00:00




> g++ spits out the error:

> /tmp/cc3VGJui.o(.text+0x7b): undefined reference to `int
> is_true<int>(int &)'

> What is preventing this simple (yes, I have even tried this exact
> example...) test from succeeding?

The problem is one of instantiation.  Check out the gcc info page; there
are a few ways to do it.  The simplest (and most portable) way to handle
this situation is to have the template definitions (the _definitions_,
not just the declarations) in the header file (or at least in a file
ultimately included by the header file), not in a source file that you
compile and link in separately.

The reason is pretty simple, if you think about it:  Templates behave
much like macros, in that they're defined and then instantiated when
needed.  If the definition is in a separately-compiled source file, and
you use the template (i.e., need it instantiated) in another, then gcc
can't know what it needs to do because the relevant definitions aren't
handy when it's compiling the source.  To let gcc do its thing without
getting into explicit (and non-Standard) instantiations, ensure the
definition of the template is handy when it needs it by simply making
sure it's included.  The worst thing that can happen is that it results
in a little code bloat (but I think gcc is much better about stripping
out the duplicates now).

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ All the people in my neighborhood turn around and get mad and sing
\__/ Public Enemy
    Crank Dot Net / http://www.crank.net/
 Cranks, crackpots, kooks, & loons on the Net.

 
 
 

gcc and function templates

Post by Ross Vandegrif » Sat, 16 Sep 2000 14:07:02


Hello all,

        I'm sure this is a FAQ, and I'm somewhat embarassed that as a C
programmer of five years I can't solve it, but deja.com has shown me no
secrets so here goes...

        I'm trying to build an object file that contains a few useful
function templates, so I stick something like this in a file:

template <class T>
int is_true (T &foo)
{
  if (foo)
    return 1;
  else
    return 0;

Quote:}

and make an associated header file.  This file will compile fine into a
.o.  However, when I go to link it into a program that does something like

int foo = 0;
int bar = is_true (foo);

g++ spits out the error:

/tmp/cc3VGJui.o(.text+0x7b): undefined reference to `int is_true<int>(int
&)'

What is preventing this simple (yes, I have even tried this exact
example...) test from succeeding?  I have tested this behaviour on egcs
1.0.2 and 1.1.2 and read other posts on deja.com refering to gcc 2.8 as
well (but of course no solutions... gotta love deja).

Thanks in advance,
        Ross Vandegrift

 
 
 

gcc and function templates

Post by Anes Lihova » Sat, 16 Sep 2000 04:00:00



> Hello all,

>         I'm sure this is a FAQ, and I'm somewhat embarassed that as a C
> programmer of five years I can't solve it, but deja.com has shown me no
> secrets so here goes...

>         I'm trying to build an object file that contains a few useful
> function templates, so I stick something like this in a file:

> template <class T>
> int is_true (T &foo)
> {
>   if (foo)
>     return 1;
>   else
>     return 0;
> }

> and make an associated header file.  This file will compile fine into a
> .o.  However, when I go to link it into a program that does something like

> int foo = 0;
> int bar = is_true (foo);

> g++ spits out the error:

> /tmp/cc3VGJui.o(.text+0x7b): undefined reference to `int is_true<int>(int
> &)'

> What is preventing this simple (yes, I have even tried this exact
> example...) test from succeeding?  I have tested this behaviour on egcs
> 1.0.2 and 1.1.2 and read other posts on deja.com refering to gcc 2.8 as
> well (but of course no solutions... gotta love deja).

> Thanks in advance,
>         Ross Vandegrift

Try adding a -lsdtc++ to the linkage line, e.g.: gcc foo.o main.o -o
TEST -lstdc++

Regards
Anes

 
 
 

1. gcc 2.96 vs. gcc 3.2: namespace, template incompatability

The old code I once wrote in redhat 7.3(gcc 2.96) are no longer
compiled without errors in gcc 3.2 after I upgrade to redhat 8.0.

In gcc 3.2, the compiler always complain that 'cin', 'endl' etc. are
undefined unless I add the line "using namespace std".

In addition, there is also some problem in template, for example, the
code segment below is compiled OK in gcc 2.96, but there are a lot of
errors given by gcc 3.2. And there is still a error after I add "using
namespace std"

$ g++ -o max_string max_string.C
max_string.C: In function `int main()':
max_string.C:37: cannot convert
`__gnu_cxx::__normal_iterator<std::string*,
   std::vector<std::string, std::allocator<std::string> > >' to
`std::string*'
   in initialization
$

// code segment in "C++ Primer" chapter 20 iostream library
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>

using namespace std;

bool length_less( string s1, string s2 )
    { return s1.size() < s2.size(); }

template <class InputIterator>
void filter_string( InputIterator first, InputIterator last,
                    string filt_elems = string( "\",?." ))
{
    for (; first != last; first++) {
        string::size_type pos = 0;
        while ( (pos = (*first).find_first_of(filt_elems, pos))
                != string::npos )
            (*first).erase( pos, 1 );
    }

int main()
{
    istream_iterator<string> input( cin ), eos;

    vector<string> text;
    // copy is a generic algorithm
    // copy from standard input to text
    copy( input, eos, back_inserter( text ));
    string filt_elems( "\",.?;:" );
    filter_string( text.begin(), text.end(), filt_elems );

    int cnt = text.size();
    // max_element is a generic algorithm
    string *max = max_element( text.begin(), text.end(), length_less
);
    int len = max->size();

    cout << "The number of words read is " << cnt << endl;
    cout << "The longest word has a length of " << len << endl;
    cout << "The longest word is " << *max << endl;

2. To whom it may concern: I finaly got kppp working!!! (and I need help with Netscape!)

3. Inserting a function header template

4. 'ioscan' problam

5. SUN CC 5.0: shared libraries refuse to close - missing inline template functions!

6. Incomplete Package Installations

7. Templates, Virtual functions and Forte 6 compiler

8. NIS problems, AIX 3.1.5 and SunOS 4.1.3

9. CC 5.0: shared libraries refuse to close - missing inline template functions!

10. Template type as function return type

11. using template template parameters

12. gcc 2.7.2 , Templates & Exceptions

13. gcc patch file for g++ templates