LD:与STL库链接

时间:2010-09-07 14:29:53

标签: g++ opencv ld

我试图编译一个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错误消失了!所以我想我解决了这个问题。唯一的问题是:如何使用STL库ld链接我的程序??????

提前致谢

1 个答案:

答案 0 :(得分:2)

使用

构建正确的解决方案
g++ test.c -lhighgui -lcv -lcxcore -Wl,--enable-auto-import

与'gpp'别名不同,这会将库放在引用它们的对象之后(在链接归档库时很重要),并且还会将--enable-auto-import标志正确地传递给链接器。

您当前的“修复”仅“偶然”起作用。