【问题标题】:LNK2019: unresolved extrenal symbol when using std::ifstreamLNK2019:使用 std::ifstream 时无法解析的外部符号
【发布时间】:2021-01-26 03:25:36
【问题描述】:

我在 c++ 中使用 std::ifstream 时遇到问题。我正在使用 Visual Studio 2019。每次我想初始化 std::ifstream 对象时,都会出现以下错误:

文件:Shader.obj

函数“void * __cdecl std::_Allocate_manually_vector_aligned(unsigned int)”中引用的 LNK2019 未解析的外部符号 __imp___invalid_parameter (??$_Allocate_manually_vector_aligned@U_Default_allocate_traits@std@@@std@@YAPAXI@Z)

函数“void * __cdecl std::_Allocate_manually_vector_aligned(unsigned int)”(??$_Allocate_manually_vector_aligned@U_Default_allocate_traits@std@@@std@@YAPAXI@Z) 中引用的 LNK2019 未解析的外部符号 __imp___CrtDbgReport

文件:msvcprtd.lib(locale0_implib.obj)

函数“public: static void __cdecl std::_Fac_node::operator delete(void *)”(??3_Fac_node@std@@SAXPAX@Z)中引用的LNK2019未解析的外部符号__imp___free_dbg

函数“public: static void * __cdecl std::_Fac_node::operator new(unsigned int)”(??2_Fac_node@std@@SAPAXI@Z)中引用的LNK2019未解析的外部符号__imp___malloc_dbg

Shader.h

#pragma once

#include <iostream>
#include <fstream>
#include <string>

#include <GLFW/glfw3.h>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

class Shader
{
private:
    /* some code */
public:
    /* some code */
    void setSourceFile(const std::string& filename);
}

Shader.cpp

#include "Shader.h"

/* some code */

void Shader::setSourceFile(const std::string& filename)
{
    std::ifstream* shaderFile = new std::ifstream();
}

即使我只运行std::ifstream shaderFile();,甚至在将参数传递给构造函数之后,这些错误仍然存​​在。当我对此发表评论时,程序会正确构建。

【问题讨论】:

  • 如果您只在Shader.cpp 中写入#include &lt;fstream&gt; 会怎样?
  • “即使我只运行std::ifstream shaderFile();”,这些错误仍然存​​在 - 仅供参考,这甚至不是对象定义;它是一个名为 shaderFile 的函数的声明,它不接受任何参数并返回一个按值 std::ifstream
  • @StackDanny 没有任何改变。
  • 这看起来像您正在将调试版本链接到非调试(发布)库。
  • 调试和发布混用是不安全的。

标签: c++ visual-studio compiler-errors ifstream lnk2019


【解决方案1】:

问题是我将一些 realease 库(glew32.libglfw32.lib)与调试库混合在一起。所以在 Project Properties->Linker->General 我已将 Additional Library Directories 更改为调试库,并在 Linker->Input 更改了文件名字。

感谢@drescherjm 和@1201ProgramAlarm 的建议!

【讨论】:

  • 很高兴您得到了解决方案,感谢您的分享,如果您将它们标记为答案,我将不胜感激,这将对其他社区有益。
猜你喜欢
  • 2019-03-05
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多