www.digitalmars.com         C & C++   DMDScript  

c++.windows.32-bits - Export and Importing template classes in Dll

reply "Ingvaldur Sigurjonsson" <ingi ementor.se> writes:
Hi

I've built a dll that exports a bunch of template classes. A dll is built
and a import library is generated.

sc
tmpldll.cpp -Ae -Ar -mn -C -WD -Pz -S -5 -a8 -c -gf -DTMPLDLL_EXPORTS -DWIN3
2_LEAN_AND_MEAN -otmpldll.obj
link /CO /NOI /DO /DE /NOPACKF /XN /NT /ENTRY:_DllMainCRTStartup
/BAS:268435456 /A:512 /IMPL:.\$SCW$.LIB  tmpldll.LNK
ren .\$SCW$.DLL tmpldll.DLL
ren .\$SCW$.LIB tmpldll.LIB
.\tmpldll.DLL built

When linking with the import library the linker complains as follows when I
try to instantiate a template class. See enclosed file tmpldll.hpp.

sc
usedlltest.cpp -mn -C -WA -S -ND -3 -a8 -c -gf -D_MT -D_DEBUG -I\utv\vwcl -o
.\usedlltest_objs\usedlltest.obj
<... some other files in my testapp project ... >
link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:WinMainCRTStartup /BAS:4194304
/A:512 /RC   :.\usedlltest_objs\usedlltest.RES  usedlltest.LNK
Error: D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
(1464936): Symbol Undefined ??1?$TmplDerived H  QAE XZ (syscall
TmplDerived<int >::~TmplDerived<int >(void ))
Error: D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
(1464936): Symbol Undefined ??0?$TmplDerived H  QAE XZ (syscall
TmplDerived<int >::TmplDerived<int >(void ))
Lines Processed: 1133565  Errors: 2  Warnings: 0
Build failed

I enclose a sample tmpldll.hpp file containing  1 Base class, 1 TmplDerived
Template class and 1 SimpleDerived class.
I also enclose the usedlltest.cpp containing the LibMain ! Just create a
Windows DLL project with these files and try it for you self.

When instantiating the classes in my testapp, I get link error when I try to
instantiate TmplDerived class, but it works fine when I instantiate the
SimpleDerived class.

                TmplDerived<int> d;    <-- This gives me link-time error
                // d.BaseMethod();

                SimpleDerived s;        <-- Just using these 2 lines work
ok.
                s.BaseMethod();

Can anyone please advise ?

I can and I have built this example in Micrsoft Visual C++ 6.0.

I got my Digital Mars C++ CD today :-)

Regards
- Ingvaldur



