3

I am new with visual studio, opencv.

I am using visual studio 2013, opencv and c++ for my project.

I configured (copied path) the opencv and other library to my computer environment system.

After run the project in visual studio, normally, there will be an exe file in the project.

I can copy the exe file in the project folder and copy to other place in my computer and it will run normally.

This is because my computer environment systems are configured with opencv and other library.

I want to do the same thing with other computers BUT I do not want to manually configure each computer with opencv and other libraries.

Are there any ways that I can do to link everythings all in exe file after run the project in visual studio 2013 so that I can run the exe without depend on the path of libaries and opencv?

EDITED

I use opencv installer opencv2.4.7.exe

In the current VS2013 my project, i configured my project and opencv installer as this link http://www.anlak.com/using-opencv-2-4-x-with-visual-studio-2010-tutorial/

question : Can i use the library in folder C:\opencv\build\x64\vc11\staticlib come from the opencv installer no need create my own library from source opencv?

question : In case i need to generate new library from opencv source (http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html) or use lib in static folder of opencv installer, if i want to include it all to exe files, do i need to create new project and reconfigure?

Thank you.

sayvortana
  • 825
  • 2
  • 16
  • 32

1 Answers1

0

First you need to rebuild openCV to generate static libraries instead of dynamically linked ones. This way all code that your application uses is thrown together in one single exe-file (which will probably be significantly bigger). This exe-file you can move to other computers and they should still work there, provided they have an architecture that is at least compatible with yours. So if you build it on an x86 perconal computer (32-bit), it should basically work on any other personal computer. If you build it on a x64 computer (AMD 64-bit), it will only run on other x64 machines. At least this is true assuming both systems use the same syscall API (Windows NT, POSIX...).

For openCV you do this by setting the BUILD_SHARED_LIBS build flag to false (see OpenCV as a static library (cmake options), the following line is taken from there):

cmake -DBUILD_SHARED_LIBS=OFF ..

Once you have done this, you will see that the openCV folder looks very similar to the one you have now, except that in your 'lib' folder there will now be .lib-files instead of .dll files (at least if you are working on Windows, which I assume you do since you are using Visual Studio).

Next step is to go to your project settings and set your linker to link with the static libraries instead of the dynamically ones. If you have used openCV's local method for linking, you can go to project settings -> linker -> input -> Addtional dependencies. There you change the extension of all the openCV libraries from .dll to .lib.

Now you should be able to rebuild your application and the resulting exe-file should have all dependent libraries contained in it.

Community
  • 1
  • 1
