C++/VS 2012-多项目解决方案:一个项目中存在链接器错误,但另一个项目没有
C++ / VS 2012 - Multi-project solution: Linker errors in one project but not another
我有一个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种不同的方式来添加依赖项。
- 手动-在项目设置中添加include/library路径,以及添加库
- 新建(更喜欢这样做…)-项目->属性。选择:通用属性->框架和引用。按"添加参考"。现在选择依赖项
- Old=添加依赖项-在解决方案资源管理器,然后选择"添加依赖项">
我认为你的问题是你在一个项目中使用了一种方式,而在第二个项目中则使用了另一种方式。
另一种可能性是,一个项目是静态lib。所以它不需要链接。
- CMake 项目链接静态库
- 尝试使用 gradle 构建指向静态库的 c++ 项目链接时找不到头文件
- 将错误GoogleMock与我在linux下的C++项目链接
- 将OpenBLAS与Qt Creator项目链接
- 从大型 github 项目链接共享库
- 将TBB库与QTCreator与CMAKE项目链接
- 如何与Libm.so和libdl.so与我的Bazel项目链接
- 将C++项目与 Visual Studio 中的窗体项目链接
- GTEST测试项目链接链接到其他可执行文件
- Visual Studio,将项目链接在一起并进行测试
- 如何在Xcode4上从头开始构建C++项目-链接现有和新的外部库
- 我怎么能说视觉工作室将库与我的项目链接起来
- 添加类后项目链接错误
- CMake:从外部项目链接共享C++对象会生成具有相对路径的二进制文件,而不是绝对路径
- Netbeans/C++:将2个项目链接在一起(可执行文件/动态库)
- 裸机目标与cmake项目链接错误
- 使用lua lib的Xcode c++项目链接错误
- 试图从另一个项目链接类时出现链接器错误
- 如何将code::块设置为在目标链接器选项之前链接项目链接器选项
- 无法将一个项目的头和 CPP 文件与另一个项目链接 LNK2019:未解决的外部符号错误