begin 666 tmpldll.hpp
M(VEF("%D969I;F5D*%1-4$Q$3$Q?2%!0*0T*(V1E9FEN92!435!,1$Q,7TA0
M4 T*#0HC:69D968 5$U03$1,3%]%6%!/4E13#0HC9&5F:6YE(%1-4$Q?1$5#
M3%-014, 7U]D96-L<W!E8RAD;&QE>'!O<G0I#0HC96QS90T*(V1E9FEN92!4
M35!,7T1%0TQ34$5#(%]?9&5C;'-P96,H9&QL:6UP;W)T*0T*(V5N9&EF#0H-
M" T*8VQA<W, 5$U03%]$14-,4U!%0R!"87-E#0I[#0H ("  <'5B;&EC. T*
M("  ("  ("!V:7)T=6%L(&)O;VP 0F%S94UE=&AO9" I(#T ,#L-"GT[#0H-
M"G1E;7!L871E(#P 8VQA<W, ='EP95]T(#X-"F-L87-S(%1-4$Q?1$5#3%-0
M14, 5&UP;$1E<FEV960 .B!P=6)L:6, 0F%S90T*>PT*("  ('!U8FQI8SH-
M"B  ("  ("  5&UP;$1E<FEV960H*3L-" T*("  ("  ("!^5&UP;$1E<FEV
M960H*3L-" T*("  ("  ("!B;V]L($)A<V5-971H;V0H*3L-" T*("  ("  
M(" O+R!T>7!E7W0J(&]P97)A=&]R+3XH*2![(')E='5R;B F9%]D871A.R!]
M#0H-"B  ("!P<FEV871E. T*("  ("  ("!T>7!E7W0 9%]D871A.PT*?3L-
M" T*=&5M<&QA=&4 /"!C;&%S<R!T>7!E7W0 / T*:6YL:6YE(%1M<&Q$97)I
M=F5D/'1Y<&5?=#XZ.E1M<&Q$97)I=F5D*"D-"GL-"B  ("!M96US970H("9D
M7V1A=&$L(#!X,"P <VEZ96]F*'1Y<&5?="D *3L-"B  (" Z.D]U='!U=$1E
M8G5G4W1R:6YG*" B8RUT;W( 1&5R:79E9%QN(B I.PT*?0T*#0IT96UP;&%T
M92 \(&-L87-S('1Y<&5?=" ^#0II;FQI;F4 5&UP;$1E<FEV960\='EP95]T
M/CHZ?E1M<&Q$97)I=F5D*"D-"GL-"B  (" Z.D]U='!U=$1E8G5G4W1R:6YG
M*" B9"UT;W( 1&5R:79E9%QN(B I.PT*?0T*#0IT96UP;&%T92 \(&-L87-S
M('1Y<&5?=" ^#0II;FQI;F4 8F]O;"!4;7!L1&5R:79E9#QT>7!E7W0^.CI"
M87-E365T:&]D*"D-"GL-"B  (" Z.D]U='!U=$1E8G5G4W1R:6YG*" B1&5R
M:79E9"!"87-E365T:&]D($EM<&QE;65N=&5D7&XB("D[#0H ("  <F5T=7)N
M('1R=64[#0I]#0H-"F-L87-S(%1-4$Q?1$5#3%-014, 4VEM<&QE1&5R:79E
M9" Z('!U8FQI8R!"87-E#0I[#0H ("  <'5B;&EC. T*("  ("  ("!3:6UP
M;&5$97)I=F5D*"D-"B  ("  ("  >PT*("  ("  ("  ("  .CI/=71P=71$
M96)U9U-T<FEN9R  (F,M=&]R(%-I;7!L941E<FEV961<;B( *3L-"B  ("  
M("  ?0T*#0H-"B  ("  ("  ?E-I;7!L941E<FEV960H*0T*("  ("  ("![
M#0H ("  ("  ("  (" Z.D]U='!U=$1E8G5G4W1R:6YG*" B9"UT;W( 4VEM
M<&QE1&5R:79E9%QN(B I.PT*("  ("  ("!]#0H-" T*("  ("  ("!B;V]L
M($)A<V5-971H;V0H*0T*("  ("  ("![#0H ("  ("  ("  (" Z.D]U='!U
M=$1E8G5G4W1R:6YG*" B4VEM<&QE1&5R:79E9"!"87-E365T:&]D($EM<&QE
M;65N=&5D7&XB("D[#0H ("  ("  ("  ("!R971U<FX =')U93L-"B  ("  
F("  ?0T*#0I].PT*#0HC96YD:68 +RH 5$U03$1,3%](4%  *B\`
`
end

begin 666 tmpldll.cpp
M+R\ 5&UP;$1L;"YC<'  .B!$969I;F5S('1H92!E;G1R>2!P;VEN="!F;W( 
M=&AE($1,3"!A<'!L:6-A=&EO;BX-"B\O#0H-"B-I;F-L=61E(#QW:6YD;W=S
M+F ^#0HC:6YC;'5D92 B=&UP;&1L;"YH<' B#0H-" T*0D]/3"!!4$E%3E12
M62!$;&Q-86EN*"!(04Y$3$4 :$UO9'5L92P-"B  ("  ("  ("  ("  ("  
M("  ("  1%=/4D0 ('5L7W)E87-O;E]F;W)?8V%L;"P-"B  ("  ("  ("  
M("  ("  ("  ("  3%!63TE$(&QP4F5S97)V960-" D)"0D)("D-"GL-"B  
M("!S=VET8V  *'5L7W)E87-O;E]F;W)?8V%L;"D-" E[#0H)"6-A<V4 1$Q,
M7U!23T-%4U-?051404-(. T*"0EC87-E($1,3%]42%)%041?051404-(. T*
M"0EC87-E($1,3%]42%)%041?1$5404-(. T*"0EC87-E($1,3%]04D]#15-3
M7T1%5$%#2#H-" D)"6)R96%K.PT*("  ('T-"B  ("!R971U<FX 5%)513L-
&"GT-" T*
`
end
Feb 19 2002
parent reply "Walter" <walter digitalmars.com> writes:
Try putting it into a .exe and see if that works. Also, do you really need
all those options <g>? When diagnosing such problems, it helps a lot to
eliminate as many switches and complications as possible. Problems can be
obfuscated by layers of macros, directories, include files, etc.

"Ingvaldur Sigurjonsson" <ingi ementor.se> wrote in message
news:a4usbo$8s6$1 digitaldaemon.com...
 Hi

 I've built a dll that exports a bunch of template classes. A dll is built
 and a import library is generated.

 sc
tmpldll.cpp -Ae -Ar -mn -C -WD -Pz -S -5 -a8 -c -gf -DTMPLDLL_EXPORTS -DWIN3
 2_LEAN_AND_MEAN -otmpldll.obj
 link /CO /NOI /DO /DE /NOPACKF /XN /NT /ENTRY:_DllMainCRTStartup
 /BAS:268435456 /A:512 /IMPL:.\$SCW$.LIB  tmpldll.LNK
 ren .\$SCW$.DLL tmpldll.DLL
 ren .\$SCW$.LIB tmpldll.LIB
 .\tmpldll.DLL built

 When linking with the import library the linker complains as follows when
I
 try to instantiate a template class. See enclosed file tmpldll.hpp.

 sc
usedlltest.cpp -mn -C -WA -S -ND -3 -a8 -c -gf -D_MT -D_DEBUG -I\utv\vwcl -o
 .\usedlltest_objs\usedlltest.obj
 <... some other files in my testapp project ... >
 link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:WinMainCRTStartup /BAS:4194304
 /A:512 /RC   :.\usedlltest_objs\usedlltest.RES  usedlltest.LNK
 Error: D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??1?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::~TmplDerived<int >(void ))
 Error: D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??0?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::TmplDerived<int >(void ))
 Lines Processed: 1133565  Errors: 2  Warnings: 0
 Build failed

 I enclose a sample tmpldll.hpp file containing  1 Base class, 1
TmplDerived
 Template class and 1 SimpleDerived class.
 I also enclose the usedlltest.cpp containing the LibMain ! Just create a
 Windows DLL project with these files and try it for you self.

 When instantiating the classes in my testapp, I get link error when I try
to
 instantiate TmplDerived class, but it works fine when I instantiate the
 SimpleDerived class.

                 TmplDerived<int> d;    <-- This gives me link-time error
                 // d.BaseMethod();

                 SimpleDerived s;        <-- Just using these 2 lines work
 ok.
                 s.BaseMethod();

 Can anyone please advise ?

 I can and I have built this example in Micrsoft Visual C++ 6.0.

 I got my Digital Mars C++ CD today :-)

 Regards
 - Ingvaldur
Feb 19 2002
parent reply "Ingvaldur Sigurjonsson" <ingi ementor.se> writes:
Hi again,

It work's as expected when I include the tmpldll.hpp directly into the app.
You can make it work in your own testapplication if you do one of the
following

i) I just have to define the symbol TMPLDLL_EXPORTS for the test
application. That defines the __declspec(dllexport) for the classes.
ii) use the updated tmpldll.hpp which I enclose and define the symbol
JUST_APP for the test application. By doing that you'll have neither
__declspec(dllimport) or __declspec(dllexport) for the classes.

This the output for when I build my test application (I've built both a test
Windows app and a Console based app).

options being used are default when project is created in the IDDE.

sc
contest.cpp -mn -C -WA -S -3 -a8 -c -gf -DJUST_APP -D_CONSOLE=1 -ocontest.ob
j
link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:mainCRTStartup /BAS:4194304 /A:512
 contest.LNK
ren .\$SCW$.EXE contest.EXE
.\contest.EXE built
Lines Processed: 124544  Errors: 0  Warnings: 0
Successful build

I tried to dump the exports from the .exe by entering dumpexe
contest.exe -e, but the dumpexe exited with error:
... lot of output ...
    ---- seg 1
00: 6961 (26977)     Seg, segment index for this table
02: 486e (18542)     cPair, number of source line pairs to follow
Error: file contest.exe is too short (34796/4)

When building the contest.cpp from the command line with
sc -mn -DJUST_APP contest.cpp
contest.exe gets built and dumpexe contest.exe -e completes successfully.

Should I try to build the .dll with fewer switches maybe ?

Regards
- Ingi

"Walter" <walter digitalmars.com> wrote in message
news:a4vie5$jgl$2 digitaldaemon.com...
 Try putting it into a .exe and see if that works. Also, do you really need
 all those options <g>? When diagnosing such problems, it helps a lot to
 eliminate as many switches and complications as possible. Problems can be
 obfuscated by layers of macros, directories, include files, etc.

 "Ingvaldur Sigurjonsson" <ingi ementor.se> wrote in message
 news:a4usbo$8s6$1 digitaldaemon.com...
 Hi

 I've built a dll that exports a bunch of template classes. A dll is
built
 and a import library is generated.

 sc
tmpldll.cpp -Ae -Ar -mn -C -WD -Pz -S -5 -a8 -c -gf -DTMPLDLL_EXPORTS -DWIN3
 2_LEAN_AND_MEAN -otmpldll.obj
 link /CO /NOI /DO /DE /NOPACKF /XN /NT /ENTRY:_DllMainCRTStartup
 /BAS:268435456 /A:512 /IMPL:.\$SCW$.LIB  tmpldll.LNK
 ren .\$SCW$.DLL tmpldll.DLL
 ren .\$SCW$.LIB tmpldll.LIB
 .\tmpldll.DLL built

 When linking with the import library the linker complains as follows
when
 I
 try to instantiate a template class. See enclosed file tmpldll.hpp.

 sc
usedlltest.cpp -mn -C -WA -S -ND -3 -a8 -c -gf -D_MT -D_DEBUG -I\utv\vwcl -o
 .\usedlltest_objs\usedlltest.obj
 <... some other files in my testapp project ... >
 link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:WinMainCRTStartup /BAS:4194304
 /A:512 /RC   :.\usedlltest_objs\usedlltest.RES  usedlltest.LNK
 Error:
D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??1?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::~TmplDerived<int >(void ))
 Error:
D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??0?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::TmplDerived<int >(void ))
 Lines Processed: 1133565  Errors: 2  Warnings: 0
 Build failed

 I enclose a sample tmpldll.hpp file containing  1 Base class, 1
TmplDerived
 Template class and 1 SimpleDerived class.
 I also enclose the usedlltest.cpp containing the LibMain ! Just create a
 Windows DLL project with these files and try it for you self.

 When instantiating the classes in my testapp, I get link error when I
try
 to
 instantiate TmplDerived class, but it works fine when I instantiate the
 SimpleDerived class.

                 TmplDerived<int> d;    <-- This gives me link-time error
                 // d.BaseMethod();

                 SimpleDerived s;        <-- Just using these 2 lines
work
 ok.
                 s.BaseMethod();

 Can anyone please advise ?

 I can and I have built this example in Micrsoft Visual C++ 6.0.

 I got my Digital Mars C++ CD today :-)

 Regards
 - Ingvaldur
begin 666 tmpldll.hpp M(VEF("%D969I;F5D*%1-4$Q$3$Q?2%!0*0T*(V1E9FEN92!435!,1$Q,7TA0 M4 T*#0HC:68 9&5F:6YE9"A*55-47T%04"D-"B-D969I;F4 5$U03%]$14-, M4U!%0PT*(V5L<V4-"B-I9F1E9B!435!,1$Q,7T584$]25%,-"B-D969I;F4 M5$U03%]$14-,4U!%0R!?7V1E8VQS<&5C*&1L;&5X<&]R="D-"B-E;'-E#0HC M9&5F:6YE(%1-4$Q?1$5#3%-014, 7U]D96-L<W!E8RAD;&QI;7!O<G0I#0HC M96YD:68-"B-E;F1I9 T*#0IC;&%S<R!435!,7T1%0TQ34$5#($)A<V4-"GL- M"B ("!P=6)L:6,Z#0H (" (" ('9I<G1U86P 8F]O;"!"87-E365T:&]D M*"D /2 P.PT*?3L-" T*=&5M<&QA=&4 /"!C;&%S<R!T>7!E7W0 / T*8VQA M<W, 5$U03%]$14-,4U!%0R!4;7!L1&5R:79E9" Z('!U8FQI8R!"87-E#0I[ M#0H (" <'5B;&EC. T*(" (" ("!4;7!L1&5R:79E9" I#0H (" (" M('L-"B (" (" (" (&UE;7-E=" )F1?9&%T82P ,' P+"!S:7IE;V8H M='EP95]T*2 I.PT*(" (" (" (" .CI/=71P=71$96)U9U-T<FEN9R M(F,M=&]R(%1M<&Q$97)I=F5D7&XB("D[#0H (" (" ('T-" T*(" (" M("!^5&UP;$1E<FEV960H*0T*(" (" ("![#0H (" (" (" (" Z.D]U M='!U=$1E8G5G4W1R:6YG*" B9"UT;W( 5&UP;$1E<FEV961<;B( *3L-"B M(" (" ?0T*#0H (" (" (&)O;VP 0F%S94UE=&AO9" I#0H (" (" M('L-"B (" (" (" (#HZ3W5T<'5T1&5B=6=3=')I;F<H(")4;7!L1&5R M:79E9"!"87-E365T:&]D($EM<&QE;65N=&5D7&XB("D[#0H (" (" (" M("!R971U<FX =')U93L-"B (" (" ?0T*#0H (" (" ("\O('1Y<&5? M="H ;W!E<F%T;W(M/B I('L <F5T=7)N("9D7V1A=&$[('T-" T*(" ('!R M:79A=&4Z#0H (" (" ('1Y<&5?="!D7V1A=&$[#0I].PT*#0IC;&%S<R!4 M35!,7T1%0TQ34$5#(%-I;7!L941E<FEV960 .B!P=6)L:6, 0F%S90T*>PT* M(" ('!U8FQI8SH-"B (" (" 4VEM<&QE1&5R:79E9" I#0H (" (" M('L-"B (" (" (" (#HZ3W5T<'5T1&5B=6=3=')I;F<H(")C+71O<B!3 M:6UP;&5$97)I=F5D7&XB("D[#0H (" (" ('T-" T*#0H (" (" ('Y3 M:6UP;&5$97)I=F5D*"D-"B (" (" >PT*(" (" (" (" .CI/=71P M=71$96)U9U-T<FEN9R (F0M=&]R(%-I;7!L941E<FEV961<;B( *3L-"B M(" (" ?0T*#0H-"B (" (" 8F]O;"!"87-E365T:&]D*"D-"B (" M(" >PT*(" (" (" (" .CI/=71P=71$96)U9U-T<FEN9R (E-I;7!L M941E<FEV960 0F%S94UE=&AO9"!);7!L96UE;G1E9%QN(B I.PT*(" (" M(" (" <F5T=7)N('1R=64[#0H (" (" ('T-" T*?3L-" T*(V5N9&EF 2("\J(%1-4$Q$3$Q?2%!0("HO ` end begin 666 contest.cpp M(VEN8VQU9&4 /'=I;F1O=W,N:#X-"B-I;F-L=61E(")T;7!L9&QL+FAP<"(- M" T*:6YT(&UA:6XH=F]I9"D-"GL-"B ("!4;7!L1&5R:79E9#QI;G0^(&0[ M#0H (" 9"Y"87-E365T:&]D*"D[#0H-"B ("!3:6UP;&5$97)I=F5D(',[ I#0H (" <RY"87-E365T:&]D*"D[#0H-"B ("!R971U<FX ,#L-"GT` ` end
Feb 20 2002
parent reply Jan Knepper <jan smartsoft.cc> writes:
AFAIK:

Build the .DLL with -WD
Build the .EXE that uses the .DLL with -WA
Make sure you have a define like:

class header file:

#ifdef _WINDLL                // -WD throws this!
#   define CLASS_MOD    __declspec(dllexport)
#else
#   define CLASS_MOD    __declspec(dllimport)
#endif


class CLASS_MOD ClassName
{
};



This makes ClassName: __declspec(dllexport) ClassName when compiling the .DLL
This makes ClassName: __declspec(dllimport) ClassName when compiling the .EXE

HTH

Jan



Ingvaldur Sigurjonsson wrote:

 Hi again,

 It work's as expected when I include the tmpldll.hpp directly into the app.
 You can make it work in your own testapplication if you do one of the
 following

 i) I just have to define the symbol TMPLDLL_EXPORTS for the test
 application. That defines the __declspec(dllexport) for the classes.
 ii) use the updated tmpldll.hpp which I enclose and define the symbol
 JUST_APP for the test application. By doing that you'll have neither
 __declspec(dllimport) or __declspec(dllexport) for the classes.

 This the output for when I build my test application (I've built both a test
 Windows app and a Console based app).

 options being used are default when project is created in the IDDE.

 sc
 contest.cpp -mn -C -WA -S -3 -a8 -c -gf -DJUST_APP -D_CONSOLE=1 -ocontest.ob
 j
 link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:mainCRTStartup /BAS:4194304 /A:512
  contest.LNK
 ren .\$SCW$.EXE contest.EXE
 .\contest.EXE built
 Lines Processed: 124544  Errors: 0  Warnings: 0
 Successful build

 I tried to dump the exports from the .exe by entering dumpexe
 contest.exe -e, but the dumpexe exited with error:
 ... lot of output ...
     ---- seg 1
 00: 6961 (26977)     Seg, segment index for this table
 02: 486e (18542)     cPair, number of source line pairs to follow
 Error: file contest.exe is too short (34796/4)

 When building the contest.cpp from the command line with
 sc -mn -DJUST_APP contest.cpp
 contest.exe gets built and dumpexe contest.exe -e completes successfully.

 Should I try to build the .dll with fewer switches maybe ?

 Regards
 - Ingi

 "Walter" <walter digitalmars.com> wrote in message
 news:a4vie5$jgl$2 digitaldaemon.com...
 Try putting it into a .exe and see if that works. Also, do you really need
 all those options <g>? When diagnosing such problems, it helps a lot to
 eliminate as many switches and complications as possible. Problems can be
 obfuscated by layers of macros, directories, include files, etc.

 "Ingvaldur Sigurjonsson" <ingi ementor.se> wrote in message
 news:a4usbo$8s6$1 digitaldaemon.com...
 Hi

 I've built a dll that exports a bunch of template classes. A dll is
built
 and a import library is generated.

 sc
tmpldll.cpp -Ae -Ar -mn -C -WD -Pz -S -5 -a8 -c -gf -DTMPLDLL_EXPORTS -DWIN3
 2_LEAN_AND_MEAN -otmpldll.obj
 link /CO /NOI /DO /DE /NOPACKF /XN /NT /ENTRY:_DllMainCRTStartup
 /BAS:268435456 /A:512 /IMPL:.\$SCW$.LIB  tmpldll.LNK
 ren .\$SCW$.DLL tmpldll.DLL
 ren .\$SCW$.LIB tmpldll.LIB
 .\tmpldll.DLL built

 When linking with the import library the linker complains as follows
when
 I
 try to instantiate a template class. See enclosed file tmpldll.hpp.

 sc
usedlltest.cpp -mn -C -WA -S -ND -3 -a8 -c -gf -D_MT -D_DEBUG -I\utv\vwcl -o
 .\usedlltest_objs\usedlltest.obj
 <... some other files in my testapp project ... >
 link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:WinMainCRTStartup /BAS:4194304
 /A:512 /RC   :.\usedlltest_objs\usedlltest.RES  usedlltest.LNK
 Error:
D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??1?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::~TmplDerived<int >(void ))
 Error:
D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??0?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::TmplDerived<int >(void ))
 Lines Processed: 1133565  Errors: 2  Warnings: 0
 Build failed

 I enclose a sample tmpldll.hpp file containing  1 Base class, 1
TmplDerived
 Template class and 1 SimpleDerived class.
 I also enclose the usedlltest.cpp containing the LibMain ! Just create a
 Windows DLL project with these files and try it for you self.

 When instantiating the classes in my testapp, I get link error when I
try
 to
 instantiate TmplDerived class, but it works fine when I instantiate the
 SimpleDerived class.

                 TmplDerived<int> d;    <-- This gives me link-time error
                 // d.BaseMethod();

                 SimpleDerived s;        <-- Just using these 2 lines
work
 ok.
                 s.BaseMethod();

 Can anyone please advise ?

 I can and I have built this example in Micrsoft Visual C++ 6.0.

 I got my Digital Mars C++ CD today :-)

 Regards
 - Ingvaldur
Name: tmpldll.hpp tmpldll.hpp Type: HPP file (application/x-unknown-content-type-hppfile) Encoding: x-uuencode Name: contest.cpp contest.cpp Type: MEW32 File (application/x-unknown-content-type-cppfile) Encoding: x-uuencode
Feb 20 2002
parent reply "Ingvaldur Sigurjonsson" <ingi ementor.se> writes:
Hi

I have been doing what you've explained below and it works for classes that
are NOT templates, but it doesn't work for template classes

"Jan Knepper" <jan smartsoft.cc> wrote in message
news:3C73AAA9.5E87B42D smartsoft.cc...
 AFAIK:

 Build the .DLL with -WD
 Build the .EXE that uses the .DLL with -WA
 Make sure you have a define like:

 class header file:

 #ifdef _WINDLL                // -WD throws this!
 #   define CLASS_MOD    __declspec(dllexport)
 #else
 #   define CLASS_MOD    __declspec(dllimport)
 #endif


 class CLASS_MOD ClassName
 {
 };
This works ok. But if I add a class: class CLASS_MOD TmplClassName { }; it doesn't seem to get exported by the generated dll while the simple class does. Please see the latest tmpldef.hpp in this thread for reference/example. The workaround for this solution was to include the source of the dll (except the LibMain) into my application and that works ok.
 This makes ClassName: __declspec(dllexport) ClassName when compiling the
.DLL
 This makes ClassName: __declspec(dllimport) ClassName when compiling the
.EXE
 HTH

 Jan



 Ingvaldur Sigurjonsson wrote:

 Hi again,

 It work's as expected when I include the tmpldll.hpp directly into the
app.
 You can make it work in your own testapplication if you do one of the
 following

 i) I just have to define the symbol TMPLDLL_EXPORTS for the test
 application. That defines the __declspec(dllexport) for the classes.
 ii) use the updated tmpldll.hpp which I enclose and define the symbol
 JUST_APP for the test application. By doing that you'll have neither
 __declspec(dllimport) or __declspec(dllexport) for the classes.

 This the output for when I build my test application (I've built both a
test
 Windows app and a Console based app).

 options being used are default when project is created in the IDDE.

 sc
contest.cpp -mn -C -WA -S -3 -a8 -c -gf -DJUST_APP -D_CONSOLE=1 -ocontest.ob
 j
 link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:mainCRTStartup /BAS:4194304
/A:512
  contest.LNK
 ren .\$SCW$.EXE contest.EXE
 .\contest.EXE built
 Lines Processed: 124544  Errors: 0  Warnings: 0
 Successful build

 I tried to dump the exports from the .exe by entering dumpexe
 contest.exe -e, but the dumpexe exited with error:
 ... lot of output ...
     ---- seg 1
 00: 6961 (26977)     Seg, segment index for this table
 02: 486e (18542)     cPair, number of source line pairs to follow
 Error: file contest.exe is too short (34796/4)

 When building the contest.cpp from the command line with
 sc -mn -DJUST_APP contest.cpp
 contest.exe gets built and dumpexe contest.exe -e completes
successfully.
 Should I try to build the .dll with fewer switches maybe ?

 Regards
 - Ingi

 "Walter" <walter digitalmars.com> wrote in message
 news:a4vie5$jgl$2 digitaldaemon.com...
 Try putting it into a .exe and see if that works. Also, do you really
need
 all those options <g>? When diagnosing such problems, it helps a lot
to
 eliminate as many switches and complications as possible. Problems can
be
 obfuscated by layers of macros, directories, include files, etc.

 "Ingvaldur Sigurjonsson" <ingi ementor.se> wrote in message
 news:a4usbo$8s6$1 digitaldaemon.com...
 Hi

 I've built a dll that exports a bunch of template classes. A dll is
built
 and a import library is generated.

 sc
tmpldll.cpp -Ae -Ar -mn -C -WD -Pz -S -5 -a8 -c -gf -DTMPLDLL_EXPORTS -DWIN3
 2_LEAN_AND_MEAN -otmpldll.obj
 link /CO /NOI /DO /DE /NOPACKF /XN /NT /ENTRY:_DllMainCRTStartup
 /BAS:268435456 /A:512 /IMPL:.\$SCW$.LIB  tmpldll.LNK
 ren .\$SCW$.DLL tmpldll.DLL
 ren .\$SCW$.LIB tmpldll.LIB
 .\tmpldll.DLL built

 When linking with the import library the linker complains as follows
when
 I
 try to instantiate a template class. See enclosed file tmpldll.hpp.

 sc
usedlltest.cpp -mn -C -WA -S -ND -3 -a8 -c -gf -D_MT -D_DEBUG -I\utv\vwcl -o
 .\usedlltest_objs\usedlltest.obj
 <... some other files in my testapp project ... >
 link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:WinMainCRTStartup
/BAS:4194304
 /A:512 /RC   :.\usedlltest_objs\usedlltest.RES  usedlltest.LNK
 Error:
D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??1?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::~TmplDerived<int >(void ))
 Error:
D:\utv\dm\test\wintest\.\usedlltest_objs\usedlltest.OBJ(usedlltest)
 (1464936): Symbol Undefined ??0?$TmplDerived H  QAE XZ (syscall
 TmplDerived<int >::TmplDerived<int >(void ))
 Lines Processed: 1133565  Errors: 2  Warnings: 0
 Build failed

 I enclose a sample tmpldll.hpp file containing  1 Base class, 1
TmplDerived
 Template class and 1 SimpleDerived class.
 I also enclose the usedlltest.cpp containing the LibMain ! Just
create a
 Windows DLL project with these files and try it for you self.

 When instantiating the classes in my testapp, I get link error when
I
 try
 to
 instantiate TmplDerived class, but it works fine when I instantiate
the
 SimpleDerived class.

                 TmplDerived<int> d;    <-- This gives me link-time
error
                 // d.BaseMethod();

                 SimpleDerived s;        <-- Just using these 2 lines
work
 ok.
                 s.BaseMethod();

 Can anyone please advise ?

 I can and I have built this example in Micrsoft Visual C++ 6.0.

 I got my Digital Mars C++ CD today :-)

 Regards
 - Ingvaldur
Name: tmpldll.hpp tmpldll.hpp Type: HPP file
(application/x-unknown-content-type-hppfile)
               Encoding: x-uuencode

                   Name: contest.cpp
    contest.cpp    Type: MEW32 File
(application/x-unknown-content-type-cppfile)
               Encoding: x-uuencode
regards - Ingvaldur
Feb 20 2002
parent reply Christof Meerwald <cmeerw web.de> writes:
On Wed, 20 Feb 2002 17:16:36 +0100, Ingvaldur Sigurjonsson wrote:
 it doesn't seem to get exported by the generated dll while the simple class
 does.
I guess you are just expecting too much from the compiler. When compiling the DLL the compiler can't know which template instantiations might be needed, so it doesn't generate any template instantiations. The C++ standard provides a feature called "explicit template instantiation" to tell the compiler which templates to instantiate, but AFAIK, DMC++ doesn't support it yet. And there is also a "template export" feature in the C++ standard, but currently no compiler (including DMC++) supports this one. I am wondering how you got Microsoft Visual C++ to instantiate the template. bye, Christof -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?
Feb 20 2002
parent reply "Ingvaldur Sigurjonsson" <ingi ementor.se> writes:
"Christof Meerwald" <cmeerw web.de> wrote in message
news:a50oor$16p3$1 digitaldaemon.com...
 On Wed, 20 Feb 2002 17:16:36 +0100, Ingvaldur Sigurjonsson wrote:
 it doesn't seem to get exported by the generated dll while the simple
class
 does.
I guess you are just expecting too much from the compiler. When compiling the DLL the compiler can't know which template
instantiations
 might be needed, so it doesn't generate any template instantiations. The
C++
 standard provides a feature called "explicit template instantiation" to
tell
 the compiler which templates to instantiate, but AFAIK, DMC++ doesn't
 support it yet.

 And there is also a "template export" feature in the C++ standard, but
 currently no compiler (including DMC++) supports this one.


 I am wondering how you got Microsoft Visual C++ to instantiate the
template. Well I started wondering about it as well, I agree with you'r reasoning above. The reason why I believed it was possible was because I've been using both STLport v4.5.x and Dinkumware STL which both can be built and used as dll's, so why shouldn't I be able too. I guess that Microsoft is probably using some non-standard way of accomplishing this, dont blame them for that, because they usually do so and it was quite convenient :-) I think I read som article/kb-article about this as well but I cant find the reference right now. There was an easy workaround for this problem so I wont shed tears not having this functionality in DMC++. But I would like to be able to use the STLport v.4.5.x. That would make it easier for me to write reusable classes that I can use on both Win32 and Linux/Solaris and OpenVMS without bloating the code with #ifdefs.... I'm happily unknowing about the inners of the parser/compiler and how it act's on template stuff so I just hope that Walter find's time to enhance the template stuff required to for the new STLport. ( I also tried to build Dinkumware's STL from it but it choked on things which seemed to rely on Microsoft extensions and templates ). Thank you for your answer. regards - Ingvaldur
 bye, Christof

 --
 http://cmeerw.org                                 JID: cmeerw jabber.at
 mailto cmeerw at web.de

 ...and what have you contributed to the Net?
Feb 20 2002
next sibling parent "Rajiv Bhagwat" <dataflow vsnl.com> writes:
Whenever faced with similar problems, I try: -XD, which usually works. As
per documentation, you can also try to instantiate individual classes
using -XItemp<type>

- Rajiv

"Ingvaldur Sigurjonsson" <ingi ementor.se> wrote in message
news:a517h2$1d9n$1 digitaldaemon.com...
 "Christof Meerwald" <cmeerw web.de> wrote in message
 news:a50oor$16p3$1 digitaldaemon.com...
 On Wed, 20 Feb 2002 17:16:36 +0100, Ingvaldur Sigurjonsson wrote:
 it doesn't seem to get exported by the generated dll while the simple
class
 does.
I guess you are just expecting too much from the compiler. When compiling the DLL the compiler can't know which template
instantiations
 might be needed, so it doesn't generate any template instantiations. The
C++
 standard provides a feature called "explicit template instantiation" to
tell
 the compiler which templates to instantiate, but AFAIK, DMC++ doesn't
 support it yet.

 And there is also a "template export" feature in the C++ standard, but
 currently no compiler (including DMC++) supports this one.


 I am wondering how you got Microsoft Visual C++ to instantiate the
template. Well I started wondering about it as well, I agree with you'r reasoning above. The reason why I believed it was possible was because I've been using both STLport v4.5.x and Dinkumware STL which both can be built and used as
dll's,
 so why shouldn't I be able too.
 I guess that Microsoft is probably using some non-standard way of
 accomplishing this, dont blame them for that, because they usually do so
and
 it was quite convenient :-)

 I think I read som article/kb-article about this as well but I cant find
the
 reference right now.

 There was an easy workaround for this problem so I wont shed tears not
 having this functionality in DMC++.

 But I would like to be able to use the STLport v.4.5.x. That would make it
 easier for me to write reusable classes that I can use on both Win32 and
 Linux/Solaris and OpenVMS without bloating the code with #ifdefs....

 I'm happily unknowing about the inners of the parser/compiler and how it
 act's on template stuff so I just hope that Walter find's time to enhance
 the template stuff required to for the new STLport. ( I also tried to
build
 Dinkumware's STL from it but it choked on things which seemed to rely on
 Microsoft extensions and templates ).

 Thank you for your answer.

 regards
 - Ingvaldur

 bye, Christof

 --
 http://cmeerw.org                                 JID: cmeerw jabber.at
 mailto cmeerw at web.de

 ...and what have you contributed to the Net?
Feb 20 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Ingvaldur Sigurjonsson" <ingi ementor.se> wrote in message
news:a517h2$1d9n$1 digitaldaemon.com...
 ( I also tried to build
 Dinkumware's STL from it but it choked on things which seemed to rely on
 Microsoft extensions and templates ).
It'd be useful to send customer email to Dinkumware requesting a DMC version of their great library.
Feb 21 2002