Alcatel-Lucent nmake Product Builder
Potential viewpathing errors caused by Link-time Instantiation using SUN C++ Templates?
Using SUN C++ compilers which support
definitions-separate template organization may lead to viewpathing problems
when used with nmake. Viewpathing problems can result when
using definitions-separate template organization, because the compiler uses
link-time instantiation which does not use the needed ppcc
-I- preprocessing step during the link stage.
The problem can be solved by using
definitions-included
organization for templates.
The use of definitions-separate can also lead to recompilation issues with nmake. See the Implicit C++ Templates FAQ for details.
Details
Instantiation is the process by which a C++ compiler creates a usable function or object from a template. Two common methods of template instantiation are compile-time instantiation and link-time instantiation. Compile-time instantiation forces instantiations to occur when the reference to the template is being compiled, typically, compile-time instantiation occurs during the regular compilation step(CC -c). Link-time instantiation occurs during the link step (CC -o).
Generally, SUN C++ compilers use Compile-time instantiation. But under some conditions, some C++ compilers, such as SUN C++ 4.2 and SUN C++ 6, use link-time instantiation, which may lead to a problem when used in combination with nmake.
Here is an example using the SUN C++ 4.2 compiler definitions-separate feature and nmake.
$ pwd
/usr/d1
$ echo $VPATH
/usr/d1:/usr/d2
$ ls -l
drwxr-xr-x 2 bcdeng sablime 512 Aug 5 17:29 include
-rw-r--r-- 1 bcdeng sablime 84 Aug 19 11:09 makefile
-rw-r--r-- 1 bcdeng sablime 66 Aug 5 15:44 myprog.C
-rw-r--r-- 1 bcdeng sablime 15 Aug 1 13:44 test.h
$ ls -l include
-rw-r--r-- 1 bcdeng sablime 123 Jul 30 14:49 t.C
-rw-r--r-- 1 bcdeng sablime 16 Aug 1 13:47 test.h
$ ls -l ../d2
drwxr-xr-x 2 bcdeng sablime 512 Aug 12 12:27 include
-rw-r--r-- 1 bcdeng sablime 15 Jul 31 15:57 test.h
$ ls -l ../d2/include
-rw-r--r-- 1 bcdeng sablime 144 Aug 9 14:19 mytemplate.C
-rw-r--r-- 1 bcdeng sablime 167 Aug 12 12:28 mytemplate.h
-rw-r--r-- 1 bcdeng sablime 15 Aug 12 12:27 test.h
$ cat makefile
CC = /opt/SUNWspro/SC4.2/bin/CC
.SOURCE.h : include
myprog :: include/t.C myprog.C
$ cat myprog.C
#include <iostream.h>
int test();
int
main()
{
return test();
}
$ cat include/t.C
#include <iostream.h>
#include "mytemplate.h"
int
test()
{
mytemplate<5> myt;
cout << myt.get_x() << endl;
return 0;
}
$ cat ../d2/include/mytemplate.h
#ifndef MYTEMPLATE_H
#define MYTEMPLATE_H
template <int T> class mytemplate {
public:
mytemplate();
int get_x();
private:
int x;
};
#endif
$ cat ../d2/include/mytemplate.C
#include "test.h"
template <int T>
mytemplate<T>::mytemplate()
: x(T)
{
}
template <int T>
int
mytemplate<T>::get_x()
{
return x+MAXA;
}
$ cat test.h
#define MAXA 4
$ cat include/test.h
#define MAXA 5
$ cat ../d2/test.h
#define MAXA 6
$ cat ../d2/include/test.h
#define MAXA 7
$ nmake CCFLAGS=-ptv
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I/usr/d2/include -I- -c
include/t.C
template manager : Warning: No valid template database available. Creating defa
ult repository "Templates.DB".
"include/t.C", line 7: Information: Instantiating mytemplate<5>::mytemplate().
"include/t.C", line 8: Information: Instantiating mytemplate<5>::get_x().
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I- -c myprog.C
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I/usr/d2/include -I- -I/
usr/d2/include -o myprog t.o myprog.o
tdb_link : Information: Invoking: "/opt/SUNWspro/SC4.2/bin/CC -ptl -I/usr/d2/inc
lude -I/usr/d2/include -xildoff t.o myprog.o -o myprog Templates.DB/JisfyfDROpGW.
o Templates.DB/Vya1fE7heI8n.o ".
$ rm myprog
$ touch ../d2/include/mytemplate.C
$ nmake CCFLAGS=-ptv
make: warning: myprog has been replaced by an older version
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I/usr/d2/include -I- -I/
usr/d2/include -o myprog t.o myprog.o
tdb_link : Information: "Templates.DB/Vya1fE7heI8n.o" is out-of-date.
tdb_link : Information: "Templates.DB/JisfyfDROpGW.o" is out-of-date.
tdb_link : Information: Regenerating "t.o". Compile line: "/opt/SUNWspro/SC4.2/
bin/CC -I/usr/d2/include -c -ptv -ptm -c include/t.C".
"include/t.C", line 7: Information: Instantiating mytemplate<5>::mytemplate().
"include/t.C", line 8: Information: Instantiating mytemplate<5>::get_x().
tdb_link : Information: Invoking: "/opt/SUNWspro/SC4.2/bin/CC -ptl -I/usr/d2/inc
lude -I/usr/d2/include -xildoff t.o myprog.o -o myprog Templates.DB/JisfyfDROpGW.
o Templates.DB/Vya1fE7heI8n.o ".
The
definitions-separate
approach leads to link-time instantiation
which may lead the viewpathing errors because the link stage does not use
the needed ppcc -I- preprocess step.
To avoid the link-time instantiation, it is necessary to use the
definitions-included
approach to explicitly #include
the definitions file in the "mytemplate.h" file. To ensure
that you don't inadvertently leave out the definitions file #include and
possibly trigger implicit inclusion, you may wish to rename the
definitions file so it no longer has the same basename as the
corresponding .h file. This is shown below by #include'ing
the definitions file "mytemplateDEF.C" at the
bottom of the template declarations header file "mytemplate.h".
$ cat ../d2/include/mytemplate.h
#ifndef MYTEMPLATE_H
#define MYTEMPLATE_H
template <int T> class mytemplate {
public:
mytemplate();
int get_x();
private:
int x;
};
#include "mytemplateDEF.C"
#endif
$ nmake CCFLAGS=-ptv
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I. -I/usr/d2/include -I-
-c include/t.C
template manager : Warning: No valid template database available. Creating defa
ult repository "Templates.DB".
"include/t.C", line 7: Information: Instantiating mytemplate<5>::mytemplate().
"include/t.C", line 8: Information: Instantiating mytemplate<5>::get_x().
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I- -c myprog.C
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I. -I/usr/d2/include -I-
-I. -I/home/bcdeng/MR/990158bak/include -o myprog t.o myprog.o
tdb_link : Information: Invoking: "/opt/SUNWspro/SC4.2/bin/CC -ptl -I. -I/usr/d2
/include -I. -I/usr/d2/include -xildoff t.o myprog.o -o myprog Templates.DB/Jisf
yfDROpGW.o Templates.DB/Vya1fE7heI8n.o ".
$ rm myprog
$ touch ../d2/include/mytemplate.C
$ nmake CCFLAGS=-ptv
make: warning: myprog has been replaced by an older version
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I. -I/usr/d2/include -I-
-c include/t.C
"include/t.C", line 7: Information: Instantiating mytemplate<5>::mytemplate().
"include/t.C", line 8: Information: Instantiating mytemplate<5>::get_x().
+ ppcc -i -l /home/bcdeng/nmake/lu3.4/lib/cpp /opt/SUNWspro/SC4.2/bin/CC -ptv -I
-D/home/nmake/nmakelu3.2/lib/probe/C/pp/DF0B6BEB2binCC -I. -I/usr/d2/include -I-
-I. -I/usr/d2/include -o myprog t.o myprog.o
tdb_link : Information: Invoking: "/opt/SUNWspro/SC4.2/bin/CC -ptl -I. -I/usr/d2
/include -I. -I/usr/d2/include -xildoff t.o myprog.o -o myprog Templates.DB/Jisfy
fDROpGW.o Templates.DB/Vya1fE7heI8n.o ".







