Opencv Build From Source Python binding problem

Hello @ cudawarped

Thank you for the answer.

As far as I understand, to get python bindings, C++ debug files and C++ release files in the same built before running cmake command we make the changes stated here to source files. Can you confirm?

For BUILD_SHARED_LIBS=OFF errors, even if I add the bin directory to Path environment variable I still get errors. In Visual studio. Should we change Properties->C/C+±>Code generation->Runtime Library? For Debug its default value is Multi-threaded Debug DLL (/MD) and for release its default value is Multi-threaded DLL (/MD).

Here are some of the errors I get from Visual Studio. The code works without errors with builts BUILD_SHARED_LIBS=ON.

1>Generating Code...
1>opencv_world480d.lib(matrix.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in Config.obj
1>opencv_world480d.lib(matrix.obj) : warning LNK4099: PDB 'opencv_world480d.pdb' was not found with 'opencv_world480d.lib(matrix.obj)' or at 'C:\Users\THEUSER\Desktop\01_vsprojects\Trackers\OpenCVTrackers_00\x64\Debug\opencv_world480d.pdb'; linking object as if no debug info
1>opencv_world480d.lib(convert.dispatch.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in Config.obj
1>opencv_world480d.lib(convert.dispatch.obj) : warning LNK4099: PDB 'opencv_world480d.pdb' was not found with 'opencv_world480d.lib(convert.dispatch.obj)' or at 'C:\Users\THEUSER\Desktop\01_vsprojects\Trackers\OpenCVTrackers_00\x64\Debug\opencv_world480d.pdb'; linking object as if no debug info
1>opencv_world480d.lib(loadsave.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in Config.obj
...
...
...
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_domain_createA_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_id_create_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_id_destroy_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_string_handle_createA_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_region_begin_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_region_end_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_task_begin_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_task_end_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_metadata_add_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_metadata_str_addA_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_relation_add_ptr__3_0
1>opencv_world480d.lib(trace.obj) : error LNK2001: unresolved external symbol __itt_api_version_ptr__3_0
...
...
...
1>opencv_world480d.lib(templmatch.obj) : error LNK2019: unresolved external symbol ippicviCrossCorrNorm_32f_C1R referenced in function "bool __cdecl cv::ipp_crossCorr(class cv::Mat const &,class cv::Mat const &,class cv::Mat &,bool)" (?ipp_crossCorr@cv@@YA_NAEBVMat@1@0AEAV21@_N@Z)
1>opencv_world480d.lib(templmatch.obj) : error LNK2019: unresolved external symbol ippicviCrossCorrNorm_8u32f_C1R referenced in function "bool __cdecl cv::ipp_crossCorr(class cv::Mat const &,class cv::Mat const &,class cv::Mat &,bool)" (?ipp_crossCorr@cv@@YA_NAEBVMat@1@0AEAV21@_N@Z)
1>opencv_world480d.lib(templmatch.obj) : error LNK2019: unresolved external symbol ippicviSqrDistanceNormGetBufferSize referenced in function "bool __cdecl cv::ipp_sqrDistance(class cv::Mat const &,class cv::Mat const &,class cv::Mat &)" (?ipp_sqrDistance@cv@@YA_NAEBVMat@1@0AEAV21@@Z)
1>opencv_world480d.lib(templmatch.obj) : error LNK2019: unresolved external symbol ippicviSqrDistanceNorm_32f_C1R referenced in function "bool __cdecl cv::ipp_sqrDistance(class cv::Mat const &,class cv::Mat const &,class cv::Mat &)" (?ipp_sqrDistance@cv@@YA_NAEBVMat@1@0AEAV21@@Z)
1>opencv_world480d.lib(templmatch.obj) : error LNK2019: unresolved external symbol ippicviSqrDistanceNorm_8u32f_C1R referenced in function "bool __cdecl cv::ipp_sqrDistance(class cv::Mat const &,class cv::Mat const &,class cv::Mat &)" (?ipp_sqrDistance@cv@@YA_NAEBVMat@1@0AEAV21@@Z)
1>opencv_world480d.lib(init.obj) : error LNK2019: unresolved external symbol "void __cdecl google::protobuf::ShutdownProtobufLibrary(void)" (?ShutdownProtobufLibrary@protobuf@google@@YAXXZ) referenced in function "public: __cdecl cv::dnn::dnn4_v20230620::`anonymous namespace'::ProtobufShutdown::~ProtobufShutdown(void)" (??1ProtobufShutdown@?A0x8580819d@dnn4_v20230620@dnn@cv@@QEAA@XZ)
1>opencv_world480d.lib(pooling_layer.cpp.obj) : error LNK2019: unresolved external symbol __std_max_element_1 referenced in function "signed char const * __cdecl __std_max_element<signed char const >(signed char const *,signed char const *)" (??$__std_max_element@$$CBC@@YAPEBCPEBC0@Z)
1>opencv_world480d.lib(permute_layer.obj) : error LNK2019: unresolved external symbol __std_find_trivial_8 referenced in function "unsigned __int64 * __cdecl __std_find_trivial<unsigned __int64,int>(unsigned __int64 *,unsigned __int64 *,int)" (??$__std_find_trivial@_KH@@YAPEA_KPEA_K0H@Z)
1>C:\Users\THEUSER\Desktop\01_vsprojects\Trackers\OpenCVTrackers_00\x64\Debug\OpenCVTrackers_00.exe : fatal error LNK1120: 675 unresolved externals
1>Done building project "OpenCVTrackers_00.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

No. By default you cannot generate python bindings for a Debug build which from my understanding is why you have two builds:

  1. Release build for python bindings
  2. Debug build for C++

Is this correct? If not you could just use a single Release build to generate both python bindings and C++ libraries.

The changes mentioned allow you to build a Debug build for python meaning that you can just work with a Debug build for both generating python bindings and C++ libs.

I am pretty sure that unless you specify

-DBUILD_WITH_STATIC_CRT=ON

OpenCV will be linked against the dynamic CRT. i.e. you need to specify Multi-threaded Debug DLL (/MD) in your project which links to OpenCV. You can check the CMake file to be sure

Why are you are changing the default shared library configuration as this is causing you lots of issues which if you left them as defaults you would not have?

Hello @ cudawarped

Thank you for the answer.

Ok, changing Properties->C/C+±>Code generation->Runtime Library was some action I saw when I googled. So what is the problem when you inspect the error messages? I can paste more?

Generating 2 builts is my strategy. According to your suggestion, I understand that 1 built is enough for python bindings, C++ debug and C++ release?

To follow your suggestion, I think I should follow the steps below?

  1. Unzip opencv source files (opencv and opencv contrib)
  2. Apply the changes here to opencv source files.
  3. Run the following cmake command to configure and generate Visual Studio Project

“C:\Program Files\CMake\bin\cmake.exe” -B"D:\openCV_from_source\build16" -H"D:\openCV_from_source\opencv-4.8.0" -DOPENCV_EXTRA_MODULES_PATH=“D:\openCV_from_source\opencv_contrib-4.8.0\modules” -G"Visual Studio 17 2022" -DBUILD_opencv_python3=ON -DPYTHON3_INCLUDE_DIR=C:/Users/THEUSER/.conda/envs/env3_10/include -DPYTHON3_LIBRARY=C:/Users/THEUSER/.conda/envs/env3_10/libs/python310.lib -DPYTHON3_EXECUTABLE=C:/Users/THEUSER/.conda/envs/env3_10/python.exe -DPYTHON3_NUMPY_INCLUDE_DIRS=C:/Users/THEUSER/.conda/envs/env3_10/Lib/site-packages/numpy/core/include -DPYTHON3_PACKAGES_PATH=C:/Users/THEUSER/.conda/envs/env3_10/Lib/site-packages -DBUILD_opencv_world=ON -DBUILD_SHARED_LIBS=OFF -DOPENCV_ENABLE_NONFREE=ON

  1. Open OpenCV solution file for Visual Studio.
  2. Select Debug, All Build, Install
  3. Select Release, All Build, Install
  4. Check Python bindings in PYTHON3_PACKAGES_PATH
  5. Check C++ Debug and Relase Files in build folder.

I am assuming from the error that you have changed Multi-threaded Debug DLL (/MDd) in your project to Multi-threaded Debug (/MTd). I am still unsure as to why you are building a static version of OpenCV.

Its possible but forget it for now. Just build your a seperate Release version for the python bindings and a Debug for your C++ library, and everything should work.

Hello @ cudawarped

Thank you for the answer.

The error logs I pasted belongs to a run using a built with default Properties->C/C+±>Code generation->Runtime Library values.