2011-07-10 124 views
1

编译时出现 问题 用g ++连接程序和多个文件(我通常使用vstudio,但是...)。g ++编译多个文件

  1. 如果我只用main.cpp中(并包含相应的头文件OpenCV的),一切都确定了

    g++ main.cpp -o main -I"C:\OpenCV2.1\include\opencv" -L"C:\OpenCV2.1\lib" 
    -lcxcore210 -lcv210 -lhighgui210 
    
  2. 如果我的main.cpp和一些otherfile.cpp(两者都需要OpenCV的),并使用

    g++ main.cpp otherfile.cpp -o main -I"C:\OpenCV2.1\include\opencv" 
    -L"C:\OpenCV2.1\lib" -lcxcore210 -lcv210 -lhighgui210 
    

    它根本不起作用,我得到了

    c:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning: a 
    uto-importing has been activated without --enable-auto-import specified on the c 
    ommand line. 
    This should work unless it involves constant data structures referencing symbols 
    from auto-imported DLLs. 
    C:\Users\ONDEJM~1\AppData\Local\Temp\ccNisCoC.o:main.cpp:(.text+0x16d0): undefin 
    ed reference to `cv::Mat::Mat(_IplImage const*, bool)' 
    C:\Users\ONDEJM~1\AppData\Local\Temp\ccNisCoC.o:main.cpp:(.text+0x16f1): undefin 
    ed reference to `cv::FAST(cv::Mat const&, std::vector<cv::KeyPoint, std::allocat 
    or<cv::KeyPoint> >&, int, bool)' 
    C:\Users\ONDEJM~1\AppData\Local\Temp\ccNisCoC.o:main.cpp:(.text$_ZN2cv3Mat7relea 
    seEv[cv::Mat::release()]+0x3f): undefined reference to `cv::fastFree(void*)' 
    collect2: ld returned 1 exit status 
    

我在做什么错?

+2

这是一个链接问题,而不是编译问题。你有没有尝试单独编译main.cpp和otherfile.cpp,然后将生成的.o文件链接在一起?这是否会影响错误? – Dan

+0

是的,我试过了。单独编译这两个文件不是问题,但是,将它们一起构建会返回相同的错误 – morph

+0

请参阅相关的http://stackoverflow.com/questions/1095298/gcc-c-linker-errors-undefined-reference-to-vtable -for-xxx-undefined -referen/1095321#1095321对此更多信息 – jonsca

回答

0

嗯,看起来我犯了一个愚蠢的错误......解决方案很简单:只需用g ++重新编译所有openCV binarries,一切都会好的!

0

将“-Wl, - enable-auto-import”传递给g ++。阅读ld关于这个的文档。

+0

好,警告已离开,但未定义引用的问题不是...... – morph