Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting around a LNK2019 C++ template issue?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/456713/why-do-i-get-unresolved-external-symbol-errors-when-using-templates">Why do I get “unresolved external symbol” errors when using templates?</a> </p> </blockquote> <p>My solution is split into 3 projects. The first being an MFC (GUI) app, the second being a static lib, and the third being a DLL. The DLL depends on the static lib, and the GUI depends on both. The idea here was to write an SDK (static lib) complete with a demo plugin (DLL) which the MFC (GUI) app uses.</p> <p>That being said, the SDK contains template functions to allow plugin developers to choose a prototype function to hook. But there seems to be an implementation problem with the dll vs. the lib...<br/><br/></p> <p>SdkLib.h:</p> <pre><code>#define MY_EXPORTS #include "SdkHookMgr.h" // Prototypes typedef HMODULE (WINAPI *HookLoadLibraryA)( LPCSTR lpFileName ); typedef HMODULE (WINAPI *HookLoadLibraryW)( LPWSTR lpFileName ); </code></pre> <p>SdkHookMgr.h:</p> <pre><code>#ifdef MY_EXPORTS #define MY_API __declspec(dllexport) WINAPI #else #define MY_API WINAPI #endif template &lt;typename HookFunction&gt; BOOL MY_API Hook(HookFunction Target); </code></pre> <p>SdkHookMgr.cpp:</p> <pre><code>#include "SdkHookMgr.h" template &lt;typename HookFunction&gt; BOOL MY_API Hook(HookFunction Target) { switch( typeid(Target) ) { case HookLoadLibraryA: // Use private SDK functions to hook LoadLibraryA break; case HookLoadLibraryW: // Use private SDK functions to hook LoadLibraryW break; //... default: return FALSE; } return TRUE; } </code></pre> <p>demodll.cpp:</p> <pre><code>#include &lt;SdkLib.h&gt; HMODULE WINAPI Hooked_LoadLibraryA ( LPCSTR lpFileName ) { //... } //... Hook&lt;HookLoadLibraryA&gt;(Hooked_LoadLibraryA); </code></pre> <p><br/><br/> The issue here is that the lib compiles just fine, but the dll shows: &nbsp;</p> <pre><code>demodll.obj : error LNK2019: unresolved external symbol "int __stdcall Hook&lt;struct HINSTANCE__ * (__stdcall*)(char const *)&gt;(struct HINSTANCE__ * (__stdcall*)(char const *))" </code></pre> <p>&nbsp;<br/> I suppose what's happening is the lib sees the implementation in <code>SdkHookMgr.cpp</code>, but the dll doesn't (even though it links with sdklib.lib). I really enjoyed the idea of limiting developers to using prototypes and functions with identical signatures when calling the <code>Hook()</code> function, and figured templates was the best way to go... <strong>But how can I get around this loophole</strong> without having to include the implementation of <code>Hook()</code> within <code>SdkHookMgr.h</code>? The point of building a static lib was to avoid plugin developers from poking at the SDK's source code in the first place...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload