LD:与STL库链接

3

我试图编译OpenCV的VideoCapture示例。在编译时,我得到了以下输出:

gpp test.c
Info: resolving vtable for cv::VideoCapture by linking to __imp___ZTVN2cv12VideoCaptureE (auto-import)
c:/programs/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning: auto-importing has
enable-auto-import specified on the command line.
This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.

顺便说一句,gpp是g++ -lhighgui -lcv -lcxcore的别名。

所以,我尝试使用"gpp --enable-auto-import"进行编译,但g++无法识别此选项。因此,我尝试这样编译:

gpp -c test.c
ld test.o

我遇到了同样的错误以及其他许多与STL函数有关的错误,表明它没有与STL链接:

undefined reference to std::cout
...

最后,当我按照以下方式编译时:

gpp -c test.c
ld --enable-auto-import test.o

这次,我只遇到了STL错误。VideoCapture错误已经解决了!所以我猜我解决了这个问题。唯一的问题是:我该如何让ld将我的程序与STL库链接起来?
提前感谢。

问题已解决。虽然在ld中参数是“--enable-auto-import”,但在使用g++编译时只需使用“-enable-auto-import”。这样,我就能够成功编译并运行我的示例了。 - ABC
1个回答

2
正确的解决方案是使用以下构建:
g++ test.c -lhighgui -lcv -lcxcore -Wl,--enable-auto-import

与您的“gpp”别名不同,此方法将库放在引用它们的对象之后(在链接归档库时很重要),并正确地向链接器传递--enable-auto-import标志。您目前的“修复”仅仅是“偶然”起作用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接