mpals
mpals

Reputation: 121

Linking OpenCV static libraries in Eclipse Windows

I want to create an executable that I can move to another computer that does not have OpenCV installed.

As such I am trying to statically link all the libraries needed into the executable (Thats what its called right?). The program compiles just fine and works on my local computer but when I copy it to another computer it complains that it is missing .dll files and won`t execute.

I am using eclipse juno with mingw as compiler on windows 7.

My progress so far: I have included the libraries needed from opencv\build\x86\mingw\lib into the MinGW C++ linker -> libraries in the project properties.

opencv_core244.dll
opencv_highgui244.dll
opencv_imgproc244.dll

In the original folder these are called:

libopencv_core244.dll.a
libopencv_highgui244.dll.a
libopencv_imgproc244.dll.a

I have set the linker flag in MinGW C++ Linker -> miscellaneous to -static.

I have been searching a lot for answers and have tried a few different things but I`m really stumped by this.

How do you force the compiler in eclipse (MinGW in this case) to link the libraries as static libraries and not as dynamic libraries as it is apparently doing?

Upvotes: 0

Views: 2266

Answers (2)

blkhatpersian
blkhatpersian

Reputation: 437

This is almost a year late with the clarification/answer, but hope it can be some use to you or anyone else who comes across this page with the same problem.

While Pruthviraj didn't clearly explain his answer, he had the right idea. By default, the config file for cmake has "BUILD_SHARED_LIBRARY" flag set to true. The .a files that were created simply redirected to the dll files and are in essence useless to statically link in a program.

The easiest way is to rebuild OpenCV with cmake, but make sure the flag "BUILD_SHARED_LIBRARY" is set to false. It is under the Build sub-category in cmake GUI if you are using that.

The new generated make files should produce static libraries only in the lib folder and should properly link opencv statically in your program. Hope it helps!

Upvotes: 1

Pruthviraj
Pruthviraj

Reputation: 131

1) If OpenCV has been compiled to be used as DLLs, then you can not link statically.

2) Remember when you compile the project for DLL output then you have a .lib file. This .lib file is not actually a static library with code. It is used by the compiler to know the DLL name for class/function definitions. This will need the DLL at runtime.

3) For static linking you need .lib file which compiled as static library. In such compilation there is only one output which is a .lib file. This .lib file can be used for static linking and the code from .lib file is added to your application.

4) I have just compiled one project with VStudio which is a DLL having only one function. I get a DLL and a .lib as output. The size of the .lib is 2kb.

5) When I compile the same project as static library, then I get only one output that is .lib file. Its size is 133kb.

Upvotes: 1

Related Questions