c++ - iostream 链接器错误

标签 c++ c linker iostream

我有一些旧的 C 代码,我想与一些 C++ 代码结合使用。

以前的C代码有以下内容:

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "mysql.h"

现在我正在尝试让它像这样使用 C++ 和 iostream:

#include <windows.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "mysql.h"

但我在编译时不断收到以下链接器错误:

[Linker error] undefined reference to `std::string::size() const'

[Linker error] undefined reference to `std::string::operator[](unsigned int) const'

[Linker error] undefined reference to `std::string::operator[](unsigned int) const'

[Linker error] undefined reference to `std::string::operator[](unsigned int) const'

[Linker error] undefined reference to `std::ios_base::Init::Init()'

[Linker error] undefined reference to `std::ios_base::Init::~Init()'

ld returned 1 exit status

我该如何解决这个问题?

编辑:我的编译器是 Dev-C++ 4.9.9.2

最佳答案

C string.h header 和 C++ string header 不可互换。

不过,总体而言,您的问题是文件已正确编译,但链接到了错误的运行时库。

Dev-C++ 使用 GCC。 GCC 可以根据文件扩展名正确确定文件中的语言,但不会链接正确的运行时库,除非您特别要求它(命令行中的 -lstdc++)。将 GCC 称为“g++”(或者,在您的情况下,称为“mingwin32-g++”)也将获得正确的语言并链接所需的库。

关于c++ - iostream 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/264057/

相关文章:

c# - 使用 MemoryMappedFile 将 2 个数组从 CreateFileMappingW 共享到 C#

c++ - 为什么 MonitoringManager::OnRequestSucceeded() 中的 assert(s_monitors) 会失败?

c - C中的交换指针

makefile - BoringSSL 的 undefined symbol

python - 嵌入Python 3.3

c++ - 如何使用 WiX 手动注册 64 位 DLL

c++ - 尽管推送成功,但线程池队列未使用推送的项目进行更新

c - 如何在c中正确使用bsearch?

c++ - 枚举和定义语句之间的区别

c++ - 如何将二进制数据嵌入/链接到 Windows 模块中