gpt4 book ai didi

c++ - 使用 vector 模板时出现 mingw 链接器错误

转载 作者:行者123 更新时间:2023-11-28 03:42:36 24 4
gpt4 key购买 nike

我在 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++ 编译工作正常。

最佳答案

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

FAQ说,

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.

所以我猜问题是因为 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

希望对您有所帮助。

关于c++ - 使用 vector 模板时出现 mingw 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8645587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com