2014-01-06 52 views
0

我有一個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")); 
    } 

任何人都可以建議我要去哪裏錯了嗎?

+2

您是否在'FileTestUtils'項目中包含了'DLL-name.lib'文件? –

+1

我編輯了你的問題,以顯示可能的真正問題。你的CString專用於'char'。很有可能您的DLL是專門爲'wchar_t'構建的。避免像_T()這樣的東西,或者仍然將「字符集」設置設置爲多字節的代碼,Unicode已經存在了21年。 –

+0

感謝漢斯。我繼承了這段代碼,大部分時間都是C#lad,所以我已經把CStrings和_T()作爲前一個開發者留給我的。儘管我正在重構並試圖在可能的情況下進行改進,但我還是有很多東西要學習。 CStrings有專業化(我認爲他們的重點是沒有專業化?)以及如何更改項目中的專業化。我將開始使用Google搜索! – technorabble

回答

1

在Visual Studio 10中(我猜與vs 2012是一樣的)有3種不同的方式來添加依賴關係。

  1. 手動 - 在項目設置中添加包含/庫路徑,並且添加庫庫 。
  2. 新(喜歡這樣做...) - 項目 - >屬性。 選擇:通用屬性 - >框架和參考。 按下「添加參考」。現在選擇依賴關係。
  3. 舊=添加依賴 - 當你對上項目的 解決方案資源管理器中右擊並選擇「添加依賴」

我覺得你的問題是,你使用的一個方式在一個項目和其他方式爲第二。

其他可能性是一個項目是靜態庫。所以它不需要鏈接。

+0

請參閱此處:「如何使用」添加引用「對話框添加或刪除引用」(http://msdn.microsoft.com/zh-cn/library/wkze6zky(v=vs.110).aspx) – yasouser

相關問題