LNK2005 already defined error after update OpenCV

Hello everyone,

I am working on a 32 bit project that uses OpenCV in its 32 bit version (compiled by me).

The fact is, that the project was compiling fine, but I’ve decided to update OpenCV from version 3.4.3 to version 4.5.1 to get some dnn features that the previously version lacks.

After update the libraries and dependencies, now I got the error:

error LNK2005: "public: __thiscall cv::KeyPoint::KeyPoint(class cv::KeyPoint const &)" (??0KeyPoint@cv@@QAE@ABV01@@Z) already defined in opencv_world451.lib(opencv_world451.dll)

I’ve checked my code, and I am obviously not defining KeyPoint. Is there anything else I must do when updating from 3.4.3 to 4.5.x?

I’ve scanned internet searching for this problem, and I can only find two related themes:

  1. Runtime Library missmatch between my project and the OpenCV compiled. I’ve reviewed the CMake project generated and used for compile the OpenCV project, and it has the option “Multi-threaded DLL (/MD)” as Runtime library, and my project has exactly the same option.

  2. Set “Force File Output” Linker option to /FORCE. This “solves” the problem and I am able to compile my project, but I am worried about if there is really a problem, or is just the linker throwing random errors, because as I said, with version 3.4.3 of OpenCV I got no errors before.

Thank you very much, any answer will be welcome!

clean the build directory.

it looks like there are still old object files there.

Hello @crackwitz,

Thanks for the answer. I’ve already cleaned before the project, but after your answer I’ve used the “Clean solution” and compiled again but the problem persists.

The point is that I can’t track where the problem is coming from. I have a file called “pattern.cpp” and the problems comes from there, if I comment the whole file I don’t get that linker error compiling.

I’ve commented the whole file and started to uncomment functions till I get the error, and the error appears when I uncomment:

std::vector<std::vector<cv::Point>> templateContourCorner = templateContourW;

templateContourW is an input parameter of the class function, but I don’t get how can this be related with “KeyPoints” class.

Do you know how can I track better the problem? The linker error message it’s very confussing, because I can’t double click on it to visualize the problem and the line where I am getting the error isn’t related with KeyPoints…

Thank you.

what did you do, exactly ? can you show us the list of (opencv) libs you link to ?

Hello @berak, thank you for your answer,

My project (lets say A) has another project (lets say B) inside as dependency.

B is configured as “Static Library (.lib)” and by itself compiles fine. It only has the “Additional Include Directories” with the path to OPENCV_DIR\install\include.

A, which has B as dependency, is the project that fails compiling. It is configured as “Dynamic Library (.dll)” and has the next OpenCV dependencies configured:

  • C/C++: General → Additional Include Directories (path to OPENCV_DIR\install\include)
  • VC++ Directories: Library Directories (path to OPENCV_DIR\install\lib)
  • Linker: General → Additional Library Directories (path to OPENCV_DIR\install\lib)
  • Linker: Input → Additional Dependencies (opencv_world451.lib)

For the update, I’ve just changed the OPENCV_DIR from the 3.4.3 version to point the 4.5.1 directory, and changed opencv_world343.lib to opencv_world451.lib.

Thank you.

Didn’t you deleted 3.4.3? It looks liked you are running opencv 3.4.3.

Hello @Supra,

Following your comment, I’ve deleted all the local files regarding 3.4.3 and erased every track from path/environment variables. Still get the same error.

If I Ctrl+Click on a OpenCV include, I open the file and the path is:


So I think I am running 4.5.1.

Thank you.

You didn’t mention VS2019… As @berak can help you to solve problem. I am off during covid-19 pandemic.

Hello everyone again,

I’ve tested that the problem is really by just declaring and using KeyPoint.

For example, I commented all the code in my .cpp file that uses cv::KeyPoint, and uncommented that function:

std::vector<cv::KeyPoint> Pattern::templateKp()
    std::vector<cv::KeyPoint> points;
    for (size_t i = 0; i < _TemplateKp.size(); ++i)
	   cv::KeyPoint kp = _TemplateKp[i];
	   kp.pt /= _Scale;
    return points;

Now the linker throws the error, but if I comment the part inside the for, the error disappears.

I’ve also tested to declare a cv::KeyPoint with random initializing parameters (eg. Point2f(3.0, 2.0) and size 3.0), in another file that doesn’t contain any KeyPoint, and after put the KeyPoint it fails again, so the problem for some reason is the KeyPoint, but I can’t figure why…

With the 3.4.3 OpenCV version I have no problems. And I’ve checked a lot of times that I am using the 4.5.1 version correctly…

Thanks a lot.

try adding

#include <opencv2/core/types.hpp>

that contains cv::KeyPoint

I’m not sure whether that header is implied by other includes already or it needs to be included explicitly.

Hi @crackwitz,

Thank you for your answer.

I’ve tried to add the include you mentioned, but nothing, the problem persists.

I have a test project that I am using to test the dnn that I am developing, and I am using the same OpenCV version (4.5.1) installed in the same computer with same environments variables, and it works fine in that project.

I can use cv::KeyPoint and the linker doesn’t tells me that there is a problem. The difference between these projects, is whereas the project failing has a dependency on a submodule in which the KeyPoint are being used, the other project is alone and has no dependency and no one depends on it.

I am thinking that the problem is there, for some reason that dependency is causing the error, but I wonder why the problem just appears when using 4.5.x and didn’t appear when using 3.4.3.

has the other project been recompiled? has its build directory been cleaned before a clean build?

Yes, I’ve cleaned the build directory and rebuilded everything, but in the other project I am not able to reproduce the error I am facing in the main project.

a complete (or parts of a) log of the build would be helpful. the single line in the first post gives no context.

there’s something going on that we haven’t seen yet.

Hello @crackwitz,

I’ve been able to reproduce the error in another project. I am preparing the project to upload here so we can check it out properly.

Hello @crackwitz,

I’ve found the error. After go function by function commenting lines, it results that the error was caused by:

#pragma once

//Some includes...

#pragma unmanaged
#include <opencv2/opencv.hpp>
#pragma managed


After remove the #pragma unmanaged / #pragma managed, now the code compiles fine, but it throws the following warnings:

what is #pragma unmanaged?

is this some .NET stuff?

I am not gonna play 20 questions. there is a lot more going on here than you’re telling.

Hello @crackwitz,

The project is a big project that I started working on, and for requirements I’ve updated OpenCV from 3.4.3 to 4.5.1.

After the update, the error that I’ve have been pointing in the main message of the post appeared, and after a lot of time trying to figure out where the error came from, I finally detected that “pragma managed/unmanaged” in my header files was causing the error.

After erase all from the header files, now compiles fine but throws these warnings.

About your question managed-unmanaged is used in .NET to differentiate between managed code (like Java, it self-manages the memory) and not managed code (in this case, OpenCV library).

Sorry if I am not being clear about something, I really don’t understand why this is happening after update OpenCV 3.4.3 to 4.5.x.