Static linking with vcpkg installation

mjan
Fri, 24 Oct 2025 09:02:57 +0000
C++

Hi,

this might be one of those beginner questions you guys have seen alot already and you’re probably thinking: “why didn’t the guy just use the search function”, but I did and I couldn’t find a topic regarding both vcpkg and linking. So here we go:

I installed vcpkg by using:

git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat

and then integrated it into VS2022 by executing:

.\vcpkg integrate install

After that I installed opencv 4.11.0#4 by executing:

.\vcpkg install opencv4

Then in VS2022 I made a new project and tried(from now on called “standard include”):

#include <opencv2/core.hpp>

Didn’t work. Why? Because vcpkg is using a different folder structure:

.\vcpkg\packages\opencv4_x64-windows\include\opencv4\opencv2\core.hpp

Therefore, it would only recognize my include if I did:

#include <opencv4/opencv2/core.hpp>

But the linker then couldn’t find the files included in the core.hpp because there the includes are all:

#include <opencv2/anotherheader.hpp>

Now in Additional Include Directories I added:

.\vcpkg\packages\opencv4_x64-windows\include\opencv4\opencv2

That made it possible for me to use the “standard include” again and compilation took place as expected. Folder had an executable and all dlls needed. Now unfortunately I need to use also another package aside of opencv and I need to link it statically, since my customer has a custom version of that package and doesn’t want anyone to copy their alternated version of this package to another machine, or even replace the dll with the unaltered one. Therefore I set the vcpkg option in VS2022 to use the static library instead. As expected, this didn’t work, because the linker couldn’t find the static libraries. So I installed opencv4 again, but this time I used the triplet for static:

.\vcpkg install opencv4:x64-windows-static

Now I have a new problem with the linker:

LINK2038: mismatch detected for ‘RuntimeLibrary’: value ‘MT_StaticRelease’ doesn’t match value ‘MD_DynamicRelease’ in myProject.obj

Is there anything I did wrong? I even changed the Additional Include Directories to:

.\vcpkg\packages\opencv4_x64-windows-static\include\opencv4\opencv2

Is there anyone who has dealt with this before?

1 post - 1 participant