1

我正在使用 Visual Studio 10 集来使用 v90 平台工具集。我有三个使用几个相同类的应用程序。因此,我不是每个类都拥有三个副本,而是试图将它们移到一个通用的静态库中。其中之一是给我链接问题。其他的似乎链接很好。这是问题类:

标题

#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

身体

#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);
}

当这个类直接成为我的主要应用程序解决方案之一时,没有问题。我现在已将其移至我的静态库解决方案中,并且该解决方案构建良好。但是,我发现我无法再将我的主要应用程序解决方案与我的新静态库链接起来。这是尝试构建我的主应用程序的调试和发布版本的结果:

------ 重建全部开始:项目:WCCJ,配置:ReleaseTENA Win32 ------ CAssetEntity.cpp main.cpp ReadWCCJParameters.cpp
WCCJ.cpp WCCJParameters.cpp 生成代码...
DCTUtilsRel.lib(MessageWrapper.obj) : 找到 MSIL .netmodule 或使用 /GL 编译的模块;重新启动与 /LTCG 的链接;将 /LTCG 添加到链接命令行以提高链接器性能 创建库 ....\bin\WCCJ-TENA.lib 和对象 ....\bin\WCCJ-TENA.exp main.obj : error LNK2001: unresolved external symbol "public: __thiscall CLimitSingleInstance::CLimitSingleInstance(char *)" (??0CLimitSingleInstance@@QAE@PAD@Z) ....\bin\WCCJ-TENA.exe : 致命错误 LNK1120: 1 unresolved externals ----- - 重建全部开始:项目:WCCJ,配置:DebugTENA Win32 ------ CAssetEntity.cpp main.cpp ReadWCCJParameters.cpp
WCCJ.cpp WCCJParameters.cpp 生成代码... CAssetEntity.obj : 警告 LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification Creating library ....\bin\WCCJ-TENA-d.lib and object ....\bin\WCCJ-TENA-d.exp main.obj:错误 LNK2019:引用了未解析的外部符号“公共:__thiscall CLimitSingleInstance::CLimitSingleInstance(char *)”(??0CLimitSingleInstance@@QAE@PAD@Z)在函数“void _ cdecl 'gSingleInstanceObj''(void)”的动态初始化程序中 (?? _EgSingleInstanceObj@@YAXXZ) ....\bin\WCCJ-TENA-d.exe : 致命错误 LNK1120: 1 unresolved externals == ========全部重建:0成功,2失败,0跳过==========

当我在二进制编辑器中打开 .lib 并搜索链接器想要的错误名称 (??0CLimitSingleInstance@@QAE@PAD@Z) 时,我发现它确实没有找到。我能找到的最接近的匹配是:

??0CLimitSingleInstance@@QAE@PA_W@Z

??1CLimitSingleInstance@@QAE@XZ

@CLimitSingleInstance@@QAEHXZ

有人能告诉我为什么会发生这种情况以及如何解决吗?提前致谢。

戴夫

4

2 回答 2

2

好像没有 CLimitSingleInstance::CLimitSingleInstance(char_t *)。在你的图书馆。您是否检查了这两个项目的定义?

有 Microsoft 实用程序:undname.exe它是 VisualStudio 的一部分:

C:\Program Files\Microsoft Visual Studio\COMMON\Tools>UNDNAME.EXE
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998

usage: UNDNAME [-f] decorated-names...
       -f Undecorate fully.  Default is to only undecorate the class::member


所以链接器想要

>UNDNAME.EXE -f ??0CLimitSingleInstance@@QAE@PAD@Z
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998

>> ??0CLimitSingleInstance@@QAE@PAD@Z == public: __thiscall CLimitSingleInstance::CLimitSingleInstance(char *)

你在图书馆里有:

>UNDNAME.EXE -f ??0CLimitSingleInstance@@QAE@PA_W@Z
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998

>> ??0CLimitSingleInstance@@QAE@PA_W@Z == public: __thiscall CLimitSingleInstance::CLimitSingleInstance(wchar_t *)


>UNDNAME.EXE -f ??1CLimitSingleInstance@@QAE@XZ
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998

>> ??1CLimitSingleInstance@@QAE@XZ == public: __thiscall CLimitSingleInstance::~CLimitSingleInstance(void)
于 2012-06-09T20:29:44.527 回答
2

我能找到的最接近的匹配是:??0CLimitSingleInstance@@QAE@PA_W@Z

在该符号上使用 VS 命令行中的 undname.exe 实用程序:

C:\>undname ??0CLimitSingleInstance@@QAE@PA_W@Z
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.

Undecoration of :- "??0CLimitSingleInstance@@QAE@PA_W@Z"
is :- "public: __thiscall CLimitSingleInstance::CLimitSingleInstance(wchar_t *)"

请注意参数类型 wchar_t*,而不是 char*。你的 lib 项目有 UNICODE #defined,你的 exe 项目没有。相关设置为General + Character Set。

于 2012-06-09T20:33:36.973 回答