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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6003368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:18:34  来源:igfitidea点击:

Unresolved externals in C++ when using vectors and find

c++findstdvectorunresolved-external

提问by MintGrowth

I have tried this code in a totally separate project, and it works fine (the only difference being that the project that is not working is being exported as a DLL). Here is the code:

我在一个完全独立的项目中尝试了这段代码,它运行良好(唯一的区别是不工作的项目被导出为 DLL)。这是代码:

RTATMATHLIB.CPP

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

RTATMATHLIB.H

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

ERRORS

错误

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

And that's it. I am not sure why it works in the other project and not this one...

就是这样。我不确定为什么它适用于另一个项目而不是这个项目......

回答by Lucky Luke

I found another forum post, where somebody seems to have reported the same exact problem that you are having. Please check to see if you have

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

_DEBUG

defined either in your project settings (under C/C++ -- Preprocessor) or somewhere in your code (or include files).

在您的项目设置(在 C/C++ -- Preprocessor 下)或在您的代码(或包含文件)中的某处定义。

It looks as if std::vector thinks you are building a debug build, when you are in fact creating a release build.

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

I hope this helps.

我希望这有帮助。

回答by cubuspl42

The problem in my case was a Debug configuration with Runtime Libraryset to Multi-threaded DLL. The fix was to change it to Multi-threaded Debug DLL. The error is gone. Removing _DEBUGmacro was also a kind of workaround, by I guess it's not a good idea because you end up with debug build linked to non-debug standard library.

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

回答by Yonatan Simson

The problem was I had _DEBUG defined in C/C++->Preprocessor. Changing it to NDEBUG solved the problem.

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

回答by Anurag Daware

Worked for me with : The problem in my case was a Debug configuration with Runtime Library set to Multi-threaded DLL. The fix was to change it to Multi-threaded Debug DLL

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