OpenCV library linking issues in Visual Studio

I am trying to link opencv to my c++ project (shared library) in Visual Studio. I have tried using both precompiled headers for WIndows as well as tried compiling myself from the source. But I get the same error.

I have compiled OpenCV binaries with GCC using this tutorial (OpenCV C++ installation on Windows with MinGW | by RAJAT KANTI Bhattacharjee | csmadeeasy | Medium) and linked the directories and files under the project settings such as C/C++ > Additional Include Directories as well as under Linker > Input > Additional Directories with no luck. My code is able to recognize OpenCV variables however, I believe I am not just linking libraries right.

The error output I get is:
1>Target Link:
1> C:\Microsoft\AndroidNDK64\android-ndk-r16b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin\ld: error: C:\Program_Files\Release\install\x64\mingw\bin\libopencv_features2d3414.dll:1:3: invalid character
1> C:\Microsoft\AndroidNDK64\android-ndk-r16b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin\ld: error: C:\Program_Files\Release\install\x64\mingw\bin\libopencv_features2d3414.dll:1:3: syntax error, unexpected $end
1> C:\Microsoft\AndroidNDK64\android-ndk-r16b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin\ld: error: C:\Program_Files\Release\install\x64\mingw\bin\libopencv_features2d3414.dll: not an object or archive
1> undefined reference to ‘cv::Mat::Mat(int, int, int)’
1> undefined reference to ‘cv::Mat::~Mat()’
1> undefined reference to ‘cv::ORB::create(int, float, int, int, int, int, int, int, int)’
1> undefined reference to ‘cv::Mat::Mat()’
1> undefined reference to ‘cv::Mat::Mat()’
1> undefined reference to ‘cv::Mat::~Mat()’
1> undefined reference to ‘cv::Mat::~Mat()’
1> clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)
1> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Application Type\Android\3.0\Android.Common.targets(119,5): error MSB6006: “clang.exe” exited with code 1.
1>Done building target “Link” in project “TestSharedLibrary.vcxproj” – FAILED.

I am using Debug configuration and have tried with various Platforms (x64, ARM, ARM64) where I get the same error but for Platform x86 the error i get is:

1>Target Link:
1> undefined reference to ‘cv::ORB::create(int, float, int, int, int, int, int, int, int)’
1> undefined reference to ‘cv::fastFree(void*)’
1> undefined reference to ‘cv::Mat::create(int, int const*, int)’
1> undefined reference to ‘cv::Mat::deallocate()’
1> clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)
1> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Application Type\Android\3.0\Android.Common.targets(119,5): error MSB6006: “clang.exe” exited with code 1.
1>Done building target “Link” in project “TestSharedLibrary.vcxproj” – FAILED.

How can I resolve this linking issue?

what exactly are you trying to build ? a desktop app ? something for android ?

why use gcc in VisualStudio (which comes with it’ s own compiler toolchain) ?

please do not try to mix things, that don’t belong together.

I am creating a shared library in .so format that works in Unity. I attempted doing that in Eclipse in Ubuntu but the generated library didn’t work for me in Unity. It does work when I develop a basic shared library in Visual Studio (C++ Mobile Development) which is why I am using Windows

that’s for pc/desktop. you cannot use the libs on android

you try to LINK a dll (wrong) where a .lib .a or dll.a file is expected

we cannot help with it, there is no support from opencv for this

I just tried linking dll.a files (generated from compiling opencv myself) as well and i get the same first error as mentioned in my question.

and by linking opencv_world343d.lib from my prebuilt opencv gives this error:

1>Target Link:
1> undefined reference to ‘cv::Mat::Mat(int, int, int)’
1> undefined reference to ‘cv::Mat::~Mat()’
1> undefined reference to ‘cv::ORB::create(int, float, int, int, int, int, int, int, int)’
1> undefined reference to ‘cv::Mat::Mat()’
1> undefined reference to ‘cv::Mat::Mat()’
1> undefined reference to ‘cv::Mat::~Mat()’
1> undefined reference to ‘cv::Mat::~Mat()’
1> clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)

Should I be recompiling OpenCV using a different compiler to make it compatible for my project?

that’s also for desktop, and the cl compiler. clang cannot use it

also note your version mix, 343, 3414, don’t do that

Do you know which ones can clang use though? Sorry if it seems pretty generic question, I am pretty new at this

you have to stick to the very same toolchain from start to end, you must not mix outputs from different compilers. (which is the correct one, depends on the platform you want to run it later. )

maybe pick a lower hanging fruit, than going through this build hell

I shouldve mentioned the compiler Visual Studio can be switched between clang and gcc. Either way I get the same errors.

Thats what I have done in terms of compiling. Compile OpenCV with gcc, Linked and included following generated files in Visual Studio (using gcc compiler).

Included opencv files from here C:\Program_Files\Release\install\include
Linked dll.a files from here C:\Program_Files\Release\install\x64\mingw\lib

From what you said, this should work since I have stuck to the same compiler so far. The error I get are the same though.

1>Target Link:
1> c:/microsoft/androidndk64/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/…/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin/ld.exe: error: C:\Program_Files\Release\install\x64\mingw\lib\libopencv_core3414.dll.a: member at 1469670 is not an ELF object
1> c:/microsoft/androidndk64/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/…/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin/ld.exe: error: C:\Program_Files\Release\install\x64\mingw\lib\libopencv_core3414.dll.a: member at 1482796 is not an ELF object
1> c:/microsoft/androidndk64/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/…/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin/ld.exe: error: C:\Program_Files\Release\install\x64\mingw\lib\libopencv_core3414.dll.a: member at 1484610 is not an ELF object
1> c:/microsoft/androidndk64/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/…/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin/ld.exe: error: C:\Program_Files\Release\install\x64\mingw\lib\libopencv_features2d3414.dll.a: member at 339412 is not an ELF object
1> undefined reference to ‘cv::Mat::Mat(int, int, int)’
1> undefined reference to ‘cv::ORB::create(int, float, int, int, int, int, int, int, int)’
1> undefined reference to ‘cv::Mat::Mat()’
1> undefined reference to ‘cv::Mat::Mat()’
1> undefined reference to ‘cv::Mat::~Mat()’
1> undefined reference to ‘cv::Mat::~Mat()’
1> collect2.exe: error: ld returned 1 exit status
1> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Application Type\Android\3.0\Android.Common.targets(119,5): error MSB6006: “arm-linux-androideabi-gcc.exe” exited with code 1.

In theory this should be working.

no, desktop gcc and android gcc are NOT THE SAME

Okay that actually makes sense. I will compile opencv in ubuntu and port it to windows then. Hope this works.

FWIW, I’m having the same problem. I made sure to use the android sdk include folders from the prebuilt binary of 4.5.2, tried with numerous android NDKs, GNU static library, GNU c++ 11.0, and I get errors out the wazoo from the OpenCV include folder. I was able to successfully build a windows dll plugin for openCV, but when I start with an android shared library solution and use the above configuration (following numerous tutorials online), it’s no good.

I am starting to think that the precompiled downloadable Android SDK is just impossible to compile as a dynamic shared library from Visual Studio 2019 and the only way is to do my homework and build from source. Is that right?