使用矢量模板时 mingw 链接器错误

发布于 2024-12-23 01:42:07 字数 1720 浏览 2 评论 0原文

我在 Windows 7 上使用 MinGw。以下简单程序编译正常,但链接器抱怨,我不明白出了什么问题:

#include <iostream>   
#include <vector>

using namespace std;

int main()
{
   std::vector<int> iv;
   iv.push_back(7);
   cout << iv.back() << endl;
   return 0;
}

编译器/链接器消息如下所示:

mingw32-g++.exe -Wall -fexceptions  -std=c++0x -Wall -g  -std=c++0x -Wall -g -frepo   -IC:\cppbuchincludes\include -IG:\Boost -IG:\Users\thomas\cpp\STLUsage\\include  -c G:\Users\thomas\cpp\STLUsage\main.cpp -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\STLUsage.exe obj\Debug\main.o    G:\Boost\stage\lib\libboost_filesystem-mgw45-mt-d-1_45.dll.a G:\Boost\stage\lib\libboost_regex-mgw45-mt-d-1_45.dll.a G:\Boost\stage\lib\libboost_system-mgw45-mt-d-1_45.dll.a G:\Boost\stage\lib\libboost_thread-mgw45-mt-1_45.dll.a G:\Boost\stage\lib\libboost_unit_test_framework-mgw45-mt-d-1_45.dll.a
collect: recompiling G:\Users\thomas\cpp\STLUsage\main.cpp
collect: relinking
collect2: '_ZNSt12_Vector_baseIiSaIiEEC1Ev' was assigned to 'obj\Debug\main.rpo', but was not defined during recompilation, or vice versa
obj\Debug\main.o: In function `vector':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_vector.h:208: undefined reference to `std::_Vector_base<int, std::allocator<int> >::_Vector_base()'
(...and so on...)

我可以使用我自己定义的模板。

我从一本书中找到了 MinGw 二进制文件,并按照该书中有关编译器设置的说明进行操作。特别是对 Boost 库的引用是从那里获取的。

这肯定是一件简单的事情,我只是想简单地使用一下STL。

编辑 按照答案中给出的建议,我在“设置”->“g++.exe”中替换了用于编译的二进制文件。编译与调试->工具链可执行文件对话框,但我收到相同的错误消息(mingw32-g++.exe 现在被 g++.exe 替换)。

编辑(再次)这一定是 Code::Blocks 设置的问题,因为从命令行使用 g++ 进行编译效果很好。

I'm using MinGw on Windows 7. The following simple program compiles fine, but the linker complains and I do not understand what's wrong:

#include <iostream>   
#include <vector>

using namespace std;

int main()
{
   std::vector<int> iv;
   iv.push_back(7);
   cout << iv.back() << endl;
   return 0;
}

the compiler/linker messages look as follows:

mingw32-g++.exe -Wall -fexceptions  -std=c++0x -Wall -g  -std=c++0x -Wall -g -frepo   -IC:\cppbuchincludes\include -IG:\Boost -IG:\Users\thomas\cpp\STLUsage\\include  -c G:\Users\thomas\cpp\STLUsage\main.cpp -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\STLUsage.exe obj\Debug\main.o    G:\Boost\stage\lib\libboost_filesystem-mgw45-mt-d-1_45.dll.a G:\Boost\stage\lib\libboost_regex-mgw45-mt-d-1_45.dll.a G:\Boost\stage\lib\libboost_system-mgw45-mt-d-1_45.dll.a G:\Boost\stage\lib\libboost_thread-mgw45-mt-1_45.dll.a G:\Boost\stage\lib\libboost_unit_test_framework-mgw45-mt-d-1_45.dll.a
collect: recompiling G:\Users\thomas\cpp\STLUsage\main.cpp
collect: relinking
collect2: '_ZNSt12_Vector_baseIiSaIiEEC1Ev' was assigned to 'obj\Debug\main.rpo', but was not defined during recompilation, or vice versa
obj\Debug\main.o: In function `vector':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_vector.h:208: undefined reference to `std::_Vector_base<int, std::allocator<int> >::_Vector_base()'
(...and so on...)

I can use templates I defined myself.

I have that MinGw binary from a book and followed the instructions in that book regarding compiler settings. In particular the references to the Boost libs are taken from there.

This must be a simple thing, I just want to make trivial use of the STL.

Edit following the advice given in an answer, I replaced the binary to be used to compile by g++.exe in the Settings -> Compiler and debugging -> toolchain executables dialog, but I'm getting the same error messages (with mingw32-g++.exe now replaced by g++.exe).

Edit (once more) this has to be problem eith the Code::Blocks settings, since compiling using g++ from the command line works just fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子 2024-12-30 01:42:07

使用g++编译和链接程序。 mingw32-g++.exe 不会这样做。

常见问题解答 说,

gcc 和 mingw32-gcc 有什么区别?

mingw32-gcc、mingw32-g++ 等二进制文件的存在是为了辅助交叉开发。它们是在典型的 gcc 构建中创建的。因此,它们按照 GCC 维护者的意图进行分发。 gcc.exe 表示该二进制文件为等于构建的目标生成二进制文件,而 mingw32-gcc 二进制文件则生成要在 mingw32 目标上执行的二进制文件。

所以我猜问题是由于 mingw32-g++.exe 造成的,您在正常构建中应该使用它。

尝试这些:

g++ program.cpp              //simple build

g++ program.cpp  -Wall       //build with all warnings enabled

g++ program.cpp  -Wall -O2   //enable warnings and optimization level 2

g++ program.cpp -std=c++0x   //use C++11 features

希望有帮助。

Use g++ to compile and link the program. mingw32-g++.exe doesn't do that.

FAQ says,

What's the difference between gcc and mingw32-gcc?

The mingw32-gcc, mingw32-g++, etc. binaries exist as an aid to cross development. They are created in a typical build of gcc. They are therefore distributed as the maintainers of GCC meant them to be. The gcc.exe indicates that the binary produces binaries for a target equal to the build, while the mingw32-gcc binary produces binaries to be executed on the mingw32 target.

So I guess the problem is because of mingw32-g++.exe which you're not supposed to use, for normal build.

Try these:

g++ program.cpp              //simple build

g++ program.cpp  -Wall       //build with all warnings enabled

g++ program.cpp  -Wall -O2   //enable warnings and optimization level 2

g++ program.cpp -std=c++0x   //use C++11 features

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文