title: Build a portable executable with its dependencies
body: <p>I want to build an executable file that looks for its dependencies in a determined relative path. In other words, I want to distribute a zip that contains 2 elements: An executable file and a folder with shared libraries. The executable must be able to find those shared libraries at runtime.</p>
  
  <h1>What I've tested</h1>
  
  <p>For now, I have the project compiled with <code>cmake</code>. I'm using the cmake's command <code>find_library</code> to find the abs. path to the dependencies. </p>
  
  <p>Here is the CMakeLists.txt file:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8)
  project( flowers )
  
  find_library( VFC_LIB1 NAMES opencv_calib3d PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib )
  find_library( VFC_LIB2 NAMES opencv_core PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib )
  ...
  find_library( VFC_LIB16 NAMES opencv_videostab PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib )
  
  SET(VFC_LIBS ${VFC_LIB1} ${VFC_LIB2} ... ${VFC_LIB16} )
  
  SET(VFC_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/opencv)
  
  IF (VFC_LIB1 AND VFC_LIB2 AND ...  AND VFC_LIB16)
  
      include_directories( ${VFC_INCLUDE} )
      include_directories( FlowersDetector NeuronalNet )
      add_executable( flowers FlowersDetector/Flowers.cpp FlowersDetector/FlowersDetector.cpp )
      add_definitions(-std=c++11)
      add_definitions(-D_DEBUG) # TODO remove this for release
      target_link_libraries( flowers ${VFC_LIBS} )
  
  ELSE (VFC_LIB1 AND VFC_LIB2 AND ...  AND VFC_LIB16)
  
      MESSAGE(FATAL_ERROR "OpenCV libraries not found")
  
  ENDIF(VFC_LIB1 AND VFC_LIB2 AND ...  AND VFC_LIB16)
  </code></pre>
  
  <p>The <code>...</code> indicates that the rest is the same as the previous, but changin the sequence number.</p>
  
  <p>For example, <code>VFC_LIB1 AND VFC_LIB2 ... AND VFC_LIB16</code> means that is alwais <code>VFC_LIBx</code>, where <code>x</code> is from 3 to 15.</p>
  
  <h1>The probem</h1>
  
  <p>The problem is that <code>find_library</code> is used to get the absolute path (I have read that), so that does not allow me to copy-paste the executable to other computer and still be able to run it.</p>
  
  <p>What I need is to find the libraries with a relative path, <strong>or other equivalent solution</strong>.</p>
  
  <h1>EDIT</h1>
  
  <p>I have tried to make a "big" executable file with all the libraries "inside it". For that purpose, I have read that I have to link static libraries. I have re-compiled OpenCV building the static libraries (now I have the *.a files plus *.so files.</p>
  
  <p>I have changed the CMakeLists.txt to add these static libraries:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8)
  project( flowers )
  find_package( OpenCV REQUIRED )
  
  SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
  SET(BUILD_SHARED_LIBRARIES OFF)
  SET(CMAKE_EXE_LINKER_FLAGS "-static")
  
  include_directories( ${OpenCV_INCLUDE_DIRS} )
  include_directories( FlowersDetector NeuronalNet )
  add_executable( flowers FlowersDetector/Flowers.cpp FlowersDetector/FlowersDetector.cpp )
  add_definitions(-std=c++11)
  add_definitions(-D_DEBUG)
  target_link_libraries( flowers ${OpenCV_LIBS} )
  </code></pre>
  
  <p>The <code>cmake</code> command runs ok, but when I try to <code>make</code> the project I obtain many errors. Actually, it appears to be only an error, but replied several times. This error is: <code>statically linked applications requires at runtime the shared libraries from the glibc version used for linking</code></p>
  
  <p>Here is the list of errors, avoiding the similar ones: (if the full list may be helpfull, tell me and I'll upload it)</p>
  
  <pre><code>Linking CXX executable flowers
  
  /usr/bin/ld: cannot find -latk-1.0
  
  /usr/bin/ld: cannot find -lgdk_pixbuf-2.0
  
  /usr/local/lib/libopencv_core.a(opencl_core.cpp.o): In function `(anonymous namespace)::opencl_fn14&lt;41, int, _cl_command_queue*, _cl_mem*, unsigned int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long, unsigned long, unsigned long, unsigned long, void const*, unsigned int, _cl_event* const*, _cl_event**&gt;::switch_fn(_cl_command_queue*, _cl_mem*, unsigned int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long, unsigned long, unsigned long, unsigned long, void const*, unsigned int, _cl_event* const*, _cl_event**)':
  
  opencl_core.cpp:(.text._ZN12_GLOBAL__N_111opencl_fn14ILi41EiP17_cl_command_queueP7_cl_memjPKmS6_S6_mmmmPKvjPKP9_cl_eventPSA_E9switch_fnES2_S4_jS6_S6_S6_mmmmS8_jSC_SD_+0x1c0): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
  
  /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libgio-2.0.a(libgio_2_0_la-glocalfileinfo.o): In function `lookup_gid_name':
  
  (.text+0x11a7): warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
  
  [And many similar errors]
  
  collect2: error: ld returned 1 exit status
  
  CMakeFiles/flowers.dir/build.make:143: recipe for target 'flowers' failed
  
  make[2]: *** [flowers] Error 1
  
  CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/flowers.dir/all' failed
  
  make[1]: *** [CMakeFiles/flowers.dir/all] Error 2
  
  Makefile:76: recipe for target 'all' failed
  
  make: *** [all] Error 2
  </code></pre>
  
  <h1>EDIT 2</h1>
  
  <p>I've tested the to re-install OpenCV with only the static libraries (*.a) and to use the default OpenCV CMakeLists:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8)
  project( flowers )
  find_package( OpenCV REQUIRED )
  
  include_directories( ${OpenCV_INCLUDE_DIRS} )
  include_directories( FlowersDetector NeuronalNet )
  add_executable( flowers FlowersDetector/Flowers.cpp FlowersDetector/FlowersDetector.cpp )
  add_definitions(-std=c++11)
  add_definitions(-D_DEBUG)
  target_link_libraries( flowers ${OpenCV_LIBS} )
  </code></pre>
  
  <p>I can compile all, even I can execute the program, but when the program tries to show an image I have the following error:</p>
  
  <pre><code>OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file /media/ubuntu/67803d44-978a-493e-bdfb-5730e7e9f5a8/opencv-3.1.0/modules/highgui/src/window.cpp, line 527
  terminate called after throwing an instance of 'cv::Exception'
    what():  /media/ubuntu/67803d44-978a-493e-bdfb-5730e7e9f5a8/opencv-3.1.0/modules/highgui/src/window.cpp:527: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow
  
  Aborted (core dumped)
  </code></pre>
  
  <p>I have installed with <code>apt-get install</code> <code>libgtk2.0-dev</code> and <code>pkg-config</code>, and also I have downloaded and installed the ATK lib (from <a href="http://www.linuxfromscratch.org/blfs/view/svn/x/atk.html" rel="nofollow">http://www.linuxfromscratch.org/blfs/view/svn/x/atk.html</a>).</p>
  
  <p>After that, I have rebooted and re-run <code>cmake</code> and <code>make</code>. When I execute the program, the error persists.</p>