使用向量和查找时C++未解析的外部

Unresolved externals in C++ when using vectors and find

本文关键字:外部 C++ 向量 查找      更新时间:2023-10-16

我已经在一个完全独立的项目中尝试了这段代码,它工作正常(唯一的区别是无法正常工作的项目被导出为 DLL)。这是代码:

RTATMATHLIB。.CPP

#include "stdafx.h"
#include "RTATMATHLIB.h"
#include <math.h>
#include <vector>
#include <algorithm>
#include <stdexcept>
using namespace std;
double someFunc(double** Y, int length)
{
    vector<double> myVector;
    for(int i = 0; i < length; i++)
    {
        double value = (*Y)[i];
        vector<double>::iterator it = find(myVector.begin(), myVector.end(), value);
        if(it != myVector.end())
        {
            continue;
        }
        else
        {
            myVector.push_back(value);
        }
    }
    return 0;
}

RTATMATHLIB。H

__declspec(dllexport) double someFunc(double** Y, int length);

错误

Error   1   error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: __thiscall std::_Vector_const_iterator<double,class std::allocator<double> >::_Vector_const_iterator<double,class std::allocator<double> >(double *,class std::_Container_base_secure const *)" (??0?$_Vector_const_iterator@NV?$allocator@N@std@@@std@@QAE@PANPBV_Container_base_secure@1@@Z)  RTATMATHLIB.obj RTATMATHLIB
Error   2   fatal error LNK1120: 1 unresolved externals

仅此而已。我不确定为什么它在另一个项目中工作而不是这个项目......

我找到了另一个论坛帖子,其中似乎有人报告了与您遇到的相同问题。请检查您是否有

_DEBUG

在项目设置(在 C/C++ -- 预处理器下)或代码中的某个位置(或包含文件)中定义。

看起来好像 std::vector 认为您正在构建调试版本,而实际上您正在创建发布版本。

我希望这有所帮助。

在我的情况下,问题是将Runtime Library设置为 Multi-threaded DLL 的调试配置。解决方法是将其更改为 Multi-threaded Debug DLL .错误消失了。删除_DEBUG宏也是一种解决方法,我想这不是一个好主意,因为您最终会得到链接到非调试标准库的调试版本。

问题是我在 C/C++->预处理器中定义了_DEBUG。将其更改为NDEBUG解决了问题。

为我工作:在我的情况下,问题是运行时库设置为多线程 DLL 的调试配置。解决方法是将其更改为多线程调试 DLL