Search

Linker can not find exported templates in CLR support mode by Xentrax (Vyacheslav Lanovets)

Closed
as By Design Help for as By Design

2
Sign in to vote
2
Sign in to vote
Sign in
to vote
Type: Bug
ID: 109979
Opened: 4/16/2006 8:38:22 AM
Access Restriction: Public
0
Workaround(s)
2
User(s) can reproduce this bug
In CLR mode if the dll exports some template classes then module that imports them can not be linked.
Details (expand)
Product Language
English
Version
Visual Studio 2005
Category
Visual C++ Development
Operating System
Windows XP Professional
Operating System Language
US English
Steps to Reproduce
Create an empty solution and 2 NATIVE C++ projects. One for console application and one for the DLL.

Add to the DLL project next file "templ.h"
#pragma once
template <typename T> public class TSuperType
{
public:
T __stdcall getData();
};

Then add new cpp fiile to the DLL project with this code:

#include "templ.h"
template <typename T>
T TSuperType<T>::getData()
{
return T();
}
template class __declspec (dllexport) TSuperType<int>;

Then you need to import this template specialization into EXE project:

template class __declspec (dllimport) TSuperType<int>;
int _tmain(int argc, _TCHAR* argv[])
{
TSuperType<int> x;
int xx = x.getData();
return 0;
}

Set EXE project to be dependent on DLL project in the dependency tree!
This compiles and runs in native mode.

Then change projects setting to support CLR on General tab (i.e. compile with /clr switch). Rebuild all. You get the following errors

------ Build started: Project: TestCLRXportTemplates, Configuration: Debug Win32 ------
Linking...
TestCLRXportTemplates.obj : error LNK2028: unresolved token (0A000022) "public: int __stdcall TSuperType<int>::getData(void)" (?getData@?$TSuperType@H@@$$FQAGHXZ) referenced in function "int __cdecl wmain(int,wchar_t * * const)" (?wmain@@$$HYAHHQAPA_W@Z)
TestCLRXportTemplates.obj : error LNK2019: unresolved external symbol "public: int __stdcall TSuperType<int>::getData(void)" (?getData@?$TSuperType@H@@$$FQAGHXZ) referenced in function "int __cdecl wmain(int,wchar_t * * const)" (?wmain@@$$HYAHHQAPA_W@Z)
C:\Documents\Visual Studio 2005\Projects\Test\TestCLRXportTemplates\Debug\TestCLRXportTemplates.exe : fatal error LNK1120: 2 unresolved externals
TestCLRXportTemplates - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
Actual Results
Linker errors

Workaround: If you explicitly add DLL import library (dllname.lib) to the EXE project settings (Linker|Input|Additional dependencies tab) then it compiles too.
Expected Results
Successful compilation
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 4/17/2006 at 2:46 PM
Thanks for reporting this. I can reproduce the problem. From a quick investigation, it looks like the IDE is improperly discarding the import library once /clr is turned on. However, I'm sending the bug to the VC IDE team for more analysis. In the meantime, a good workaround is exactly what you mentioned - add the import lib yourself.

Arjun Bijanki
Visual C++ Compiler Team
Posted by Microsoft on 4/17/2006 at 3:02 PM
Thanks for reporting this. I can reproduce the problem. From a quick investigation, it looks like the IDE is improperly discarding the import library once /clr is turned on. However, I'm sending the bug to the VC IDE team for more analysis. In the meantime, a good workaround is exactly what you mentioned - add the import lib yourself.

Arjun Bijanki
Visual C++ Compiler Team
Posted by Microsoft on 4/24/2006 at 3:10 PM
actually the IDE intentionally does not link in import libs from dependent projects that are managed (use /clr). the IDE has no way of knowing what the user intended when they depend on a managed output. did they intend to access the dependent project via .NET mechanisms (which would require a #using or /FU of the assembly) or did they want to access it via native mechanisms (like import libs or LoadLibrary() calls). the assumption was made in VS 2002 that managed assemblies would be referenced via managed mechanisms by default, and this is the default that we have stuck with since then.

in any case the previous poster was correct, the work-around is to add the import library to the link line ("additional dependencies" property) yourself.

josh