C++/VS 2012-多项目解决方案:一个项目中存在链接器错误,但另一个项目没有

C++ / VS 2012 - Multi-project solution: Linker errors in one project but not another

本文关键字:项目 链接 存在 错误 另一个 一个 2012- VS 解决方案 C++      更新时间:2023-10-16

我有一个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:_SVNCalibrationToolUnitTestsFileUtilsTest.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"));
}

有人能告诉我哪里出了问题吗?

在Visual studio 10中(我想与2012的版本是一样的)有3种不同的方式来添加依赖项。

  1. 手动-在项目设置中添加include/library路径,以及添加库
  2. 新建(更喜欢这样做…)-项目->属性。选择:通用属性->框架和引用。按"添加参考"。现在选择依赖项
  3. Old=添加依赖项-在解决方案资源管理器,然后选择"添加依赖项">

我认为你的问题是你在一个项目中使用了一种方式,而在第二个项目中则使用了另一种方式。

另一种可能性是,一个项目是静态lib。所以它不需要链接。