3

My end goal is to have a portable Opencv program that I can easily install and use on other Windows computers. I have successfully written the program using codeblocks and Mingw.

Currently I have a functioning program that uses OpenCV's dynamic linked libraries. But when I move the executable anywhere, it can't find the dlls.

I have attempted to use static libraries so that I don't have to install Opencv on the computers that will be running the program, and everything can run from the executable. The following are the steps I took.

  1. Compile Static Libraries using CMAKE, Opencv source, and Mingw64 using the BUILD_SHARED_LIB = OFF. This gives me a series libopencv_xxxxx.a (I saw a similar problem using opencv and qt where the compiler flag was adding 'lib' to the front of these names causing an error - not the case in codeblocks.)
  2. Set the Codeblocks build options as follows:

enter image description here

Linker Serach Path

Linker Libraries

When I build this, I get a series of "undefined reference to 'xxxxx'" errors.

e.g.

Build Messages

C:\opencv\build\x64\mingw (static)\staticlib\libopencv_core2410.a(persistence.cpp.obj):persistence.cpp:(.text$_ZL12icvCloseFileP13CvFileStorage+0x5a)||undefined reference to `gzclose'|

Build Log

C:\opencv\build\x64\mingw (static)\staticlib\libopencv_core2410.a(persistence.cpp.obj):persistence.cpp:(.text$_ZL12icvCloseFileP13CvFileStorage+0x5a): undefined reference to `gzclose'

If you have any advice for these errors, it would be greatly appreciated.

I have also been thinking that it is possible that I am going about this all wrong (I have a degree in electrical engineering, so I was exposed to a lot of programming; however, I may have missed out on some basic software engineering components.)

Here are some other things I have considered:

  1. Using Visual Studios - The libraries for opencv are precompiled and also the target computers run windows so this might seem logical - I experimented a little bit and became frustrated with Visual Studios. I am becoming a Linux enthusiast and do not want to waste time learning windows specific programming.

  2. Use Visual Studios Compiler in Codeblocks - The only option in Codeblocks is to use Visual C++ 2010 - I can't seem to get to get VC11 or VC12 to be used in CodeBlocks. Also, I see Visual C++ installed, but can't seem to locate its folder. (In Summary, I tried a few things that didn't yield immediate positive results)

  3. Use an installer - I don't have experience with installers, but perhaps it is easier than trying to link libraries into an exectuatble. I am under the impression that one can set up an installer to store all dependencies in Program Files and put a shortcut to the executable in a convenient place. Is this the case, and what degree of difficulty is involved?

  4. Manually organize a portable folder with dependencies. Not much experience with this, but again, maybe easier than linking libraries.

Please feel free to comment on the other options or provide any solution that I may be missing.

Langleson
  • 103
  • 1
  • 13
  • 1
    possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Captain Obvlious Apr 03 '15 at 21:33
  • 1
    Check if this solves your problem: http://stackoverflow.com/questions/8259441/undefined-reference-to-gzopen-in-moses (Basically, adding -lz to your linker options). Also [this](http://stackoverflow.com/questions/9700414/compilation-problems-with-zlib) might be relevant. – Antonio Apr 04 '15 at 20:57
  • if you're linking statically, you also need to link a couple of helper libs, like : `-lrt -ldl -lz -lpthread` . this is the same on linux, too ! (your error complains about missing zlib [-lz]) – berak Jul 27 '15 at 05:36

2 Answers2

1

Set the compiler as the one you use for building OpenCV static library maybe the solution.

For example, I build OpenCV with MinGW 4.7.1, then I should set compiler in Code Blocks to the same compiler (MinGW 4.7.1).

James Lai
  • 11
  • 1
1

I had problems too getting a stand-alone .exe file with the combination of OpenCV3.1.0, CMake3.4.2, MinGW5.3.0 using code:blocks. As I am relatively new to OpenCV and c++ in general, the solution is maybe more complicated than it has to be, but at least for me it works. My solution worked as follows:

  1. Make static libraries, as you did it, with BUILD_SHARED_LIB = OFF, getting the .a static files (and not the .dll.a-files). Cmake did make a my_static_build\install\ directory, so the folders could change when setting another CMAKE_INSTALL_PREFIX.

  2. Code:blocks: Using gcc and g++ as compiler; Changing Project -> Build options (not Settings -> Compiler, but probably doesn't matter):

    2.1 Compiler settings -> Compiler Flags: Have g++ follow the C++11 ISO C++ language standard; Optimize fully (for speed)

    2.2 Linker settings -> Link libraries: I added all .a files of the directories: C:\OpenCV\my_static_build\lib and C:\OpenCV\my_static_build\install\x86\mingw\staticlib. These are around 90 .a-files.

    2.3 Linker settings -> Other linker options: -static -static-libstdc++

    2.4 Search directories -> Compiler: C:\OpenCV\my_static_build\install\include

    2.5 Search directories -> Linker: C:\OpenCV\my_static_build\lib

Phann
  • 1,283
  • 16
  • 25