我有一些旧的 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/