c++ – How to build OpenCV for static linking in Visual Studio on Windows


I’ve been spending the better part of a day trying to figure this out, but to no avail.
I’ve scoured the internet for documentation and tutorials, and I still can’t make it work.

I am able to build OpenCV just fine, but no matter what I do, when I try to build my project and statically link against generated .lib files, I always get unresolved external symbols from the linker.

Let me guide you through the process I followed, and maybe you can spot what I’m doing wrong.

  1. Clone the repository.
    I went straight to the github, and made sure to select a release tag before cloning https://github.com/opencv/opencv/tree/4.11.0
    I saw a tutorial on YouTube where they cloned opencv_contrib as well, so I grabbed that too; again, from a release tag https://github.com/opencv/opencv_contrib/tree/4.11.0

  2. I installed CMake 4.0.0 (the GUI version)
    For “where is the source”, I selected the root of the opencv repository.
    Then pressed “configure” to generate the initial set of CMake options.
    Selected Visual Studio 17 2022 and platform x64 on the prompt.
    After that, I made sure to set the following options:

    • BUILD_SHARED_LIBRARIES: FALSE
    • BUILD_WITH_STATIC_CRT: TRUE
    • OPENCV_EXTRA_MODULES_PATH: point to opencv_contrib/modules
    • BUILD_opencv_world: TRUE & FALSE (tried both options in separate builds)
  3. I then generated the OpenCV VS sln, selected options Debug and x64 and built the ALL_BUILD solution.

  4. When the build was done, Visual Studio generated the following directories: bin, lib and x64.
    Since I want to statically link my project against OpenCV, I only grab the lib directory, and paste it in MyProject/MyProject/OpenCV/lib.

  5. I also grab opencv/include (from the original repository) and paste it in MyProject/MyProject/OpenCV/include.

  6. I open my project sln, and set the following options in the solution configuration:

    • C/C++ > Preprocessor > Preprocessor Definitions: add OPENCV_STATIC
    • C/C++ > General > Additional Include Directories: point it to ./OpenCV/include
    • C/C++ > Code Generation > Runtime Library: Multi-threaded Debug (/MTd)
    • Linker > General > Additional Library Directories: point it to ./OpenCV/lib
    • Linker > Input > Additional Dependencies:
      • (when compiled with BUILD_opencv_world=True) opencv_world4110d.lib
      • (when compiled with BUILD_opencv_world=False) opencv_core4110d.lib, opencv_imgproc4110d.lib, opencv_imgcodecs4110d.lib, opencv_highgui4110d.lib

    note: I made sure configuration is Debug x64

  7. Saved the configuration and built MyProject.

As source code for the project I chose samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp

Whatever I do, the linker keeps saying it has unresolved external symbols. There are around 200 of them when building a world lib, and 400 when building with world off.

Some other things I have tried:

  • without opencv_contrib/modules (i.e. do not point CMake to the directory when generating)
  • WITH_IPP = False (I saw a few mentions of IPP in the logs, so I thought I’d give that a try)
  • tried adding all *.lib files in the lib directory to Additional Dependencies, just in case I missed any
  • tried installing OpenCV by building the INSTALL solution
  • double and triple checked the solution configuration and paths that point to OpenCV
  • generate with option OPENCV_GENERATE_PKGCONFIG = True (saw a comment mention that worked for them, but I think that flag is for linux builds)

I’m not sure what else to try at this point. If you see anything wrong with what I’m doing, or have a new suggestion, please let me know.

Thank you.

Edit: examples of log messages:

opencv_world4110d.lib(copy.obj) : error LNK2019: unresolved external symbol ippicviCopy_8u_C1R_L referenced in function "public: void __cdecl cv::Mat::copyTo(class cv::debug_build_guard::_OutputArray const &)const " (?copyTo@Mat@cv@@QEBAXAEBV_OutputArray@debug_build_guard@2@@Z)
opencv_world4110d.lib(color_yuv.dispatch.obj) : error LNK2001: unresolved external symbol ippicviCopy_8u_C1R_L
opencv_world4110d.lib(grfmt_png.obj) : error LNK2019: unresolved external symbol crc32 referenced in function "private: void __cdecl cv::PngEncoder::writeChunk(struct _iobuf *,char const *,unsigned char *,unsigned int)" (?writeChunk@PngEncoder@cv@@AEAAXPEAU_iobuf@@PEBDPEAEI@Z)
opencv_world4110d.lib(persistence.obj) : error LNK2019: unresolved external symbol gzputs referenced in function "public: virtual void __cdecl cv::FileStorage::Impl::puts(char const *)" (?puts@Impl@FileStorage@cv@@UEAAXPEBD@Z)

Leave a Reply

Your email address will not be published. Required fields are marked *