C ++ / VS 2012 - 多项目解决方案:一个项目中的链接器错误,而不是另一个项目中的链接器错误

时间:2014-01-06 16:21:59

标签: c++ visual-studio-2012 mfc linker-errors

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

有谁可以建议我哪里出错了?

1 个答案:

答案 0 :(得分:1)

在Visual Studio 10中(我猜vs 2012是相同的)有3种不同的方式来添加依赖。

  1. 手动 - 在项目设置中添加包含/库路径,以及 添加库。
  2. 新(喜欢这样做......) - 项目 - &gt;属性。 选择:共同属性 - &gt;框架和参考。 按“添加参考”。现在选择依赖项。
  3. 旧=添加依赖关系 - 当您右键单击项目时 解决方案资源管理器并选择“添加依赖项”
  4. 我认为你的问题是你在一个项目中采用了一种方式,而在第二种方式采用了其他方式。

    其他可能性是一个项目是静态库。所以它不需要链接。

相关问题