Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question about compiled program usage of external libraries

Tags:

c++

linux

ubuntu

I know this might be a very stupid question but I am new to compiled languages (my domain is mostly scripting languages like PHP, Python or JavaScript).

I am learning C++ for one project where it is the only language I can use.

I wrote a program in Ubuntu 10.10 and then compiled it. I can run the generated binary file from cmd like this and it works:

sudo ./compiled-program

But, I have used some external libraries in the program (OpenCV). Does that mean that all computers where I will run the program will have to have OpenCV installed? Or is OpenCV bundled inside the compiled binary file? Will it work on PCs without OpenCV installed?

like image 396
Richard Knop Avatar asked Dec 21 '22 21:12

Richard Knop


2 Answers

You should read a few things about libraries, and particularly what makes the difference between static and dynamic libraries. To quote the basic definitions so you get the point :

A static library, also known as an archive, consists of a set of routines which are copied into a target application by the compiler, linker, or binder, producing object files and a stand-alone executable file.

[...]

Dynamic linking involves loading the subroutines of a library (which may be referred to as a DLL, especially under Windows, or as a DSO (dynamic shared object) under Unix-like systems) into an application program at load time or runtime, rather than linking them in at compile time.

like image 196
icecrime Avatar answered Dec 24 '22 12:12

icecrime


Not a stupid question at all!

The "normal" way this works - is that your program has been linked against a "Shared Library" - in which case, yes, the user needs the OpenCV (or whatever bundle includes the shared library) to work.

If you compiled as a static executable, (using the -static) flag, then it, and all libraries would be included directly into your executable, making a bit of a larger executable that wastes more memory, because it isn't using a shared library.

There are ways that you could compile your program to link only your OpenCV libraries as static - but that only can be done if the bundle included a static library ".a" vs. a shared one ".so".

like image 21
Brad Avatar answered Dec 24 '22 11:12

Brad