This may be obvious, but I want to make sure what to do before I do anything rash. I want to compile my C++ program, libraries and all, to a release executable such that the file can be run on any computer (running the same OS). Right now, I’m on Mac OS X (10.7.4) and I need to be able to run my executable on other Macs. The problem is I am using the OpenCV library in my project, and I only have it installed on this computer. Is there a way to compile with g++ such that if I open this program on a computer that doesn’t have the OpenCV library installed, it will work anyway? As in, build all the dependencies into the executable. Or does this happen automatically?
I am also quite new to the “.o” object files, so can those have anything to do with it? I would prefer a way to get it all into a single file, but I’ll settle for a package as long as it works.
Thank you.
There are two ways to do this. You can static link if you aren’t going to run into licensing issues with any of the libraries you are linking to. This is pretty easily handled by using
g++ -o myApp -static -lopencv myapp.cppHowever, this also depends on static libraries existing for the libraries you want to link to. Most distribute static libs with the shared libs these days.The other way is to distribute the shared libraries and tell your application to force it to look in a certain spot for the shared library using
-rpath. Note: I am telling you the Linux way to do this, it will probably work on a Mac but I have no way to test.So say all of your shared libraries are in the same directory as your executable, you can compile with:
g++ -rpath ./ -lopencv -o YourApp yourApp.cppI hope this helps.