C++ iostream 链接器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/264057/
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
iostream linker error
提问by Steve
I have some old C code that I would like to combine with some C++ code.
我有一些旧的 C 代码,我想将它们与一些 C++ 代码结合起来。
The C code used to have has the following includes:
过去的 C 代码包括以下内容:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "mysql.h"
Now I'm trying to make it use C++ with iostream like this:
现在我试图让它像这样使用 C++ 和 iostream:
#include <windows.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "mysql.h"
But I keep getting the following linker errors when I compile:
但是我在编译时不断收到以下链接器错误:
[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
[链接器错误] 未定义对 `std::string::size() const' 的引用
[链接器错误] 未定义对`std::string::operator[](unsigned int) const'的引用
[链接器错误] 未定义对`std::string::operator[](unsigned int) const'的引用
[链接器错误] 未定义对`std::string::operator[](unsigned int) const'的引用
[链接器错误] 未定义对 `std::ios_base::Init::Init()' 的引用
[链接器错误] 未定义对 `std::ios_base::Init::~Init()' 的引用
ld 返回 1 个退出状态
How do I resolve this?
我该如何解决?
Edit: My compiler is Dev-C++ 4.9.9.2
编辑:我的编译器是 Dev-C++ 4.9.9.2
回答by Max Lybbert
The C string.h
header and the C++ string
header are not interchangeable.
Cstring.h
头文件和 C++string
头文件不可互换。
Overall, though, your problem is that the file is getting properly compiled, but the wrong runtime library is getting linked in.
不过,总的来说,您的问题是文件得到了正确编译,但错误的运行时库被链接到了。
Dev-C++ uses GCC. GCC can correctly determine the language in a file based on file extension, but won't link the right runtime library in unless you specifically ask it to (-lstdc++ at the command line). Calling GCC as "g++" (or, in your case, "mingwin32-g++") will also get the right language and will link the needed library.
Dev-C++ 使用 GCC。GCC 可以根据文件扩展名正确确定文件中的语言,但不会链接正确的运行时库,除非您特别要求它(命令行中的 -lstdc++)。将 GCC 称为“g++”(或者,在您的情况下为“mingwin32-g++”)也将获得正确的语言并链接所需的库。
回答by Mike G.
You need to link against your C++ runtime. It depends on your platform and compiler, but adding -lC to your linkline might do it.
您需要链接到您的 C++ 运行时。这取决于您的平台和编译器,但将 -lC 添加到您的链接行可能会这样做。
So might linking using your C++ compiler rather than ld.
所以可能会使用 C++ 编译器而不是 ld 进行链接。
In any case, you probably have to link using the C++ compiler rather than ld if you want your C++ code to work correctly -- it's often required for exceptions and static initializers to work correctly...
在任何情况下,如果您希望 C++ 代码正常工作,您可能必须使用 C++ 编译器而不是 ld 进行链接——通常需要异常和静态初始化程序才能正常工作......
回答by Amaresh
I got the same exact error when i was trying to compile with Cygwin (g++).
当我尝试使用 Cygwin (g++) 进行编译时,我遇到了完全相同的错误。
just add -L/usr/local/bin -L/usr/lib
in the compilation rules and it should work.
只需添加-L/usr/local/bin -L/usr/lib
编译规则,它应该可以工作。
This may be specific to Cygwin but it might help solve your problem too.
这可能是 Cygwin 特有的,但它也可能有助于解决您的问题。