0

I'm trying to compile the OpenCV 4.6.0 using cMake 3.23 and Visual studio 2022 and I need to make sure that it will not depend on VC++ runtime DLLs.
The problem is no matter what I do, the opencv_world460.dll will end up dependent on VCRUNTIME140.DLL and VCRUNTIME140_1.DLL.
Please note that I'm using Visual Studio 2022 which has the version number 17, but the compiled dll is dependent on version 14 runtimes. Why?
I have also unchecked the BUILD_WITH_STATIC_CRT and the result is still the same.
So how can I make sure my DLL is not dependent on any VCRUNTIMExxx.DLL?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Sam
  • 2,473
  • 3
  • 18
  • 29
  • 2
    The numbers are just confusing. Version 17 is for the IDE, Visual Studio. The "platform toolkit" is still at version 14. – BoP Aug 12 '22 at 17:34
  • What you are trying to do is a full statically linked application. I am not sure MSVC will even allow this any more. The fact is the compiler is going to always use the libraries provided in the runtime. You are just trying to bundle it with your application code instead of having to install it separately or expect it to be present on target systems. I assume? – Mikel F Aug 12 '22 at 17:37
  • @MikelF MSVC allows it by setting **Runtime Library** in **Properties >> Configuration Properties >> C/C++ >> Code Generation** to **/MT** for Release and **/MTd** for debug. But I want to force this setting from the CMake. – Sam Aug 12 '22 at 18:20
  • Does this answer your question? [Compile with /MT instead of /MD using CMake](https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake) – Mikel F Aug 12 '22 at 18:34
  • @MikelF Had already tried it, did not work. – Sam Aug 12 '22 at 18:37
  • ***Please note that I'm using Visual Studio 2022 which has the version number 17, but the compiled dll is dependent on version 14 runtimes. Why?*** All versions of VS from 2015 through 2022 share the same redistributable and have binary compatibility: [https://learn.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017?view=msvc-170](https://learn.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017?view=msvc-170) – drescherjm Aug 12 '22 at 19:35
  • ***Had already tried it, did not work*** When you edited the CMakeLists.txt for opencv did you try this option for each target and VS Configuration: [https://stackoverflow.com/a/70420052/487892](https://stackoverflow.com/a/70420052/487892) – drescherjm Aug 12 '22 at 19:39
  • @drescherjm at line 142 of CMakeLists.txt we have `project(OpenCV CXX C)` followed by `if(MSVC)` at line 144. I added `add_compile_options` inside that if statement. Not sure what you mean by "**each target and VS Configuration**". – Sam Aug 12 '22 at 19:59
  • The answer I linked said to `set_property(TARGET foo PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>")` that sets the runtime for the Debug only in the TARGET `foo` each add_executable() add_library() is a target you need to set for each target and each configuration Debug, Release ... The documentation for this is here: [https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html](https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html) – drescherjm Aug 12 '22 at 20:05
  • @drescherjm CMakeLists.txt for OpenCV 4.6 does not have any `add_executable()` or `add_library()` in it. – Sam Aug 12 '22 at 20:16
  • It has to somewhere it could use a CMake macro or function for that. Edit: It looks like the build system is pretty complex. – drescherjm Aug 12 '22 at 20:19

1 Answers1

0

The only solution that I found was to uncheck the BUILD_SHARED_LIBS and BUILD_opencv_world and created static .lib files which I then linked those libs into my app.
keep in mind using this method because you don't create the world package, you will have to link many libs like below. All of these files will be in \opencv-4.6.0\Build\install\x64\vc17\staticlib folder.

#if defined(_DEBUG)
#pragma comment(lib, "opencv_core460d")
#pragma comment(lib, "opencv_imgproc460d")
#pragma comment(lib, "opencv_highgui460d")
#pragma comment(lib, "opencv_ml460d")
#pragma comment(lib, "opencv_video460d")
#pragma comment(lib, "opencv_features2d460d")
#pragma comment(lib, "opencv_calib3d460d")
#pragma comment(lib, "opencv_objdetect460d")
#pragma comment(lib, "opencv_imgcodecs460d")
#pragma comment(lib, "opencv_flann460d")
#pragma comment(lib, "aded")
#pragma comment(lib, "IlmImfd")
#pragma comment(lib, "ippiwd")
#pragma comment(lib, "ittnotifyd")
#pragma comment(lib, "libjpeg-turbod")
#pragma comment(lib, "libopenjp2d")
#pragma comment(lib, "libpngd")
#pragma comment(lib, "libprotobufd")
#pragma comment(lib, "libtiffd")
#pragma comment(lib, "libwebpd")
#pragma comment(lib, "quircd")
#pragma comment(lib, "zlibd")
#else
#pragma comment(lib, "opencv_core460")
#pragma comment(lib, "opencv_imgproc460")
#pragma comment(lib, "opencv_highgui460")
#pragma comment(lib, "opencv_ml460")
#pragma comment(lib, "opencv_video460")
#pragma comment(lib, "opencv_features2d460")
#pragma comment(lib, "opencv_calib3d460")
#pragma comment(lib, "opencv_objdetect460")
#pragma comment(lib, "opencv_imgcodecs460")
#pragma comment(lib, "opencv_flann460")
#pragma comment(lib, "ade")
#pragma comment(lib, "IlmImf")
#pragma comment(lib, "ippiw")
#pragma comment(lib, "ittnotify")
#pragma comment(lib, "libjpeg-turbo")
#pragma comment(lib, "libopenjp2")
#pragma comment(lib, "libpng")
#pragma comment(lib, "libprotobuf")
#pragma comment(lib, "libtiff")
#pragma comment(lib, "libwebp")
#pragma comment(lib, "quirc")
#pragma comment(lib, "zlib")
#endif
#pragma comment(lib, "ippicvmt")
Sam
  • 2,473
  • 3
  • 18
  • 29