bverhagen
  • 99
  • 4
  • Thank @bverhagen, How about other other dll files ? for instance i used PThreads dll so i just copied path of the dll files n put in the computer environments path. – sayvortana Feb 13 '14 at 14:51
  • The same reasoning applies to all dynamically linked libraries you use and may not be available on the other system: you need to link with the static library, so at least the relevant parts of the library are in your exe. BTW: you cannot convert dynamic libraries to static ones (or this is hard at the least). So for all dynamic libraries you use you should generate their static counterparts. Your best guess is using the tool which generated your dll-file and set it to generate static libraries. For system dll's this is not necessary, they should be available on the other system anyway. – bverhagen Feb 14 '14 at 09:35
  • **could you describe the steps from scratch ?** I am new with cmake, opencv and c++ ... All i know is in vs2013 after we compile the project we could get the exe file. i download opencv2.4.7.2.exe i install it and put in c:/opencv and when i create the project in vs2013 i just configure the path of linker and dependencies. – sayvortana Feb 20 '14 at 12:41
  • Oh, I see your problem: you use the pre-built libraries installer for openCV? I am sorry, I am using Linux and there you have no choice but to build if from source (using the command I just gave you), so I did not consider that as an option. This is what you should do: follow the 'Installation by Making Your Own Libraries from the Source Files' from http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html. Skip all the 3rd party instructions. In step 7 uncheck 'BUILD_SHARED_LIBS'. Then you should get your lib-files to which you should link in visual studio. – bverhagen Feb 20 '14 at 13:49
  • You might want to check first whether there are already lib-files in the current c:/opencv folder (perhaps they are already present in the pre-built ones), in which case you should simply link to them in stead of the dll files (shared libraries are preferred over static ones when linking) but I seriously doubt it. If this is the case, add /c to your build options and manually link the files using the 'lib' program using command prompt (http://msdn.microsoft.com/en-us/library/ms235627.aspx) – bverhagen Feb 20 '14 at 13:55
  • Oh I read the tutorial now i installed the cMake in window but I dont understand that after step 7 we got an opencv vsproject. And then how can i make it link with my project so that i can include all in executable file? do i need to include in dependancies and linkers? one more thing the below section of the tutorials "Set the OpenCV enviroment variable and add it to the systems path" we need to set system path. so when we copy our exe files to others do they need to set system path too?? – sayvortana Feb 20 '14 at 13:56
  • in c:/opencv after installed opencv2.4.7.2 there are two main folders "build" and "sources" I notices that in the build folder they have folder and file created by cmake OpenCVConfig.cmake OpenCVConfig-version.cmake – sayvortana Feb 20 '14 at 13:59
  • in the build folder of the opencv installer i saw folder static library : C:\opencv\build\x64\vc11\staticlib is it the lib files you mentioned??? – sayvortana Feb 20 '14 at 14:06
  • The vsproject part is the same as you did before with the dynamic libraries, only now you need to point to the lib-files in stead of the dll files. The opencv environment variable is only there to make life easier and enhance portability _of the project_ (not of your executable!). You can set absolute paths in stead if you like. You need to set this path so the linker can find your libraries for linking: it will 'copy' those into your exe, so you will not need them after the exe is generated. The lib filenames I mention should be like opencv_core.lib. Is such a file in the folder you mention? – bverhagen Feb 20 '14 at 15:20
  • Cmake files are the files giving instructions to cmake on how to build your project. So they are not created by cmake, but interpreted by it. Cmake will generate Makefiles (or similar, do not know for sure on a Windows system), which will in turn generate your libraries. – bverhagen Feb 20 '14 at 15:23
  • as you said i need to point to lib files instead of the dll files. In the current VS2013 my project, i configured my project and opencv installer as this link http://www.anlak.com/using-opencv-2-4-x-with-visual-studio-2010-tutorial/ question 1: Can i use the library in folder C:\opencv\build\x64\vc11\staticlib come from the opencv installer no need create my own library from source opencv? question 2: In case i need to generate new library or use lib in static folder of opencv installer, if i want to include it all to exe files, do i need to create new project and reconfigure? – sayvortana Feb 20 '14 at 15:41
  • 1) Yes, if the lib-files in that article are indeed in C:\opencv\build\x64\vc11\staticlib, then you won't need to build your own. I used the vc11 libs with VS2012 a long time ago and that worked, so it will probably work with VS2013 too (otherwise you wil have to build them using the VS2013 compiler) 2) Whether you build them on your own or not is irrelevant: simply change your projects' settings as described on the _Under “Common Properties” -> “Linker” -> “General”, edit “Additional Library Directories” and add_ line in the article you mentioned. Just clean and rebuild your current project. – bverhagen Feb 20 '14 at 15:48
  • My current project the settings for linker is point to C:\opencv\build\x64\vc11\lib so if I changed it to C:\opencv\build\x64\vc11\staticlib Do i need to change or delete the additional dependencies of C/C++ -> General?? – sayvortana Feb 20 '14 at 15:57
  • No, you do not have to change your include folder. You should also check _Common Properties” -> “Linker” -> “Input”, edit “Additional Dependencies”_: if the library files in their have the dll-extension, you should change them to .lib. If they have no extension or are already .lib (that last one would be weird, but still) you should be fine. You can now clean and rebuild your project and then you should get your executable with static libraries. You can check using ldd in Cygwin (if you have this) or use Dependency Walker. You could also just test it on another system of course. – bverhagen Feb 20 '14 at 16:10
  • *"change the extension of all the openCV libraries from .dll to .lib"* those are entirely different file types with entirely different contents, for different purposes. that doesn't work. – Christoph Rackwitz Nov 23 '22 at 12:23