我有一個MFC DLL,由同一解決方案中的另一個項目使用。從消費項目中調用時,一切都會按照它應該的方式工作,包括我將在下面寫的功能。C++/VS 2012 - 多項目解決方案:鏈接器錯誤在一個項目中,但不是另一個
我最近試圖把這個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
到目前爲止好。就像我說的那樣,從主項目調用時測試就OK了。然而,在我的測試項目中,它被稱爲是這樣的:
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> > >)" ([email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@@@[email protected]@@Z) referenced in function "private: bool __thiscall FileUtilsTest::FileExistsTestCString(void)" ([email protected]@@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"));
}
任何人都可以建議我要去哪裏錯了嗎?
您是否在'FileTestUtils'項目中包含了'DLL-name.lib'文件? –
我編輯了你的問題,以顯示可能的真正問題。你的CString專用於'char'。很有可能您的DLL是專門爲'wchar_t'構建的。避免像_T()這樣的東西,或者仍然將「字符集」設置設置爲多字節的代碼,Unicode已經存在了21年。 –
感謝漢斯。我繼承了這段代碼,大部分時間都是C#lad,所以我已經把CStrings和_T()作爲前一個開發者留給我的。儘管我正在重構並試圖在可能的情況下進行改進,但我還是有很多東西要學習。 CStrings有專業化(我認爲他們的重點是沒有專業化?)以及如何更改項目中的專業化。我將開始使用Google搜索! – technorabble