openCV mingw+cmake编译的bug/error笔记

使用cmake生成openCV3.1.0 makefile文件,在mingw下编译openCV。

1.openCV的build和源码目录,不能存在中文路径,否则报以下错误:

mingw32-make[2]: *** No rule to make target 'J://opencv-3.1.0/3rdparty/zlib/adler32.c', needed by '3rdparty/zlib/CMakeFiles/zlib.dir/adler32.c.obj'.  Stop.
CMakeFiles\Makefile2:252: recipe for target '3rdparty/zlib/CMakeFiles/zlib.dir/all' failed
mingw32-make[1]: *** [3rdparty/zlib/CMakeFiles/zlib.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2


2.使用cmake发布makefile的时候,请禁用BUILD_TESTS,否则将在42%左右报以下错误:

G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In constructor 'testing::internal::Mutex::Mutex()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8829:45: error: cannot convert 'CRITICAL_SECTION* {aka _CRITICAL_SECTION*}' to '_RTL_CRITICAL_SECTION*' in initialization
       critical_section_(new CRITICAL_SECTION) {
                                             ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8830:48: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void InitializeCriticalSection(LPCRITICAL_SECTION)'
   ::InitializeCriticalSection(critical_section_);
                                                ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In destructor 'testing::internal::Mutex::~Mutex()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8840:46: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'PCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void DeleteCriticalSection(PCRITICAL_SECTION)'
     ::DeleteCriticalSection(critical_section_);
                                              ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In member function 'void testing::internal::Mutex::Lock()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8848:43: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void EnterCriticalSection(LPCRITICAL_SECTION)'
   ::EnterCriticalSection(critical_section_);
                                           ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In member function 'void testing::internal::Mutex::Unlock()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8858:43: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void LeaveCriticalSection(LPCRITICAL_SECTION)'
   ::LeaveCriticalSection(critical_section_);
                                           ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In member function 'void testing::internal::Mutex::ThreadSafeLazyInit()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8879:33: error: cannot convert 'CRITICAL_SECTION* {aka _CRITICAL_SECTION*}' to '_RTL_CRITICAL_SECTION*' in assignment
         critical_section_ = new CRITICAL_SECTION;
                                 ^~~~~~~~~~~~~~~~
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8880:54: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void InitializeCriticalSection(LPCRITICAL_SECTION)'
         ::InitializeCriticalSection(critical_section_);
                                                      ^
modules\ts\CMakeFiles\opencv_ts.dir\build.make:237: recipe for target 'modules/ts/CMakeFiles/opencv_ts.dir/src/ts_gtest.cpp.obj' failed
mingw32-make[2]: *** [modules/ts/CMakeFiles/opencv_ts.dir/src/ts_gtest.cpp.obj] Error 1
CMakeFiles\Makefile2:5379: recipe for target 'modules/ts/CMakeFiles/opencv_ts.dir/all' failed
mingw32-make[1]: *** [modules/ts/CMakeFiles/opencv_ts.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

3.opencv3.2.0 mingw编译,在54%左右报以下错误:

G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In constructor 'testing::internal::Mutex::Mutex()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8829:45: error: cannot convert 'CRITICAL_SECTION* {aka _CRITICAL_SECTION*}' to '_RTL_CRITICAL_SECTION*' in initialization
       critical_section_(new CRITICAL_SECTION) {
                                             ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8830:48: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void InitializeCriticalSection(LPCRITICAL_SECTION)'
   ::InitializeCriticalSection(critical_section_);
                                                ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In destructor 'testing::internal::Mutex::~Mutex()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8840:46: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'PCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void DeleteCriticalSection(PCRITICAL_SECTION)'
     ::DeleteCriticalSection(critical_section_);
                                              ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In member function 'void testing::internal::Mutex::Lock()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8848:43: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void EnterCriticalSection(LPCRITICAL_SECTION)'
   ::EnterCriticalSection(critical_section_);
                                           ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In member function 'void testing::internal::Mutex::Unlock()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8858:43: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void LeaveCriticalSection(LPCRITICAL_SECTION)'
   ::LeaveCriticalSection(critical_section_);
                                           ^
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp: In member function 'void testing::internal::Mutex::ThreadSafeLazyInit()':
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8879:33: error: cannot convert 'CRITICAL_SECTION* {aka _CRITICAL_SECTION*}' to '_RTL_CRITICAL_SECTION*' in assignment
         critical_section_ = new CRITICAL_SECTION;
                                 ^~~~~~~~~~~~~~~~
G:\Coding\MinGW\msys\1.0\home\Administrator\opencv-3.2.0\modules\ts\src\ts_gtest.cpp:8880:54: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION {aka _CRITICAL_SECTION*}' for argument '1' to 'void InitializeCriticalSection(LPCRITICAL_SECTION)'
         ::InitializeCriticalSection(critical_section_);

                                                      ^

modules\ts\CMakeFiles\opencv_ts.dir\build.make:237: recipe for target 'modules/ts/CMakeFiles/opencv_ts.dir/src/ts_gtest.cpp.obj' failed
mingw32-make[3]: *** [modules/ts/CMakeFiles/opencv_ts.dir/src/ts_gtest.cpp.obj] Error 1
CMakeFiles\Makefile2:3919: recipe for target 'modules/ts/CMakeFiles/opencv_ts.dir/all' failed
mingw32-make[2]: *** [modules/ts/CMakeFiles/opencv_ts.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make[1]: *** [all] Error 2

参考国外给出的修改.h文件,增加mingw目标的定义:

https://stackoverflow.com/questions/41930349/opencv-installation-error-while-mingw32-make-on-windows

Edit "ts_gtest.h" which is inside "opencv\sources\modules\ts\include\opencv2\ts\"

Replace this line (probably line 723) 


    // assuming CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
    // This assumption is verified by
    // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
    struct _RTL_CRITICAL_SECTION;


with



    #if GTEST_OS_WINDOWS_MINGW
        // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
        // separate (equivalent) structs, instead of using typedef
        typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
    #else
        // Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
        // This assumption is verified by
        // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION
        typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
    #endif


Replace this line (probably on line 3060 before your edit - line number would have changed as you modified first part)


    _RTL_CRITICAL_SECTION* critical_section_;


with



    GTEST_CRITICAL_SECTION* critical_section_;


These two changes should fix your above error.


4.Ubuntu14下编译FFmpeg外加库与filter报错:

/usr/bin/ld: /usr/local/lib//libTransform360.a(VideoFrameTransformHandler.cpp.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib//libTransform360.a: 无法添加符号: 错误的值

上述错误提示和原来的没有添加“-fPIC”生成的“*.o”文件生成动态库出现的错误相同,而且也给给出了解决方法,静态库的生成重新使用“-fPIC”进行编译。

[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -fPIC -c showcoor.cpp  
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ar -cr libshowcoor.a showcoor.o  
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -fPIC -c showpoint.cpp            
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -shared -fPIC -o libshowpoint.so showpoint.o -L./ -lshowcoor  
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.so  
libshowpoint.so 


你可能感兴趣的:(bug笔记)