I am using Visual Studio 10 set to use the v90 platform toolset. I have three applications that use several of the same classes. So, rather than have three copies of each of those classes, I am trying to move them out into a common static lib. One of them is giving me link problems. The others seem to link fine. Here’s the problem class:
HEADER
#ifndef LIMIT_SINGLE_INSTANCE_INCLUDED
#define LIMIT_SINGLE_INSTANCE_INCLUDED
#include <windows.h>
class CLimitSingleInstance
{
protected:
DWORD m_dwLastError;
HANDLE m_hMutex;
public:
CLimitSingleInstance(TCHAR *strMutexName);
~CLimitSingleInstance();
BOOL IsAnotherInstanceRunning();
};
#endif
BODY
#include "LimitSingleInstance.h"
CLimitSingleInstance::CLimitSingleInstance(TCHAR *strMutexName)
{
//Make sure that you use a name that is unique for this application otherwise
//two apps may think they are the same if they are using same name for
//3rd parm to CreateMutex
m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
m_dwLastError = GetLastError(); //save for use later
}
CLimitSingleInstance::~CLimitSingleInstance()
{
if (m_hMutex) //Do not forget to close handles.
{
CloseHandle(m_hMutex); //Do as late as possible.
m_hMutex = NULL; //Good habit to be in.
}
}
BOOL CLimitSingleInstance::IsAnotherInstanceRunning()
{
return (ERROR_ALREADY_EXISTS == m_dwLastError);
}
When this class is directly part of one of my main application solutions, there is no problem. I have now moved it into my static lib solution, and that solution builds fine. However, I find that I can no longer link my main application solution against my new static lib. Here is the result of attempting to build both debug and release versions of my main application:
—— Rebuild All started: Project: WCCJ, Configuration: ReleaseTENA Win32 —— CAssetEntity.cpp main.cpp ReadWCCJParameters.cpp
WCCJ.cpp WCCJParameters.cpp Generating Code…
DCTUtilsRel.lib(MessageWrapper.obj) : MSIL .netmodule or module
compiled with /GL found; restarting link with /LTCG; add /LTCG to the
link command line to improve linker performance
Creating library ….\bin\WCCJ-TENA.lib and object ….\bin\WCCJ-TENA.exp main.obj : error LNK2001: unresolved external
symbol “public: __thiscall
CLimitSingleInstance::CLimitSingleInstance(char *)”
(??0CLimitSingleInstance@@QAE@PAD@Z) ….\bin\WCCJ-TENA.exe : fatal
error LNK1120: 1 unresolved externals
—— Rebuild All started: Project: WCCJ, Configuration: DebugTENA Win32 —— CAssetEntity.cpp main.cpp ReadWCCJParameters.cpp
WCCJ.cpp WCCJParameters.cpp Generating Code… CAssetEntity.obj :
warning LNK4075: ignoring ‘/EDITANDCONTINUE’ due to ‘/INCREMENTAL:NO’
specification
Creating library ….\bin\WCCJ-TENA-d.lib and object ….\bin\WCCJ-TENA-d.exp main.obj : error LNK2019: unresolved
external symbol “public: __thiscall
CLimitSingleInstance::CLimitSingleInstance(char *)”
(??0CLimitSingleInstance@@QAE@PAD@Z) referenced in function “void
_cdecl `dynamic initializer for ‘gSingleInstanceObj”(void)” (??_EgSingleInstanceObj@@YAXXZ) ….\bin\WCCJ-TENA-d.exe : fatal
error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 2 failed, 0 skipped ==========
When I open the .lib in a binary editor and search for the mangled name the linker wants (??0CLimitSingleInstance@@QAE@PAD@Z), I find that it is indeed not found. The closest matches I can find are:
??0CLimitSingleInstance@@QAE@PA_W@Z
??1CLimitSingleInstance@@QAE@XZ
@CLimitSingleInstance@@QAEHXZ
Is anybody able to tell me why this is happening and how to fix it? Thanks in advance.
Dave
Use the undname.exe utility from the VS command line on that symbol:
Note the argument type, wchar_t*, not char*. Your lib project has UNICODE #defined, your exe project does not. The relevant setting is General + Character Set.