【发布时间】:2014-01-06 16:21:59
【问题描述】:
我有一个 MFC DLL,它被同一解决方案中的另一个项目使用。当从消费项目调用时,一切都正常工作,包括我将在下面写的函数。
我最近尝试为这个 DLL 组装一个单元测试应用程序,在最近添加到 DLL 之前一直运行良好(正如我所说,我已经证明在主项目中工作正常) .
注意:我还展示了 std::string 之一,因为它可能是相关的(并且已知在我的单元测试项目中工作)。
问题函数在头文件中声明如下:
#pragma once
#include <string>
#include <afx.h>
#ifdef CALIBRATIONTOOL_EXPORTS
#define CALIBRATIONTOOL_API __declspec(dllexport)
#else
#define CALIBRATIONTOOL_API __declspec(dllimport)
#endif
namespace MyCompanyName
{
namespace CalibrationTool
{
class FileUtils
{
public:
CALIBRATIONTOOL_API static bool FileExists(std::string filename);
CALIBRATIONTOOL_API static bool FileExists(CString filename);
}; // end class FileUtils
}; // end namespace CalibrationTool
}; // end namespace MyCompanyName
并在cpp中实现:
#include "stdafx.h"
#include "FileUtils.h"
#include "StringUtils.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#ifdef CALIBRATIONTOOL_EXPORTS
#define CALIBRATIONTOOL_API __declspec(dllexport)
#else
#define CALIBRATIONTOOL_API __declspec(dllimport)
#endif
namespace MyCompanyName
{
namespace CalibrationTool
{
bool FileUtils::FileExists(std::string filename)
{
std::string filenameAsStdString(filename);
std::string filenameWithDoubleBackslashes = MyCompanyName::CalibrationTool::StringUtils::SingleToDoubleBackslash(filenameAsStdString);
std::ifstream ifile(filenameWithDoubleBackslashes);
bool exists = (bool)ifile;
ifile.close();
return exists;
}
bool FileUtils::FileExists(CString filename)
{
std::ifstream ifile(filename);
bool exists = (bool)ifile;
ifile.close();
return exists;
}
}; // end namespace CalibrationTool
}; // end namespace MyCompanyName
到目前为止一切顺利。就像我说的,从主项目调用时测试正常。但是,在我的测试项目中,它是这样调用的:
bool FileUtilsTest::FileExistsTestCString()
{
bool passed = true;
CString definitelyExists = _T("C:\\Windows\\system.ini");
CString definitelyDoesntExist = _T("C:\\NotMuchChanceThatThisFileExists.zut");
if (MyCompanyName::CalibrationTool::FileUtils::FileExists(definitelyExists))
{
// all is well
CalibrationToolUnitTestsLogging::Write("Passed FileUtilsTest.FileExistsTestCString (files which exist). Successfully found C:\\Windows\\system.ini.");
}
else
{
// all is not well
CalibrationToolUnitTestsLogging::Write("Failed FileUtilsTest.FileExistsTestCString (files which exist). Unable to find C:\\Windows\\system.ini.");
passed = false;
}
// (and more testing, e.g. for files which don't exist)
return passed;
}
我得到错误:
Error 4 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static bool __cdecl MyCompanyName::CalibrationTool::FileUtils::FileExists(class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > >)" (__imp_?FileExists@FileUtils@CalibrationTool@MyCompanyName@@SA_NV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@@Z) referenced in function "private: bool __thiscall FileUtilsTest::FileExistsTestCString(void)" (?FileExistsTestCString@FileUtilsTest@@AAE_NXZ) C:\_SVN\CalibrationToolUnitTests\FileUtilsTest.obj
为了完整起见,这是我主项目中的工作代码:
if ((MyCompanyName::CalibrationTool::FileUtils::FileExists(exists)) && (!MyCompanyName::CalibrationTool::FileUtils::FileExists(doesntExist)))
{
g_log.Write(log_debug, -1, _T("TEST"), _T("Seems fine."));
}
else
{
g_log.Write(log_debug, -1, _T("TEST"), _T("Something's up"));
}
谁能建议我哪里出错了?
【问题讨论】:
-
您是否将
DLL-name.lib文件包含在您的FileTestUtils项目中? -
我编辑了您的问题以显示可能的实际问题。您的 CString 专用于
char。很有可能您的 DLL 是专门为wchar_t构建的。请避免使用_T()之类的东西,或者仍然在“字符集”设置为多字节的情况下构建代码,Unicode 已经存在 21 年了。 -
谢谢汉斯。我继承了这段代码,并且大部分时间都是 C# 小伙子,所以我使用了前一个开发人员留给我的 CStrings 和 _T()s。尽管我正在重构并尝试在可能的情况下进行改进,但我仍然需要学习很多东西......例如CStrings 具有专业化(我认为它们的重点是没有专业化?),以及如何更改项目中的专业化。我会开始谷歌搜索!
标签: c++ visual-studio-2012 mfc linker-errors